From eea4852f6dcec8918f21fafcfb1aff2ec230774a Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Mon, 28 Oct 2024 16:36:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=82=A1=E7=A5=A8=E7=9A=84?= =?UTF-8?q?=E8=8B=B1=E6=96=87Security=E4=B8=BAStock=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E5=88=86=E6=97=B6=E6=95=B0=E6=8D=AE,=E5=80=9F?= =?UTF-8?q?=E9=89=B4=E7=9A=84=E7=AC=AC=E4=B8=89=E6=96=B9=E5=A5=BD=E5=83=8F?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E4=B9=9F=E4=B8=8D=E5=AF=B9,=E8=BF=9F?= =?UTF-8?q?=E7=82=B9=E5=86=8D=E6=89=BE=E4=B8=AA=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E5=8C=85=E8=AF=95=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/{testlist => GetStockList}/main.go | 2 +- example/GetStockMinute/main.go | 21 +++++ example/{test => GetStockQuotes}/main.go | 2 +- protocol/frame.go | 3 +- protocol/{model_stock_quote.go => model.go} | 83 +++++++++++++++++-- ...odel_stock_quote_test.go => model_test.go} | 0 6 files changed, 103 insertions(+), 8 deletions(-) rename example/{testlist => GetStockList}/main.go (83%) create mode 100644 example/GetStockMinute/main.go rename example/{test => GetStockQuotes}/main.go (94%) rename protocol/{model_stock_quote.go => model.go} (81%) rename protocol/{model_stock_quote_test.go => model_test.go} (100%) diff --git a/example/testlist/main.go b/example/GetStockList/main.go similarity index 83% rename from example/testlist/main.go rename to example/GetStockList/main.go index f800a10..c20b3a6 100644 --- a/example/testlist/main.go +++ b/example/GetStockList/main.go @@ -10,7 +10,7 @@ func main() { c, err := tdx.Dial("124.71.187.122:7709") logs.PanicErr(err) - resp, err := c.GetSecurityList(protocol.ExchangeSH) + resp, err := c.GetStockList(protocol.ExchangeSH) logs.PrintErr(err) for _, v := range resp.List { diff --git a/example/GetStockMinute/main.go b/example/GetStockMinute/main.go new file mode 100644 index 0000000..9b5f977 --- /dev/null +++ b/example/GetStockMinute/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/injoyai/logs" + "github.com/injoyai/tdx" + "github.com/injoyai/tdx/protocol" +) + +func main() { + c, err := tdx.Dial("124.71.187.122:7709") + logs.PanicErr(err) + + resp, err := c.GetStockMinute(protocol.ExchangeSZ, "000001") + logs.PrintErr(err) + + for _, v := range resp.List { + logs.Debug(v) + } + + <-c.Done() +} diff --git a/example/test/main.go b/example/GetStockQuotes/main.go similarity index 94% rename from example/test/main.go rename to example/GetStockQuotes/main.go index 3442d4c..f1ae691 100644 --- a/example/test/main.go +++ b/example/GetStockQuotes/main.go @@ -21,7 +21,7 @@ func main() { b1cb74001c00000000000d005100bd00789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13 b1cb74000c02000000003e05ac00ac000102020000303030303031601294121a1c2d4eadabcf0ed412aae5fc01afb0024561124fbcc08301afa47900b2e3174100bf68871a4201b741b6144302bb09af334403972e96354504ac09b619560e00000000f8ff601201363030303038b60fba04060607429788a70efa04ada37ab2531c12974d91e7449dbc354184b6010001844bad324102b5679ea1014203a65abd8d0143048a6ba4dd01440587e101b3d2029613000000000000b60f */ - resp, err := c.GetSecurityQuotes(map[protocol.Exchange]string{ + resp, err := c.GetStockQuotes(map[protocol.Exchange]string{ protocol.ExchangeSH: "000001", protocol.ExchangeSZ: "600008", }) diff --git a/protocol/frame.go b/protocol/frame.go index 448c6ca..13c190b 100644 --- a/protocol/frame.go +++ b/protocol/frame.go @@ -65,7 +65,8 @@ type Response struct { /* Decode -b1cb7400 1c 00000000 00 0d00 5100 bd00 789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13 +帧头 |未知 |消息ID |控制码 |数据类型 |未解压长度 |解压长度 |数据域 +b1cb7400 |1c |00000000 |00 |0d00 |5100 |bd00 |789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13 */ func Decode(bs []byte) (*Response, error) { if len(bs) < 16 { diff --git a/protocol/model_stock_quote.go b/protocol/model.go similarity index 81% rename from protocol/model_stock_quote.go rename to protocol/model.go index 625b105..c60580b 100644 --- a/protocol/model_stock_quote.go +++ b/protocol/model.go @@ -4,13 +4,16 @@ import ( "errors" "fmt" "github.com/injoyai/conv" + "github.com/injoyai/logs" "strings" ) var ( - MConnect = connect{} - MStockQuote = stockQuote{} - MStockList = stockList{} + MConnect = connect{} + MHeart = heart{} + MStockQuote = stockQuote{} + MStockList = stockList{} + MStockMinute = stockMinute{} ) type ConnectResp struct { @@ -39,6 +42,21 @@ func (connect) Decode(bs []byte) (*ConnectResp, error) { + */ + +type heart struct{} + +func (this *heart) Frame() *Frame { + return &Frame{ + Control: Control01, + Type: TypeHeart, + } +} + +/* + + + */ type StockListResp struct { @@ -64,7 +82,7 @@ func (stockList) Frame(exchange Exchange, starts ...uint16) *Frame { start := conv.DefaultUint16(0, starts...) return &Frame{ Control: Control01, - Type: TypeSecurityList, + Type: TypeStockList, Data: []byte{exchange.Uint8(), 0x0, uint8(start), uint8(start >> 8)}, } } @@ -159,7 +177,7 @@ type stockQuote struct{} func (this stockQuote) Frame(m map[Exchange]string) (*Frame, error) { f := &Frame{ Control: Control01, - Type: TypeSecurityQuote, + Type: TypeStockQuote, Data: []byte{0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, } @@ -265,3 +283,58 @@ func (this stockQuote) Decode(bs []byte) StockQuotesResp { return resp } + +/* + + + + */ + +type StockMinuteResp struct { + Count uint16 + List []PriceLevel +} + +type stockMinute struct{} + +func (this *stockMinute) Frame(exchange Exchange, code string) (*Frame, error) { + if len(code) != 6 { + return nil, errors.New("股票代码长度错误") + } + codeBs := []byte(code) + codeBs = append(codeBs, 0x0, 0x0, 0x0, 0x0) + return &Frame{ + Control: Control01, + Type: TypeStockMinute, + Data: append([]byte{exchange.Uint8(), 0x0}, codeBs...), + }, nil +} + +func (this *stockMinute) Decode(bs []byte) (*StockMinuteResp, error) { + + if len(bs) < 6 { + return nil, errors.New("数据长度不足") + } + + resp := &StockMinuteResp{ + Count: Uint16(bs[:2]), + } + //2-6字节是啥? + bs = bs[6:] + price := Price(0) + + for i := uint16(0); i < resp.Count; i++ { + bs, price = GetPrice(bs) + var what Price + bs, what = GetPrice(bs) //这个是什么 + logs.Debug(price, what) + var number int + bs, number = CutInt(bs) + resp.List = append(resp.List, PriceLevel{ + Price: price, + Number: number, + }) + } + + return resp, nil +} diff --git a/protocol/model_stock_quote_test.go b/protocol/model_test.go similarity index 100% rename from protocol/model_stock_quote_test.go rename to protocol/model_test.go