mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
简单兼容成功
This commit is contained in:
@@ -130,7 +130,7 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
|
|||||||
resp, err = protocol.MHistoryMinuteTrade.Decode(f.Data, conv.String(val))
|
resp, err = protocol.MHistoryMinuteTrade.Decode(f.Data, conv.String(val))
|
||||||
|
|
||||||
case protocol.TypeKline:
|
case protocol.TypeKline:
|
||||||
resp, err = protocol.MKline.Decode(f.Data, conv.Uint8(val))
|
resp, err = protocol.MKline.Decode(f.Data, val.(protocol.KlineCache))
|
||||||
|
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("通讯类型未解析:0x%X", f.Type)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
result, err := this.SendFrame(f, Type)
|
result, err := this.SendFrame(f, protocol.KlineCache{Type: Type, Code: code})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
common.Test(func(c *tdx.Client) {
|
common.Test(func(c *tdx.Client) {
|
||||||
resp, err := c.GetKlineDay("sz000001", 0, 800)
|
resp, err := c.GetKlineDay("sh000001", 0, 10)
|
||||||
logs.PanicErr(err)
|
logs.PanicErr(err)
|
||||||
|
|
||||||
for _, v := range resp.List {
|
for _, v := range resp.List {
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ func (kline) Frame(Type uint8, code string, start, count uint16) (*Frame, error)
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) {
|
func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
|
||||||
|
|
||||||
if len(bs) < 2 {
|
if len(bs) < 2 {
|
||||||
return nil, errors.New("数据长度不足")
|
return nil, errors.New("数据长度不足")
|
||||||
@@ -117,12 +117,10 @@ func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) {
|
|||||||
}
|
}
|
||||||
bs = bs[2:]
|
bs = bs[2:]
|
||||||
|
|
||||||
//logs.Debug(len(bs)) //264 10 237 9
|
|
||||||
|
|
||||||
var last Price //上条数据(昨天)的收盘价
|
var last Price //上条数据(昨天)的收盘价
|
||||||
for i := uint16(0); i < resp.Count; i++ {
|
for i := uint16(0); i < resp.Count; i++ {
|
||||||
k := &Kline{
|
k := &Kline{
|
||||||
Time: GetTime([4]byte(bs[:4]), Type),
|
Time: GetTime([4]byte(bs[:4]), c.Type),
|
||||||
}
|
}
|
||||||
|
|
||||||
var open Price
|
var open Price
|
||||||
@@ -157,15 +155,15 @@ func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) {
|
|||||||
*/
|
*/
|
||||||
k.Volume = int64(getVolume(Uint32(bs[:4])))
|
k.Volume = int64(getVolume(Uint32(bs[:4])))
|
||||||
bs = bs[4:]
|
bs = bs[4:]
|
||||||
switch Type {
|
switch c.Type {
|
||||||
case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKlineHour, TypeKlineDay2:
|
case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKlineHour, TypeKlineDay2:
|
||||||
k.Volume /= 100
|
k.Volume /= 100
|
||||||
}
|
}
|
||||||
k.Amount = Price(getVolume(Uint32(bs[:4])) * 100) //从元转为分,并去除多余的小数
|
k.Amount = Price(getVolume(Uint32(bs[:4])) * 100) //从元转为分,并去除多余的小数
|
||||||
bs = bs[4:]
|
bs = bs[4:]
|
||||||
|
|
||||||
//指数和股票的差别bs[12:]
|
//指数和股票的差别,指数多解析4字节
|
||||||
if false {
|
if !IsStock(c.Code) {
|
||||||
k.UpCount = conv.Int([]byte{bs[1], bs[0]})
|
k.UpCount = conv.Int([]byte{bs[1], bs[0]})
|
||||||
k.DownCount = conv.Int([]byte{bs[3], bs[2]})
|
k.DownCount = conv.Int([]byte{bs[3], bs[2]})
|
||||||
bs = bs[4:]
|
bs = bs[4:]
|
||||||
@@ -176,3 +174,8 @@ func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) {
|
|||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type KlineCache struct {
|
||||||
|
Type uint8
|
||||||
|
Code string
|
||||||
|
}
|
||||||
|
|||||||
@@ -194,3 +194,20 @@ func getVolume(val uint32) (volume float64) {
|
|||||||
volume = dbl_xmm6 + dbl_xmm4 + dbl_xmm3 + dbl_xmm1
|
volume = dbl_xmm6 + dbl_xmm4 + dbl_xmm3 + dbl_xmm1
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user