From 5ff1b4fe052434a5b493a63931ecb59de2879e36 Mon Sep 17 00:00:00 2001 From: bense Date: Tue, 10 May 2022 16:37:44 +0800 Subject: [PATCH] get index bar --- client.go | 66 +++++++++++++-- client_test.go | 55 +++++++++++-- proto/get_index_bars.go | 150 +++++++++++++++++++++++++++++++++++ proto/get_security_bars.go | 140 ++++++++++++++++++++++++++++++++ proto/get_security_count.go | 53 ++++++++----- proto/get_security_list.go | 110 +++++++++++++++++++++++++ proto/get_security_quotes.go | 59 +++++++++----- proto/hello1.go | 43 +++++++--- proto/hello2.go | 42 +++++++--- proto/proto.go | 1 + 10 files changed, 637 insertions(+), 82 deletions(-) create mode 100644 proto/get_index_bars.go create mode 100644 proto/get_security_bars.go create mode 100644 proto/get_security_list.go diff --git a/client.go b/client.go index 729c0e5..0cf84ad 100644 --- a/client.go +++ b/client.go @@ -101,9 +101,9 @@ func (client *Client) do(msg proto.Msg) error { b := bytes.NewReader(msgData) r, _ := zlib.NewReader(b) io.Copy(&out, r) - err = msg.UnSerialize(header, out.Bytes()) + err = msg.UnSerialize(&header, out.Bytes()) } else { - err = msg.UnSerialize(header, msgData) + err = msg.UnSerialize(&header, msgData) } return err @@ -120,7 +120,7 @@ func (client *Client) Connect() (*proto.Hello1Reply, error) { if err != nil { return nil, err } - return obj.Reply, err + return obj.Reply(), err } // Disconnect 断开服务器 @@ -131,21 +131,73 @@ func (client *Client) Disconnect() error { // GetSecurityCount 获取指定市场内的证券数目 func (client *Client) GetSecurityCount(market uint16) (*proto.GetSecurityCountReply, error) { obj := proto.NewGetSecurityCount() - obj.SetParams(market) + obj.SetParams(&proto.GetSecurityCountRequest{ + Market: market, + }) err := client.do(obj) if err != nil { return nil, err } - return obj.Reply, err + return obj.Reply(), err } // GetSecurityQuotes 获取盘口五档报价 func (client *Client) GetSecurityQuotes(params []proto.Stock) (*proto.GetSecurityQuotesReply, error) { obj := proto.NewGetSecurityQuotes() - obj.SetParams(params) + obj.SetParams(&proto.GetSecurityQuotesRequest{StockList: params}) err := client.do(obj) if err != nil { return nil, err } - return obj.Reply, err + return obj.Reply(), err +} + +// GetSecurityList 获取市场内指定范围内的所有证券代码 +func (client *Client) GetSecurityList(market uint16, start uint16) (*proto.GetSecurityListReply, error) { + obj := proto.NewGetSecurityList() + obj.SetParams(&proto.GetSecurityListRequest{Market: market, Start: start}) + err := client.do(obj) + if err != nil { + return nil, err + } + return obj.Reply(), err +} + +// GetSecurityBars 获取股票K线 +// e Category, byte Market, char* Zqdm, short Start, short& Count, char* Result, char* ErrInfo +func (client *Client) GetSecurityBars(category uint16, market uint16, code string, start uint16, count uint16) (*proto.GetSecurityBarsReply, error) { + obj := proto.NewGetSecurityBars() + _code := [6]byte{} + copy(_code[:], code) + obj.SetParams(&proto.GetSecurityBarsRequest{ + Market: market, + Code: _code, + Category: category, + Start: start, + Count: count, + }) + err := client.do(obj) + if err != nil { + return nil, err + } + return obj.Reply(), err +} + +// GetIndexBars 获取指数K线 +func (client *Client) GetIndexBars(category uint16, market uint16, code string, start uint16, count uint16) (*proto.GetIndexBarsReply, error) { + obj := proto.NewGetIndexBars() + _code := [6]byte{} + copy(_code[:], code) + obj.SetParams(&proto.GetIndexBarsRequest{ + Market: market, + Code: _code, + Category: category, + Start: start, + Count: count, + }) + err := client.do(obj) + if err != nil { + return nil, err + } + return obj.Reply(), err } diff --git a/client_test.go b/client_test.go index bde5ee6..1418b70 100644 --- a/client_test.go +++ b/client_test.go @@ -44,14 +44,7 @@ func Test_tdx_GetSecurityCount(t *testing.T) { } fmt.Println(reply) - reply, err = api.GetSecurityCount(MarketSz) - if err != nil { - fmt.Println(err) - } - fmt.Println(reply) - _ = api.Disconnect() - } func Test_tdx_GetSecurityQuotes(t *testing.T) { @@ -69,3 +62,51 @@ func Test_tdx_GetSecurityQuotes(t *testing.T) { _ = api.Disconnect() } + +func Test_tdx_GetSecurityList(t *testing.T) { + api := prepare() + reply, err := api.GetSecurityList(MarketSh, 0) + if err != nil { + fmt.Println(err) + } + + fmt.Println(reply) + + _ = api.Disconnect() + +} + +func Test_tdx_GetSecurityBars(t *testing.T) { + // GetSecurityBars 与 GetIndexBars 使用同一个接口靠market区分 + api := prepare() + reply, err := api.GetSecurityBars(proto.KLINE_TYPE_RI_K, 0, "000001", 0, 10) + if err != nil { + fmt.Println(err) + } + + fmt.Println(reply) + for _, bar := range reply.List { + fmt.Println(bar) + } + + _ = api.Disconnect() + +} + +func Test_tdx_GetIndexBars(t *testing.T) { + // GetSecurityBars 与 GetIndexBars 使用同一个接口靠market区分 + + api := prepare() + reply, err := api.GetIndexBars(proto.KLINE_TYPE_RI_K, 1, "000001", 0, 10) + if err != nil { + fmt.Println(err) + } + + fmt.Println(reply) + for _, bar := range reply.List { + fmt.Println(bar) + } + + _ = api.Disconnect() + +} diff --git a/proto/get_index_bars.go b/proto/get_index_bars.go new file mode 100644 index 0000000..58d6149 --- /dev/null +++ b/proto/get_index_bars.go @@ -0,0 +1,150 @@ +package proto + +import ( + "bytes" + "encoding/binary" + "encoding/hex" + "fmt" +) + +type GetIndexBars struct { + reqHeader *ReqHeader + respHeader *RespHeader + request *GetIndexBarsRequest + reply *GetIndexBarsReply + + contentHex string +} + +type GetIndexBarsRequest struct { + Market uint16 + Code [6]byte + Category uint16 // 种类 5分钟 10分钟 + I uint16 // 未知 填充 + Start uint16 + Count uint16 +} + +type GetIndexBarsReply struct { + Count uint16 + List []GetIndexBar +} + +type GetIndexBar struct { + Open float64 + Close float64 + High float64 + Low float64 + Vol float64 + Amount float64 + Year int + Month int + Day int + Hour int + Minute int + DateTime string + UpCount uint16 + DownCount uint16 +} + +func NewGetIndexBars() *GetIndexBars { + obj := new(GetIndexBars) + obj.reqHeader = new(ReqHeader) + obj.respHeader = new(RespHeader) + obj.request = new(GetIndexBarsRequest) + obj.reply = new(GetIndexBarsReply) + + obj.reqHeader.Zip = 0x0c + obj.reqHeader.SeqID = seqID() + obj.reqHeader.PacketType = 0x00 + //obj.reqHeader.PkgLen1 = + //obj.reqHeader.PkgLen2 = + obj.reqHeader.Method = KMSG_INDEXBARS + obj.contentHex = "00000000000000000000" + return obj +} +func (obj *GetIndexBars) SetParams(req *GetIndexBarsRequest) { + obj.request = req + obj.request.I = 1 +} + +func (obj *GetIndexBars) Serialize() ([]byte, error) { + obj.reqHeader.PkgLen1 = 0x1c + obj.reqHeader.PkgLen2 = 0x1c + + buf := new(bytes.Buffer) + err := binary.Write(buf, binary.LittleEndian, obj.reqHeader) + err = binary.Write(buf, binary.LittleEndian, obj.request) + b, err := hex.DecodeString(obj.contentHex) + buf.Write(b) + + //b, err := hex.DecodeString(obj.contentHex) + //buf.Write(b) + + //err = binary.Write(buf, binary.LittleEndian, uint16(len(obj.stocks))) + + return buf.Bytes(), err +} + +func (obj *GetIndexBars) UnSerialize(header interface{}, data []byte) error { + obj.respHeader = header.(*RespHeader) + + fmt.Println(hex.EncodeToString(data)) + + pos := 0 + err := binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &obj.reply.Count) + pos += 2 + + pre_diff_base := 0 + //lasttime := "" + for index := uint16(0); index < obj.reply.Count; index++ { + ele := GetIndexBar{} + + ele.Year, ele.Month, ele.Day, ele.Hour, ele.Minute = getdatetime(int(obj.request.Category), data, &pos) + + //if index == 0 { + // ele.Year, ele.Month, ele.Day, ele.Hour, ele.Minute = getdatetime(int(obj.request.Category), data, &pos) + //} else { + // ele.Year, ele.Month, ele.Day, ele.Hour, ele.Minute = getdatetimenow(int(obj.request.Category), lasttime) + //} + ele.DateTime = fmt.Sprintf("%d-%02d-%02d %02d:%02d:00", ele.Year, ele.Month, ele.Day, ele.Hour, ele.Minute) + + price_open_diff := getprice(data, &pos) + price_close_diff := getprice(data, &pos) + + price_high_diff := getprice(data, &pos) + price_low_diff := getprice(data, &pos) + + var ivol uint32 + binary.Read(bytes.NewBuffer(data[pos:pos+4]), binary.LittleEndian, &ivol) + ele.Vol = getvolume(int(ivol)) + pos += 4 + + var dbvol uint32 + binary.Read(bytes.NewBuffer(data[pos:pos+4]), binary.LittleEndian, &dbvol) + ele.Amount = getvolume(int(dbvol)) + pos += 4 + + binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &ele.UpCount) + pos += 2 + binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &ele.DownCount) + pos += 2 + + ele.Open = float64(price_open_diff+pre_diff_base) / 1000.0 + price_open_diff += pre_diff_base + + ele.Close = float64(price_open_diff+price_close_diff) / 1000.0 + ele.High = float64(price_open_diff+price_high_diff) / 1000.0 + ele.Low = float64(price_open_diff+price_low_diff) / 1000.0 + + pre_diff_base = price_open_diff + price_close_diff + //lasttime = ele.DateTime + + obj.reply.List = append(obj.reply.List, ele) + } + return err +} + +func (obj *GetIndexBars) Reply() *GetIndexBarsReply { + return obj.reply +} diff --git a/proto/get_security_bars.go b/proto/get_security_bars.go new file mode 100644 index 0000000..175fac3 --- /dev/null +++ b/proto/get_security_bars.go @@ -0,0 +1,140 @@ +package proto + +import ( + "bytes" + "encoding/binary" + "encoding/hex" + "fmt" +) + +type GetSecurityBars struct { + reqHeader *ReqHeader + respHeader *RespHeader + request *GetSecurityBarsRequest + reply *GetSecurityBarsReply + + contentHex string +} + +type GetSecurityBarsRequest struct { + Market uint16 + Code [6]byte + Category uint16 // 种类 5分钟 10分钟 + I uint16 // 未知 填充 + Start uint16 + Count uint16 +} + +type GetSecurityBarsReply struct { + Count uint16 + List []GetSecurityBar +} + +type GetSecurityBar struct { + Open float64 + Close float64 + High float64 + Low float64 + Vol float64 + Amount float64 + Year int + Month int + Day int + Hour int + Minute int + DateTime string + //UpCount uint16 + //DownCount uint16 +} + +func NewGetSecurityBars() *GetSecurityBars { + obj := new(GetSecurityBars) + obj.reqHeader = new(ReqHeader) + obj.respHeader = new(RespHeader) + obj.request = new(GetSecurityBarsRequest) + obj.reply = new(GetSecurityBarsReply) + + obj.reqHeader.Zip = 0x0c + obj.reqHeader.SeqID = seqID() + obj.reqHeader.PacketType = 0x00 + //obj.reqHeader.PkgLen1 = + //obj.reqHeader.PkgLen2 = + obj.reqHeader.Method = KMSG_SECURITYBARS + obj.contentHex = "00000000000000000000" + return obj +} +func (obj *GetSecurityBars) SetParams(req *GetSecurityBarsRequest) { + obj.request = req + obj.request.I = 1 +} + +func (obj *GetSecurityBars) Serialize() ([]byte, error) { + obj.reqHeader.PkgLen1 = 0x1c + obj.reqHeader.PkgLen2 = 0x1c + + buf := new(bytes.Buffer) + err := binary.Write(buf, binary.LittleEndian, obj.reqHeader) + err = binary.Write(buf, binary.LittleEndian, obj.request) + b, err := hex.DecodeString(obj.contentHex) + buf.Write(b) + + //b, err := hex.DecodeString(obj.contentHex) + //buf.Write(b) + + //err = binary.Write(buf, binary.LittleEndian, uint16(len(obj.stocks))) + + return buf.Bytes(), err +} + +// 结果数据都是\n,\t分隔的中文字符串,比如查询K线数据,返回的结果字符串就形如 +///“时间\t开盘价\t收盘价\t最高价\t最低价\t成交量\t成交额\n +///20150519\t4.644000\t4.732000\t4.747000\t4.576000\t146667487\t683638848.000000\n +///20150520\t4.756000\t4.850000\t4.960000\t4.756000\t353161092\t1722953216.000000” +func (obj *GetSecurityBars) UnSerialize(header interface{}, data []byte) error { + obj.respHeader = header.(*RespHeader) + + pos := 0 + err := binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &obj.reply.Count) + pos += 2 + + pre_diff_base := 0 + + for index := uint16(0); index < obj.reply.Count; index++ { + ele := GetSecurityBar{} + ele.Year, ele.Month, ele.Day, ele.Hour, ele.Minute = getdatetime(int(obj.request.Category), data, &pos) + + ele.DateTime = fmt.Sprintf("%d-%02d-%02d %02d:%02d:00", ele.Year, ele.Month, ele.Day, ele.Hour, ele.Minute) + + price_open_diff := getprice(data, &pos) + price_close_diff := getprice(data, &pos) + + price_high_diff := getprice(data, &pos) + price_low_diff := getprice(data, &pos) + + var ivol uint32 + binary.Read(bytes.NewBuffer(data[pos:pos+4]), binary.LittleEndian, &ivol) + ele.Vol = getvolume(int(ivol)) + pos += 4 + + var dbvol uint32 + binary.Read(bytes.NewBuffer(data[pos:pos+4]), binary.LittleEndian, &dbvol) + ele.Amount = getvolume(int(dbvol)) + pos += 4 + + ele.Open = float64(price_open_diff+pre_diff_base) / 1000.0 + price_open_diff += pre_diff_base + + ele.Close = float64(price_open_diff+price_close_diff) / 1000.0 + ele.High = float64(price_open_diff+price_high_diff) / 1000.0 + ele.Low = float64(price_open_diff+price_low_diff) / 1000.0 + + pre_diff_base = price_open_diff + price_close_diff + + obj.reply.List = append(obj.reply.List, ele) + } + return err +} + +func (obj *GetSecurityBars) Reply() *GetSecurityBarsReply { + return obj.reply +} diff --git a/proto/get_security_count.go b/proto/get_security_count.go index 483540f..56221bd 100644 --- a/proto/get_security_count.go +++ b/proto/get_security_count.go @@ -6,13 +6,16 @@ import ( "encoding/hex" ) -// GetSecurityCount type GetSecurityCount struct { - ReqHeader - content string - Reply *GetSecurityCountReply + reqHeader *ReqHeader + respHeader *RespHeader + request *GetSecurityCountRequest + reply *GetSecurityCountReply + contentHex string +} - market uint16 +type GetSecurityCountRequest struct { + Market uint16 } type GetSecurityCountReply struct { @@ -20,32 +23,42 @@ type GetSecurityCountReply struct { } func NewGetSecurityCount() *GetSecurityCount { - obj := &GetSecurityCount{} - obj.Zip = 0x0c - obj.SeqID = seqID() - obj.PacketType = 0x01 - obj.Method = KMSG_SECURITYCOUNT - obj.content = "75c73301" // 未解 + obj := new(GetSecurityCount) + obj.reqHeader = new(ReqHeader) + obj.respHeader = new(RespHeader) + obj.request = new(GetSecurityCountRequest) + obj.reply = new(GetSecurityCountReply) + + obj.reqHeader.Zip = 0x0c + obj.reqHeader.SeqID = seqID() + obj.reqHeader.PacketType = 0x01 + obj.reqHeader.Method = KMSG_SECURITYCOUNT + obj.contentHex = "75c73301" // 未解 return obj } -func (obj *GetSecurityCount) SetParams(market uint16) { - obj.market = market +func (obj *GetSecurityCount) SetParams(req *GetSecurityCountRequest) { + obj.request = req } func (obj *GetSecurityCount) Serialize() ([]byte, error) { - obj.PkgLen1 = 2 + uint16(len(obj.content)) + 2 - obj.PkgLen2 = 2 + uint16(len(obj.content)) + 2 + obj.reqHeader.PkgLen1 = 2 + uint16(len(obj.contentHex)) + 2 + obj.reqHeader.PkgLen2 = 2 + uint16(len(obj.contentHex)) + 2 buf := new(bytes.Buffer) - err := binary.Write(buf, binary.LittleEndian, obj.ReqHeader) - err = binary.Write(buf, binary.LittleEndian, obj.market) - b, err := hex.DecodeString(obj.content) + err := binary.Write(buf, binary.LittleEndian, obj.reqHeader) + err = binary.Write(buf, binary.LittleEndian, obj.request) + b, err := hex.DecodeString(obj.contentHex) buf.Write(b) return buf.Bytes(), err } func (obj *GetSecurityCount) UnSerialize(header interface{}, data []byte) error { - obj.Reply = new(GetSecurityCountReply) - obj.Reply.Count = binary.LittleEndian.Uint16(data[:2]) + obj.respHeader = header.(*RespHeader) + + obj.reply.Count = binary.LittleEndian.Uint16(data[:2]) return nil } + +func (obj *GetSecurityCount) Reply() *GetSecurityCountReply { + return obj.reply +} diff --git a/proto/get_security_list.go b/proto/get_security_list.go new file mode 100644 index 0000000..518bf2b --- /dev/null +++ b/proto/get_security_list.go @@ -0,0 +1,110 @@ +package proto + +import ( + "bytes" + "encoding/binary" + "gotdx/utils" +) + +type GetSecurityList struct { + reqHeader *ReqHeader + respHeader *RespHeader + request *GetSecurityListRequest + reply *GetSecurityListReply + + contentHex string +} + +type GetSecurityListRequest struct { + Market uint16 + Start uint16 +} + +type GetSecurityListReply struct { + Count uint16 + List []Security +} + +type Security struct { + Code string + VolUnit uint16 + DecimalPoint int8 + Name string + PreClose float64 +} + +func NewGetSecurityList() *GetSecurityList { + obj := new(GetSecurityList) + obj.reqHeader = new(ReqHeader) + obj.respHeader = new(RespHeader) + obj.request = new(GetSecurityListRequest) + obj.reply = new(GetSecurityListReply) + + obj.reqHeader.Zip = 0x0c + obj.reqHeader.SeqID = seqID() + obj.reqHeader.PacketType = 0x01 + obj.reqHeader.Method = KMSG_SECURITYLIST + return obj +} + +func (obj *GetSecurityList) SetParams(req *GetSecurityListRequest) { + obj.request = req +} + +func (obj *GetSecurityList) Serialize() ([]byte, error) { + obj.reqHeader.PkgLen1 = 2 + 4 + obj.reqHeader.PkgLen2 = 2 + 4 + + buf := new(bytes.Buffer) + err := binary.Write(buf, binary.LittleEndian, obj.reqHeader) + err = binary.Write(buf, binary.LittleEndian, obj.request) + + //b, err := hex.DecodeString(obj.contentHex) + //buf.Write(b) + + //err = binary.Write(buf, binary.LittleEndian, uint16(len(obj.stocks))) + + return buf.Bytes(), err +} + +func (obj *GetSecurityList) UnSerialize(header interface{}, data []byte) error { + obj.respHeader = header.(*RespHeader) + + //fmt.Println(hex.EncodeToString(data)) + pos := 0 + err := binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &obj.reply.Count) + pos += 2 + for index := uint16(0); index < obj.reply.Count; index++ { + ele := Security{} + var code [6]byte + binary.Read(bytes.NewBuffer(data[pos:pos+6]), binary.LittleEndian, &code) + pos += 6 + ele.Code = string(code[:]) + + binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &ele.VolUnit) + pos += 2 + + var name [8]byte + binary.Read(bytes.NewBuffer(data[pos:pos+8]), binary.LittleEndian, &name) + pos += 8 + + ele.Code = utils.Utf8ToGbk(name[:]) + + pos += 4 + binary.Read(bytes.NewBuffer(data[pos:pos+1]), binary.LittleEndian, &ele.DecimalPoint) + pos += 1 + var precloseraw uint32 + binary.Read(bytes.NewBuffer(data[pos:pos+4]), binary.LittleEndian, &precloseraw) + pos += 4 + + ele.PreClose = getvolume(int(precloseraw)) + pos += 4 + + obj.reply.List = append(obj.reply.List, ele) + } + return err +} + +func (obj *GetSecurityList) Reply() *GetSecurityListReply { + return obj.reply +} diff --git a/proto/get_security_quotes.go b/proto/get_security_quotes.go index e5b5d6b..05fe30a 100644 --- a/proto/get_security_quotes.go +++ b/proto/get_security_quotes.go @@ -8,13 +8,13 @@ import ( "gotdx/utils" ) -// GetSecurityQuotes //获取盘口五档报价 type GetSecurityQuotes struct { - ReqHeader - contentHex string - Reply *GetSecurityQuotesReply + reqHeader *ReqHeader + respHeader *RespHeader + request *GetSecurityQuotesRequest + reply *GetSecurityQuotesReply - stocks []Stock + contentHex string } type Stock struct { @@ -22,6 +22,10 @@ type Stock struct { Code string } +type GetSecurityQuotesRequest struct { + StockList []Stock +} + type GetSecurityQuotesReply struct { Count uint16 List []SecurityQuote @@ -83,30 +87,36 @@ type Level struct { } func NewGetSecurityQuotes() *GetSecurityQuotes { - obj := &GetSecurityQuotes{} - obj.Zip = 0x0c - obj.SeqID = seqID() - obj.PacketType = 0x01 - obj.Method = KMSG_SECURITYQUOTES + obj := new(GetSecurityQuotes) + obj.reqHeader = new(ReqHeader) + obj.respHeader = new(RespHeader) + obj.request = new(GetSecurityQuotesRequest) + obj.reply = new(GetSecurityQuotesReply) + + obj.reqHeader.Zip = 0x0c + obj.reqHeader.SeqID = seqID() + obj.reqHeader.PacketType = 0x01 + obj.reqHeader.Method = KMSG_SECURITYQUOTES obj.contentHex = "0500000000000000" return obj } -func (obj *GetSecurityQuotes) SetParams(stocks []Stock) { - obj.stocks = stocks + +func (obj *GetSecurityQuotes) SetParams(req *GetSecurityQuotesRequest) { + obj.request = req } func (obj *GetSecurityQuotes) Serialize() ([]byte, error) { - obj.PkgLen1 = 2 + uint16(len(obj.stocks)*7) + 10 - obj.PkgLen2 = 2 + uint16(len(obj.stocks)*7) + 10 + obj.reqHeader.PkgLen1 = 2 + uint16(len(obj.request.StockList)*7) + 10 + obj.reqHeader.PkgLen2 = 2 + uint16(len(obj.request.StockList)*7) + 10 buf := new(bytes.Buffer) - err := binary.Write(buf, binary.LittleEndian, obj.ReqHeader) + err := binary.Write(buf, binary.LittleEndian, obj.reqHeader) b, err := hex.DecodeString(obj.contentHex) buf.Write(b) - err = binary.Write(buf, binary.LittleEndian, uint16(len(obj.stocks))) + err = binary.Write(buf, binary.LittleEndian, uint16(len(obj.request.StockList))) - for _, stock := range obj.stocks { + for _, stock := range obj.request.StockList { //code, _ := hex.DecodeString(stock.Code) //code := []byte{} code := make([]byte, 6) @@ -115,18 +125,20 @@ func (obj *GetSecurityQuotes) Serialize() ([]byte, error) { tmp = append(tmp, code...) buf.Write(tmp) } + return buf.Bytes(), err } func (obj *GetSecurityQuotes) UnSerialize(header interface{}, data []byte) error { - obj.Reply = new(GetSecurityQuotesReply) + obj.respHeader = header.(*RespHeader) + //fmt.Println(hex.EncodeToString(data)) pos := 0 pos += 2 // 跳过两个字节 - binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &obj.Reply.Count) + binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &obj.reply.Count) pos += 2 - for index := uint16(0); index < obj.Reply.Count; index++ { + for index := uint16(0); index < obj.reply.Count; index++ { ele := SecurityQuote{} binary.Read(bytes.NewBuffer(data[pos:pos+1]), binary.LittleEndian, &ele.Market) pos += 1 @@ -209,12 +221,15 @@ func (obj *GetSecurityQuotes) UnSerialize(header interface{}, data []byte) error binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &ele.Active2) pos += 2 - obj.Reply.List = append(obj.Reply.List, ele) + obj.reply.List = append(obj.reply.List, ele) } - //obj.Reply.Count = binary.LittleEndian.Uint16(data[:2]) return nil } +func (obj *GetSecurityQuotes) Reply() *GetSecurityQuotesReply { + return obj.reply +} + func (obj *GetSecurityQuotes) getPrice(price int, diff int) float64 { return float64(price+diff) / 100.0 } diff --git a/proto/hello1.go b/proto/hello1.go index 849d30a..eebb37d 100644 --- a/proto/hello1.go +++ b/proto/hello1.go @@ -7,23 +7,34 @@ import ( "gotdx/utils" ) -// Hello1 创建握手消息1 type Hello1 struct { - ReqHeader + reqHeader *ReqHeader + respHeader *RespHeader + request *Hello1Request + reply *Hello1Reply + contentHex string - Reply *Hello1Reply } + +type Hello1Request struct { +} + type Hello1Reply struct { Info string serverTime string } func NewHello1() *Hello1 { - obj := &Hello1{} - obj.Zip = 0x0c - obj.SeqID = seqID() - obj.PacketType = 0x01 - obj.Method = KMSG_CMD1 + obj := new(Hello1) + obj.reqHeader = new(ReqHeader) + obj.respHeader = new(RespHeader) + obj.request = new(Hello1Request) + obj.reply = new(Hello1Reply) + + obj.reqHeader.Zip = 0x0c + obj.reqHeader.SeqID = seqID() + obj.reqHeader.PacketType = 0x01 + obj.reqHeader.Method = KMSG_CMD1 obj.contentHex = "01" return obj } @@ -31,11 +42,11 @@ func NewHello1() *Hello1 { func (obj *Hello1) Serialize() ([]byte, error) { b, err := hex.DecodeString(obj.contentHex) - obj.PkgLen1 = 2 + uint16(len(b)) - obj.PkgLen2 = 2 + uint16(len(b)) + obj.reqHeader.PkgLen1 = 2 + uint16(len(b)) + obj.reqHeader.PkgLen2 = 2 + uint16(len(b)) buf := new(bytes.Buffer) - err = binary.Write(buf, binary.LittleEndian, obj.ReqHeader) + err = binary.Write(buf, binary.LittleEndian, obj.reqHeader) buf.Write(b) return buf.Bytes(), err @@ -46,8 +57,14 @@ func (obj *Hello1) Serialize() ([]byte, error) { 分 时 秒 日期 */ func (obj *Hello1) UnSerialize(header interface{}, data []byte) error { - obj.Reply = new(Hello1Reply) + obj.respHeader = header.(*RespHeader) + serverInfo := utils.Utf8ToGbk(data[68:]) - obj.Reply.Info = serverInfo + + obj.reply.Info = serverInfo return nil } + +func (obj *Hello1) Reply() *Hello1Reply { + return obj.reply +} diff --git a/proto/hello2.go b/proto/hello2.go index b09e69d..3243e28 100644 --- a/proto/hello2.go +++ b/proto/hello2.go @@ -7,23 +7,34 @@ import ( "gotdx/utils" ) -// Hello2 创建握手消息2 type Hello2 struct { - ReqHeader + reqHeader *ReqHeader + respHeader *RespHeader + request *Hello2Request + reply *Hello2Reply + contentHex string - Reply *Hello2Reply } + +type Hello2Request struct { +} + type Hello2Reply struct { Info string serverTime string } func NewHello2() *Hello2 { - obj := &Hello2{} - obj.Zip = 0x0c - obj.SeqID = seqID() - obj.PacketType = 0x01 - obj.Method = KMSG_CMD2 + obj := new(Hello2) + obj.reqHeader = new(ReqHeader) + obj.respHeader = new(RespHeader) + obj.request = new(Hello2Request) + obj.reply = new(Hello2Reply) + + obj.reqHeader.Zip = 0x0c + obj.reqHeader.SeqID = seqID() + obj.reqHeader.PacketType = 0x01 + obj.reqHeader.Method = KMSG_CMD2 obj.contentHex = "d5d0c9ccd6a4a8af0000008fc22540130000d500c9ccbdf0d7ea00000002" return obj } @@ -31,11 +42,11 @@ func NewHello2() *Hello2 { func (obj *Hello2) Serialize() ([]byte, error) { b, err := hex.DecodeString(obj.contentHex) - obj.PkgLen1 = 2 + uint16(len(b)) - obj.PkgLen2 = 2 + uint16(len(b)) + obj.reqHeader.PkgLen1 = 2 + uint16(len(b)) + obj.reqHeader.PkgLen2 = 2 + uint16(len(b)) buf := new(bytes.Buffer) - err = binary.Write(buf, binary.LittleEndian, obj.ReqHeader) + err = binary.Write(buf, binary.LittleEndian, obj.reqHeader) buf.Write(b) return buf.Bytes(), err } @@ -44,9 +55,14 @@ func (obj *Hello2) Serialize() ([]byte, error) { 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011f85e34068747470733a2f2f626967352e6e65776f6e652e636f6d2e636e2f7a797968742f7a645f7a737a712e7a6970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004150503a414c4c0d0a54494d453a303a30312d31353a30352c31353a30362d32333a35390d0a20202020c4facab9d3c3b5c4b0e6b1bebcb4bdabcda3d3c3a3acceaac1cbc4fab5c4d5fdb3a3cab9d3c32cc7ebbea1bfecc9fdd6c1d5d0c9ccd6a4c8af5043b0e6a1a30d0a20202020c8e7b9fbb2bbc4dcd7d4b6afc9fdbcb6a3acc7ebb5bdb9d9cdf868747470733a2f2f7777772e636d736368696e612e636f6d2fcfc2d4d8b0b2d7b0a3acd0bbd0bbc4fab5c4d6a7b3d6a3a100 年月日 年月日 */ func (obj *Hello2) UnSerialize(header interface{}, data []byte) error { - obj.Reply = new(Hello2Reply) + obj.respHeader = header.(*RespHeader) + serverInfo := utils.Utf8ToGbk(data[58:]) //fmt.Println(hex.EncodeToString(data)) - obj.Reply.Info = serverInfo + obj.reply.Info = serverInfo return nil } + +func (obj *Hello2) Reply() *Hello2Reply { + return obj.reply +} diff --git a/proto/proto.go b/proto/proto.go index e835684..0ced4ed 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -27,6 +27,7 @@ const ( KMSG_HISTORYMINUTETIMEDATE = 0x0fb4 // 历史分时信息 KMSG_HISTORYTRANSACTIONDATA = 0x0fb5 // 历史分笔成交信息 KMSG_INDEXBARS = 0x052d // 指数K线 + KMSG_SECURITYBARS = 0x052d // 股票K线 KMSG_MINUTETIMEDATA = 0x0537 // 分时数据 KMSG_SECURITYLIST = 0x0450 // 证券列表 KMSG_SECURITYQUOTES = 0x053e // 行情信息