统一分时成交的结构名称为Trade,和K线Kline一样简洁

This commit is contained in:
injoyai
2025-06-09 16:13:34 +08:00
parent d840e0d33d
commit e58130b3c3
7 changed files with 288 additions and 283 deletions

View File

@@ -0,0 +1,70 @@
package protocol
import (
"errors"
"github.com/injoyai/conv"
"time"
)
// HistoryTradeResp 历史分时交易比实时少了单量
type HistoryTradeResp struct {
Count uint16
List Trades
}
type historyTrade struct{}
func (historyTrade) Frame(date, code string, start, count uint16) (*Frame, error) {
exchange, number, err := DecodeCode(code)
if err != nil {
return nil, err
}
dataBs := Bytes(conv.Uint32(date)) //req.Time.Format("20060102"))
dataBs = append(dataBs, exchange.Uint8(), 0x0)
dataBs = append(dataBs, []byte(number)...)
dataBs = append(dataBs, Bytes(start)...)
dataBs = append(dataBs, Bytes(count)...)
return &Frame{
Control: Control01,
Type: TypeHistoryMinuteTrade,
Data: dataBs,
}, nil
}
func (historyTrade) Decode(bs []byte, c TradeCache) (*HistoryTradeResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
_, number, err := DecodeCode(c.Code)
if err != nil {
return nil, err
}
resp := &HistoryTradeResp{
Count: Uint16(bs[:2]),
}
//第2-6字节不知道是啥
bs = bs[2+4:]
lastPrice := Price(0)
for i := uint16(0); i < resp.Count; i++ {
timeStr := GetHourMinute([2]byte(bs[:2]))
t, err := time.Parse("2006010215:04", c.Date+timeStr)
if err != nil {
return nil, err
}
mt := &Trade{Time: t}
var sub Price
bs, sub = GetPrice(bs[2:])
lastPrice += sub * 10 //把分转成厘
mt.Price = lastPrice / basePrice(number)
bs, mt.Volume = CutInt(bs)
bs, mt.Status = CutInt(bs)
bs, _ = CutInt(bs) //这个得到的是0不知道是啥
resp.List = append(resp.List, mt)
}
return resp, nil
}