mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加了获取股票数量的接口
This commit is contained in:
15
client.go
15
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)
|
||||
|
||||
19
example/GetStockCount/main.go
Normal file
19
example/GetStockCount/main.go
Normal file
@@ -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()
|
||||
}
|
||||
@@ -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 {}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -3,6 +3,7 @@ package protocol
|
||||
const (
|
||||
TypeConnect = 0x000D //建立连接
|
||||
TypeHeart = 0x0004 //心跳
|
||||
TypeStockCount = 0x044E //获取股票数量
|
||||
TypeStockList = 0x0450 //获取股票代码
|
||||
TypeStockQuote = 0x053E //行情信息
|
||||
TypeStockMinute = 0x051D //分时数据
|
||||
|
||||
@@ -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{}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user