diff --git a/itch/5.0/stock_directory.go b/itch/5.0/stock_directory.go index 628aec5..0aa5533 100644 --- a/itch/5.0/stock_directory.go +++ b/itch/5.0/stock_directory.go @@ -161,12 +161,12 @@ func (s StockDirectory) Bytes() []byte { binary.BigEndian.PutUint64(data[3:11], uint64(s.Timestamp.Nanoseconds())) binary.BigEndian.PutUint16(data[3:5], s.TrackingNumber) - copy(data[11:19], []byte(fmt.Sprintf("'%-8s'", s.Stock))) + copy(data[11:19], []byte(fmt.Sprintf("%-8s", s.Stock))) data[19] = byte(s.MarketCategory) data[20] = byte(s.FinancialStatusIndicator) - binary.BigEndian.AppendUint32(data[21:25], s.RoundLotSize) + binary.BigEndian.PutUint32(data[21:25], s.RoundLotSize) if s.RoundLotsOnly { data[25] = 'Y' @@ -175,14 +175,12 @@ func (s StockDirectory) Bytes() []byte { } data[26] = byte(s.IssueClassification) - copy(data[27:29], []byte(s.IssueSubType)) + underlying := string(s.IssueSubType) + copy(data[27:29], []byte(fmt.Sprintf("%-2s", underlying))) - if s.Authenticity == AUTHENTICITY_LIVE { - data[29] = 'P' - } else { - data[29] = 'T' - } + data[29] = byte(s.Authenticity) + // TODO: Make types data[30] = []byte(s.ShortSaleThresholdIndicator)[0] data[31] = []byte(s.IpoFlag)[0] data[32] = []byte(s.LuldReferencePriceTier)[0] @@ -204,6 +202,11 @@ func ParseStockDirectory(data []byte) (StockDirectory, error) { return StockDirectory{}, NewInvalidPacketSize(stockDirectorySize, len(data)) } + for i := range len(data) { + fmt.Printf("%v,", data[i]) + } + fmt.Println("") + locate := binary.BigEndian.Uint16(data[1:3]) tracking := binary.BigEndian.Uint16(data[3:5]) data[3] = 0 diff --git a/itch/5.0/stock_directory_test.go b/itch/5.0/stock_directory_test.go new file mode 100644 index 0000000..07ef33f --- /dev/null +++ b/itch/5.0/stock_directory_test.go @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2022 Mark Edward Winter + */ + +package itch + +import ( + "testing" + "time" + + "github.com/google/go-cmp/cmp" +) + +func TestParseStockDirectory(t *testing.T) { + timestamp, _ := time.ParseDuration("3h7m14.939262551s") + + type args struct { + data []byte + } + tests := []struct { + name string + args args + want StockDirectory + wantErr bool + }{ + { + name: "", + args: args{data: []byte{82, 3, 81, 0, 0, 10, 55, 214, 144, 86, 87, 66, 72, 66, 32, 32, 32, 32, 32, 65, 32, 0, 0, 0, 100, 78, 67, 90, 32, 80, 78, 32, 50, 78, 0, 0, 0, 0, 78}}, + want: StockDirectory{ + Timestamp: timestamp, + Stock: "BHB", + StockLocate: 849, + TrackingNumber: 0, + RoundLotSize: 100, + RoundLotsOnly: false, + IssueSubType: ICS_NOT_APPLICABLE, + IssueClassification: IC_COMMON_STOCK, + InverseIndicator: false, + Authenticity: AUTHENTICITY_LIVE, + EtpLeverageFactor: 0, + ShortSaleThresholdIndicator: "N", + IpoFlag: " ", + LuldReferencePriceTier: "2", + EtpFlag: "N", + MarketCategory: MKTCTG_NYSE_AMERICAN, + FinancialStatusIndicator: FSI_NOT_AVAILABLE, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := ParseStockDirectory(tt.args.data) + if (err != nil) != tt.wantErr { + t.Errorf("ParseStockDirectory() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !cmp.Equal(got, tt.want) { + t.Errorf("%v", cmp.Diff(tt.want, got)) + } + }) + } +} + +func TestStockDirectory_Bytes(t *testing.T) { + timestamp, _ := time.ParseDuration("3h7m14.939262551s") + + tests := []struct { + name string + s StockDirectory + want []byte + }{ + { + name: "BHB", + s: StockDirectory{ + Timestamp: timestamp, + Stock: "BHB", + StockLocate: 849, + TrackingNumber: 0, + RoundLotSize: 100, + RoundLotsOnly: false, + IssueSubType: ICS_NOT_APPLICABLE, + IssueClassification: IC_COMMON_STOCK, + InverseIndicator: false, + Authenticity: AUTHENTICITY_LIVE, + EtpLeverageFactor: 0, + ShortSaleThresholdIndicator: "N", + IpoFlag: " ", + LuldReferencePriceTier: "2", + EtpFlag: "N", + MarketCategory: MKTCTG_NYSE_AMERICAN, + FinancialStatusIndicator: FSI_NOT_AVAILABLE, + }, + want: []byte{82, 3, 81, 0, 0, 10, 55, 214, 144, 86, 87, 66, 72, 66, 32, 32, 32, 32, 32, 65, 32, 0, 0, 0, 100, 78, 67, 90, 32, 80, 78, 32, 50, 78, 0, 0, 0, 0, 78}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.s.Bytes(); !cmp.Equal(got, tt.want) { + t.Errorf("StockDirectory.Bytes() = %v, want %v", got, tt.want) + } + }) + } +}