From 8e7135eabb9d438c80d13c3fd768d468eff372f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=B1=E7=BA=AF=E5=87=80?= <1113655791@qq.com> Date: Mon, 17 Feb 2025 00:27:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=86=E5=A4=87=E5=A2=9E=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=8C=87=E6=95=B0=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 18 +++++++++ example/GetIndex/main.go | 21 ++++++++++ protocol/const.go | 1 + protocol/model_connect.go | 1 + protocol/model_index.go | 81 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 example/GetIndex/main.go create mode 100644 protocol/model_index.go diff --git a/client.go b/client.go index 3f25d7f..5dc647a 100644 --- a/client.go +++ b/client.go @@ -501,3 +501,21 @@ func (this *Client) GetKlineYearAll(code string) (*protocol.KlineResp, error) { func (this *Client) GetKlineYearUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) { return this.GetKlineUntil(protocol.TypeKlineYear, code, f) } + +/* + + + */ + +func (this *Client) GetIndex(Type uint8, code string, start, count uint16) (*protocol.IndexResp, error) { + //f, err := protocol.MIndex.Frame(Type, code, start, count) + //if err != nil { + // return nil, err + //} + f := protocol.MIndex.Frame() + result, err := this.SendFrame(f, Type) + if err != nil { + return nil, err + } + return result.(*protocol.IndexResp), nil +} diff --git a/example/GetIndex/main.go b/example/GetIndex/main.go new file mode 100644 index 0000000..8b7e002 --- /dev/null +++ b/example/GetIndex/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/injoyai/logs" + "github.com/injoyai/tdx" + "github.com/injoyai/tdx/example/common" + "github.com/injoyai/tdx/protocol" +) + +func main() { + common.Test(func(c *tdx.Client) { + resp, err := c.GetIndex(protocol.TypeKlineDay, "sh000001", 0, 100) + logs.PanicErr(err) + + for _, v := range resp.List { + logs.Debug(v) + } + + logs.Debug("总数:", resp.Count) + }) +} diff --git a/protocol/const.go b/protocol/const.go index 40bdc72..b2ae375 100644 --- a/protocol/const.go +++ b/protocol/const.go @@ -10,6 +10,7 @@ const ( TypeMinuteTrade = 0x0FC5 //分时交易 TypeHistoryMinuteTrade = 0x0FB5 //历史分时交易 TypeKline = 0x052D //K线图 + TypeIndex = 0x052D //指数K线 ) /* diff --git a/protocol/model_connect.go b/protocol/model_connect.go index 4a7a10e..c7133d9 100644 --- a/protocol/model_connect.go +++ b/protocol/model_connect.go @@ -14,6 +14,7 @@ var ( MMinuteTrade = minuteTrade{} MHistoryMinuteTrade = historyMinuteTrade{} MKline = kline{} + MIndex = index{} ) type ConnectResp struct { diff --git a/protocol/model_index.go b/protocol/model_index.go new file mode 100644 index 0000000..ebb40e5 --- /dev/null +++ b/protocol/model_index.go @@ -0,0 +1,81 @@ +package protocol + +import ( + "errors" + "time" +) + +type IndexResp struct { + Count uint16 + List []*IndexKline +} + +type IndexKline struct { + Last Price //昨日收盘价,这个是列表的上一条数据的收盘价,如果没有上条数据,那么这个值为0 + Open Price //开盘价 + High Price //最高价 + Low Price //最低价 + Close Price //收盘价,如果是当天,则是最新价/实时价 + Volume int64 //成交量 + Amount Price //成交额 + Time time.Time //时间 + UpCount uint16 // + DownCount uint16 // +} + +type index struct{} + +func (index) Frame() *Frame { + return &Frame{ + Control: Control01, + Type: TypeIndex, + Data: make([]byte, 10), + } +} + +func (index) Decode(bs []byte, Type uint8) (*IndexResp, error) { + + if len(bs) < 2 { + return nil, errors.New("数据长度不足") + } + + resp := &IndexResp{ + Count: Uint16(bs[:2]), + } + + bs = bs[2:] + + var last Price //上条数据(昨天)的收盘价 + for i := uint16(0); i < resp.Count; i++ { + k := &IndexKline{ + Time: GetTime([4]byte(bs[:4]), Type), + } + + var open Price + bs, open = GetPrice(bs[4:]) + var _close Price + bs, _close = GetPrice(bs) + var high Price + bs, high = GetPrice(bs) + var low Price + bs, low = GetPrice(bs) + + k.Last = last / 10 + k.Open = (open + last) / 10 + k.Close = (last + open + _close) / 10 + k.High = (open + last + high) / 10 + k.Low = (open + last + low) / 10 + last = last + open + _close + + k.Volume = int64(getVolume(Uint32(bs[:4]))) + switch Type { + case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKlineHour, TypeKlineDay2: + k.Volume /= 100 + } + k.Amount = Price(getVolume(Uint32(bs[4:8])) * 100) //从元转为分,并去除多余的小数 + + resp.List = append(resp.List, k) + } + + return resp, nil +}