mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加了获取k线图,简单的对了下日k线,成交量暂时不知道是否解析成功,其它的数据对的上
This commit is contained in:
@@ -2,6 +2,7 @@ package protocol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/injoyai/base/g"
|
||||
"time"
|
||||
)
|
||||
@@ -9,19 +10,18 @@ import (
|
||||
type StockKlineReq struct {
|
||||
Exchange Exchange
|
||||
Code string
|
||||
Type uint16 //类型 1分 5分 等等
|
||||
Start uint16
|
||||
Count uint16
|
||||
}
|
||||
|
||||
func (this *StockKlineReq) Bytes() (g.Bytes, error) {
|
||||
func (this *StockKlineReq) Bytes(Type TypeKline) (g.Bytes, error) {
|
||||
if len(this.Code) != 6 {
|
||||
return nil, errors.New("股票代码长度错误")
|
||||
}
|
||||
data := []byte{this.Exchange.Uint8(), 0x0}
|
||||
data = append(data, Bytes(this.Code)...)
|
||||
data = append(data, Bytes(this.Type)...)
|
||||
data = append(data, 0x0, 0x0)
|
||||
data = append(data, []byte(this.Code)...) //这里怎么是正序了?
|
||||
data = append(data, Bytes(Type.Uint16())...)
|
||||
data = append(data, 0x01, 0x0)
|
||||
data = append(data, Bytes(this.Start)...)
|
||||
data = append(data, Bytes(this.Count)...)
|
||||
data = append(data, make([]byte, 10)...) //未知啥含义
|
||||
@@ -34,24 +34,27 @@ type StockKlineResp struct {
|
||||
}
|
||||
|
||||
type StockKline struct {
|
||||
Open Price
|
||||
High Price
|
||||
Low Price
|
||||
Close Price
|
||||
Volume int //成交量
|
||||
Number int //成交数
|
||||
Time time.Time
|
||||
Year int
|
||||
Month int
|
||||
Day int
|
||||
Hour int
|
||||
Minute int
|
||||
Open Price //开盘价
|
||||
High Price //最高价
|
||||
Low Price //最低价
|
||||
Close Price //收盘价
|
||||
Volume float64 //成交量
|
||||
Amount float64 //成交额
|
||||
Time time.Time //时间
|
||||
}
|
||||
|
||||
func (this *StockKline) String() string {
|
||||
return fmt.Sprintf("%s 开盘价:%s 最高价:%s 最低价:%s 收盘价:%s 成交量:%s 成交额:%s",
|
||||
this.Time.Format("2006-01-02 15:04:05"),
|
||||
this.Open, this.High, this.Low, this.Close,
|
||||
FloatUnitString(this.Volume), FloatUnitString(this.Amount),
|
||||
)
|
||||
}
|
||||
|
||||
type stockKline struct{}
|
||||
|
||||
func (stockKline) Frame(req *StockKlineReq) (*Frame, error) {
|
||||
bs, err := req.Bytes()
|
||||
func (stockKline) Frame(Type TypeKline, req *StockKlineReq) (*Frame, error) {
|
||||
bs, err := req.Bytes(Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -62,7 +65,7 @@ func (stockKline) Frame(req *StockKlineReq) (*Frame, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (stockKline) Decode(bs []byte) (*StockKlineResp, error) {
|
||||
func (stockKline) Decode(bs []byte, Type TypeKline) (*StockKlineResp, error) {
|
||||
|
||||
if len(bs) < 2 {
|
||||
return nil, errors.New("数据长度不足")
|
||||
@@ -72,9 +75,39 @@ func (stockKline) Decode(bs []byte) (*StockKlineResp, error) {
|
||||
Count: Uint16(bs[:2]),
|
||||
}
|
||||
|
||||
for i := uint16(0); i < resp.Count; i++ {
|
||||
bs = bs[2:]
|
||||
|
||||
var last Price
|
||||
|
||||
for i := uint16(0); i < resp.Count; i++ {
|
||||
k := &StockKline{
|
||||
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.Open = (open + last) / 10
|
||||
k.Close = (open + last + _close) / 10
|
||||
k.High = (open + last + high) / 10
|
||||
k.Low = (open + last + low) / 10
|
||||
|
||||
last = last + open + _close
|
||||
|
||||
//logs.Debug(Reverse(bs[:4]), getVolume(Uint32(bs[:4])))
|
||||
//logs.Debug(Reverse(bs[4:8]), getVolume(Uint32(bs[4:8])))
|
||||
k.Volume = getVolume(Uint32(bs[:4])) / 100
|
||||
k.Amount = getVolume(Uint32(bs[4:8]))
|
||||
|
||||
bs = bs[8:]
|
||||
resp.List = append(resp.List, k)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user