forked from hurisheng/go-futu-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqot_requesthistorykl.go
77 lines (70 loc) · 1.99 KB
/
qot_requesthistorykl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package futuapi
import (
"context"
"github.com/headshot289/go-futu-api/pb/qotcommon"
"github.com/headshot289/go-futu-api/pb/qotrequesthistorykl"
"github.com/headshot289/go-futu-api/protocol"
)
const (
ProtoIDQotRequestHistoryKL = 3103 //Qot_RequestHistoryKL 在线获取单只股票一段历史 K 线
)
// 获取历史 K 线
func (api *FutuAPI) RequestHistoryKLine(ctx context.Context, security *Security, begin string, end string, klType qotcommon.KLType, rehabType qotcommon.RehabType,
maxNum int32, fields qotcommon.KLFields, nextKey []byte, extTime bool) (*HistoryKLine, error) {
// 请求参数
req := qotrequesthistorykl.Request{
C2S: &qotrequesthistorykl.C2S{
RehabType: (*int32)(&rehabType),
KlType: (*int32)(&klType),
Security: security.pb(),
BeginTime: &begin,
EndTime: &end,
NextReqKey: nextKey,
ExtendedTime: &extTime,
},
}
if maxNum != 0 {
req.C2S.MaxAckKLNum = &maxNum
}
if fields != 0 {
var klFields int64 = int64(fields)
req.C2S.NeedKLFieldsFlag = &klFields
}
// 发送请求,同步返回结果
ch := make(qotrequesthistorykl.ResponseChan)
if err := api.get(ProtoIDQotRequestHistoryKL, &req, ch); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ErrInterrupted
case resp, ok := <-ch:
if !ok {
return nil, ErrChannelClosed
}
return historyKLineFromPB(resp.GetS2C()), protocol.Error(resp)
}
}
type HistoryKLine struct {
Security *Security //证券
KLines []*KLine //K 线数据
NextKey []byte //分页请求 key。一次请求没有返回所有数据时,下次请求带上这个 key,会接着请求
}
func historyKLineFromPB(pb *qotrequesthistorykl.S2C) *HistoryKLine {
if pb == nil {
return nil
}
return &HistoryKLine{
Security: securityFromPB(pb.GetSecurity()),
KLines: kLineListFromPB(pb.GetKlList()),
NextKey: nextKeyFromPB(pb.GetNextReqKey()),
}
}
func nextKeyFromPB(pb []byte) []byte {
if pb == nil {
return nil
}
k := make([]byte, len(pb))
copy(k, pb)
return k
}