diff --git a/client.go b/client.go index 3f25d7f..3d035a5 100644 --- a/client.go +++ b/client.go @@ -130,7 +130,7 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) { resp, err = protocol.MHistoryMinuteTrade.Decode(f.Data, conv.String(val)) case protocol.TypeKline: - resp, err = protocol.MKline.Decode(f.Data, conv.Uint8(val)) + resp, err = protocol.MKline.Decode(f.Data, val.(protocol.KlineCache)) default: err = fmt.Errorf("通讯类型未解析:0x%X", f.Type) @@ -298,7 +298,7 @@ func (this *Client) GetKline(Type uint8, code string, start, count uint16) (*pro if err != nil { return nil, err } - result, err := this.SendFrame(f, Type) + result, err := this.SendFrame(f, protocol.KlineCache{Type: Type, Code: code}) if err != nil { return nil, err } diff --git a/example/GetKlineDay/main.go b/example/GetKlineDay/main.go index 448926e..e93a440 100644 --- a/example/GetKlineDay/main.go +++ b/example/GetKlineDay/main.go @@ -8,7 +8,7 @@ import ( func main() { common.Test(func(c *tdx.Client) { - resp, err := c.GetKlineDay("sz000001", 0, 800) + resp, err := c.GetKlineDay("sh000001", 0, 10) logs.PanicErr(err) for _, v := range resp.List { diff --git a/protocol/model_kline.go b/protocol/model_kline.go index 92667c7..b44262c 100644 --- a/protocol/model_kline.go +++ b/protocol/model_kline.go @@ -106,7 +106,7 @@ func (kline) Frame(Type uint8, code string, start, count uint16) (*Frame, error) }, nil } -func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) { +func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) { if len(bs) < 2 { return nil, errors.New("数据长度不足") @@ -117,12 +117,10 @@ func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) { } bs = bs[2:] - //logs.Debug(len(bs)) //264 10 237 9 - var last Price //上条数据(昨天)的收盘价 for i := uint16(0); i < resp.Count; i++ { k := &Kline{ - Time: GetTime([4]byte(bs[:4]), Type), + Time: GetTime([4]byte(bs[:4]), c.Type), } var open Price @@ -157,15 +155,15 @@ func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) { */ k.Volume = int64(getVolume(Uint32(bs[:4]))) bs = bs[4:] - switch Type { + switch c.Type { case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKlineHour, TypeKlineDay2: k.Volume /= 100 } k.Amount = Price(getVolume(Uint32(bs[:4])) * 100) //从元转为分,并去除多余的小数 bs = bs[4:] - //指数和股票的差别bs[12:] - if false { + //指数和股票的差别,指数多解析4字节 + if !IsStock(c.Code) { k.UpCount = conv.Int([]byte{bs[1], bs[0]}) k.DownCount = conv.Int([]byte{bs[3], bs[2]}) bs = bs[4:] @@ -176,3 +174,8 @@ func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) { return resp, nil } + +type KlineCache struct { + Type uint8 + Code string +} diff --git a/protocol/unit.go b/protocol/unit.go index 54094fe..b121c7f 100644 --- a/protocol/unit.go +++ b/protocol/unit.go @@ -194,3 +194,20 @@ func getVolume(val uint32) (volume float64) { volume = dbl_xmm6 + dbl_xmm4 + dbl_xmm3 + dbl_xmm1 return } + +func IsStock(code string) bool { + if len(code) != 8 { + return false + } + code = strings.ToLower(code) + switch { + case code[0:2] == ExchangeSH.String() && + (code[2:3] == "6"): + return true + + case code[0:2] == ExchangeSZ.String() && + (code[2:3] == "0" || code[2:4] == "30"): + return true + } + return false +}