From 791bd4bfacc31697fc32cf3d45283b4b1d47cadb Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Mon, 28 Oct 2024 17:03:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E6=95=B0=E9=87=8F=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 15 ++++++++++++++- example/GetStockCount/main.go | 19 +++++++++++++++++++ example/GetStockList/main.go | 5 +++-- example/GetStockMinute/main.go | 2 +- example/GetStockQuotes/main.go | 2 +- protocol/const.go | 1 + protocol/model.go | 31 ++++++++++++++++++++++++++++++- protocol/model_test.go | 6 ++++++ 8 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 example/GetStockCount/main.go diff --git a/client.go b/client.go index 261b927..91faa17 100644 --- a/client.go +++ b/client.go @@ -70,6 +70,9 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) { case protocol.TypeConnect: + case protocol.TypeStockCount: + resp, err = protocol.MStockCount.Decode(f.Data) + case protocol.TypeStockList: resp, err = protocol.MStockList.Decode(f.Data) @@ -114,7 +117,17 @@ func (this *Client) connect() error { return err } -// GetStockList 获取市场内指定范围内的所有证券代码 +// GetStockCount 获取市场内的股票数量 +func (this *Client) GetStockCount(exchange protocol.Exchange) (*protocol.StockCountResp, error) { + f := protocol.MStockCount.Frame(exchange) + result, err := this.SendFrame(f) + if err != nil { + return nil, err + } + return result.(*protocol.StockCountResp), nil +} + +// GetStockList 获取市场内指定范围内的所有证券代码,todo 这个start好像没用 func (this *Client) GetStockList(exchange protocol.Exchange, starts ...uint16) (*protocol.StockListResp, error) { f := protocol.MStockList.Frame(exchange, starts...) result, err := this.SendFrame(f) diff --git a/example/GetStockCount/main.go b/example/GetStockCount/main.go new file mode 100644 index 0000000..b9d59bd --- /dev/null +++ b/example/GetStockCount/main.go @@ -0,0 +1,19 @@ +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.GetStockCount(protocol.ExchangeSH) + logs.PanicErr(err) + + logs.Debug(resp.Count) + + <-c.Done() +} diff --git a/example/GetStockList/main.go b/example/GetStockList/main.go index c20b3a6..21b5ebe 100644 --- a/example/GetStockList/main.go +++ b/example/GetStockList/main.go @@ -10,12 +10,13 @@ func main() { c, err := tdx.Dial("124.71.187.122:7709") logs.PanicErr(err) - resp, err := c.GetStockList(protocol.ExchangeSH) - logs.PrintErr(err) + resp, err := c.GetStockList(protocol.ExchangeSH, 200) + logs.PanicErr(err) for _, v := range resp.List { logs.Debug(v) } + logs.Debug("总数:", resp.Count) select {} } diff --git a/example/GetStockMinute/main.go b/example/GetStockMinute/main.go index 9b5f977..f89a85e 100644 --- a/example/GetStockMinute/main.go +++ b/example/GetStockMinute/main.go @@ -11,7 +11,7 @@ func main() { logs.PanicErr(err) resp, err := c.GetStockMinute(protocol.ExchangeSZ, "000001") - logs.PrintErr(err) + logs.PanicErr(err) for _, v := range resp.List { logs.Debug(v) diff --git a/example/GetStockQuotes/main.go b/example/GetStockQuotes/main.go index f1ae691..9327796 100644 --- a/example/GetStockQuotes/main.go +++ b/example/GetStockQuotes/main.go @@ -25,7 +25,7 @@ func main() { protocol.ExchangeSH: "000001", protocol.ExchangeSZ: "600008", }) - logs.PrintErr(err) + logs.PanicErr(err) for _, v := range resp { logs.Debugf("%#v\n", v) diff --git a/protocol/const.go b/protocol/const.go index 0f79638..b286b40 100644 --- a/protocol/const.go +++ b/protocol/const.go @@ -3,6 +3,7 @@ package protocol const ( TypeConnect = 0x000D //建立连接 TypeHeart = 0x0004 //心跳 + TypeStockCount = 0x044E //获取股票数量 TypeStockList = 0x0450 //获取股票代码 TypeStockQuote = 0x053E //行情信息 TypeStockMinute = 0x051D //分时数据 diff --git a/protocol/model.go b/protocol/model.go index c60580b..151ef32 100644 --- a/protocol/model.go +++ b/protocol/model.go @@ -11,6 +11,7 @@ import ( var ( MConnect = connect{} MHeart = heart{} + MStockCount = stockCount{} MStockQuote = stockQuote{} MStockList = stockList{} MStockMinute = stockMinute{} @@ -57,6 +58,34 @@ func (this *heart) Frame() *Frame { + */ + +type StockCountResp struct { + Count uint16 +} + +type stockCount struct{} + +// Frame 0c0200000001080008004e04000075c73301 +func (this *stockCount) Frame(exchange Exchange) *Frame { + return &Frame{ + Control: Control01, + Type: TypeStockCount, + Data: []byte{exchange.Uint8(), 0x0, 0x75, 0xc7, 0x33, 0x01}, //后面的4字节不知道啥意思 + } +} + +func (this *stockCount) Decode(bs []byte) (*StockCountResp, error) { + if len(bs) < 2 { + return nil, errors.New("数据长度不足") + } + return &StockCountResp{Count: Uint16(bs)}, nil +} + +/* + + + */ type StockListResp struct { @@ -73,7 +102,7 @@ type Stock struct { } func (this *Stock) String() string { - return fmt.Sprintf("%s(%s)", this.Name, this.Code) + return fmt.Sprintf("%s(%s)", this.Code, this.Name) } type stockList struct{} diff --git a/protocol/model_test.go b/protocol/model_test.go index 5b5517c..a2fdd67 100644 --- a/protocol/model_test.go +++ b/protocol/model_test.go @@ -51,3 +51,9 @@ func Test_securityList_Frame(t *testing.T) { f := MStockList.Frame(ExchangeSH, 0) t.Log(f.Bytes().HEX()) } + +func Test_stockCount_Frame(t *testing.T) { + //预期0c0200000001080008004e04000075c73301 + // 0c0000000001070007004e040075c73301 + t.Log(MStockCount.Frame(ExchangeSH).Bytes().HEX()) +}