mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
准备增加获取指数函数
This commit is contained in:
18
client.go
18
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
|
||||
}
|
||||
|
||||
21
example/GetIndex/main.go
Normal file
21
example/GetIndex/main.go
Normal file
@@ -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)
|
||||
})
|
||||
}
|
||||
@@ -10,6 +10,7 @@ const (
|
||||
TypeMinuteTrade = 0x0FC5 //分时交易
|
||||
TypeHistoryMinuteTrade = 0x0FB5 //历史分时交易
|
||||
TypeKline = 0x052D //K线图
|
||||
TypeIndex = 0x052D //指数K线
|
||||
)
|
||||
|
||||
/*
|
||||
|
||||
@@ -14,6 +14,7 @@ var (
|
||||
MMinuteTrade = minuteTrade{}
|
||||
MHistoryMinuteTrade = historyMinuteTrade{}
|
||||
MKline = kline{}
|
||||
MIndex = index{}
|
||||
)
|
||||
|
||||
type ConnectResp struct {
|
||||
|
||||
81
protocol/model_index.go
Normal file
81
protocol/model_index.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user