forked from hurisheng/go-futu-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqot_stockfilter.go
294 lines (263 loc) · 8.4 KB
/
qot_stockfilter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package futuapi
import (
"context"
"github.com/headshot289/go-futu-api/pb/qotcommon"
"github.com/headshot289/go-futu-api/pb/qotstockfilter"
"github.com/headshot289/go-futu-api/protocol"
)
const (
ProtoIDQotStockFilter = 3215 //Qot_StockFilter 获取条件选股
)
// 条件选股
func (api *FutuAPI) GetStockFilter(ctx context.Context, market qotcommon.QotMarket, begin int32, num int32, filter *StockFilter) (*StockFilterResult, error) {
req := qotstockfilter.Request{
C2S: &qotstockfilter.C2S{
Begin: &begin,
Num: &num,
Market: (*int32)(&market),
},
}
if filter != nil {
req.C2S.Plate = filter.Plate.pb()
req.C2S.BaseFilterList = baseFilterList(filter.BaseFilterList).pb()
req.C2S.AccumulateFilterList = accumulateFilterList(filter.AccumulateFilterList).pb()
req.C2S.FinancialFilterList = financialFilterList(filter.FinancialFilterList).pb()
}
ch := make(qotstockfilter.ResponseChan)
if err := api.get(ProtoIDQotStockFilter, &req, ch); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ErrInterrupted
case resp, ok := <-ch:
if !ok {
return nil, ErrChannelClosed
}
return stockFilterResultFromPB(resp.GetS2C()), protocol.Error(resp)
}
}
type StockFilter struct {
Plate *Security // 板块
BaseFilterList []*BaseFilter // 简单指标过滤器
AccumulateFilterList []*AccumulateFilter // 累积指标过滤器
FinancialFilterList []*FinancialFilter // 财务指标过滤器
}
// 简单属性筛选
type BaseFilter struct {
FieldName qotstockfilter.StockField // StockField 简单属性
FilterMin *FilterDouble // 区间下限(闭区间),不传代表下限为 -∞
FilterMax *FilterDouble // 区间上限(闭区间),不传代表上限为 +∞
IsNoFilter bool // 该字段是否不需要筛选,True:不筛选,False:筛选。不传默认不筛选
SortDir qotstockfilter.SortDir // SortDir 排序方向,默认不排序。
}
func (f *BaseFilter) pb() *qotstockfilter.BaseFilter {
if f == nil {
return nil
}
return &qotstockfilter.BaseFilter{
FieldName: (*int32)(&f.FieldName),
FilterMin: f.FilterMin.pb(),
FilterMax: f.FilterMax.pb(),
IsNoFilter: &f.IsNoFilter,
SortDir: (*int32)(&f.SortDir),
}
}
type baseFilterList []*BaseFilter
func (f baseFilterList) pb() []*qotstockfilter.BaseFilter {
if f == nil {
return nil
}
list := make([]*qotstockfilter.BaseFilter, len(f))
for i, v := range f {
list[i] = v.pb()
}
return list
}
// 累积属性筛选
type AccumulateFilter struct {
FieldName qotstockfilter.AccumulateField // AccumulateField 累积属性
FilterMin *FilterDouble // 区间下限(闭区间),不传代表下限为 -∞
FilterMax *FilterDouble // 区间上限(闭区间),不传代表上限为 +∞
IsNoFilter bool // 该字段是否不需要筛选,True:不筛选,False:筛选。不传默认不筛选
SortDir qotstockfilter.SortDir // SortDir 排序方向,默认不排序。
Days int32 // 近几日,累积时间
}
func (f *AccumulateFilter) pb() *qotstockfilter.AccumulateFilter {
if f == nil {
return nil
}
return &qotstockfilter.AccumulateFilter{
FieldName: (*int32)(&f.FieldName),
FilterMin: f.FilterMin.pb(),
FilterMax: f.FilterMax.pb(),
IsNoFilter: &f.IsNoFilter,
SortDir: (*int32)(&f.SortDir),
Days: &f.Days,
}
}
type accumulateFilterList []*AccumulateFilter
func (f accumulateFilterList) pb() []*qotstockfilter.AccumulateFilter {
if f == nil {
return nil
}
list := make([]*qotstockfilter.AccumulateFilter, len(f))
for i, v := range f {
list[i] = v.pb()
}
return list
}
// 财务属性筛选
type FinancialFilter struct {
FiledName qotstockfilter.FinancialField // FinancialField 财务属性
FilterMin *FilterDouble // 区间下限(闭区间),不传代表下限为 -∞
FilterMax *FilterDouble // 区间上限(闭区间),不传代表上限为 +∞
IsNoFilter bool // 该字段是否不需要筛选,True:不筛选,False:筛选。不传默认不筛选
SortDir qotstockfilter.SortDir // SortDir 排序方向,默认不排序。
Quarter qotstockfilter.FinancialQuarter // FinancialQuarter 财报累积时间
}
func (f *FinancialFilter) pb() *qotstockfilter.FinancialFilter {
if f == nil {
return nil
}
return &qotstockfilter.FinancialFilter{
FieldName: (*int32)(&f.FiledName),
FilterMin: f.FilterMin.pb(),
FilterMax: f.FilterMax.pb(),
IsNoFilter: &f.IsNoFilter,
SortDir: (*int32)(&f.SortDir),
Quarter: (*int32)(&f.Quarter),
}
}
type financialFilterList []*FinancialFilter
func (f financialFilterList) pb() []*qotstockfilter.FinancialFilter {
if f == nil {
return nil
}
list := make([]*qotstockfilter.FinancialFilter, len(f))
for i, v := range f {
list[i] = v.pb()
}
return list
}
type StockFilterResult struct {
LastPage bool // 是否最后一页了,false:非最后一页,还有窝轮记录未返回; true:已是最后一页
AllCount int32 // 该条件请求所有数据的个数
DataList []*StockData // 返回的股票数据列表
}
func stockFilterResultFromPB(pb *qotstockfilter.S2C) *StockFilterResult {
if pb == nil {
return nil
}
return &StockFilterResult{
LastPage: pb.GetLastPage(),
AllCount: pb.GetAllCount(),
DataList: stockDataListFromPB(pb.GetDataList()),
}
}
// 返回的股票数据
type StockData struct {
Security *Security // 股票
Name string // 股票名称
BaseDataList []*BaseData // 筛选后的简单指标属性数据
AccumulateDataList []*AccumulateData // 筛选后的累积指标属性数据
FinancialDataList []*FinancialData // 筛选后的财务指标属性数据
}
func stockDataFromPB(pb *qotstockfilter.StockData) *StockData {
if pb == nil {
return nil
}
return &StockData{
Security: securityFromPB(pb.GetSecurity()),
Name: pb.GetName(),
BaseDataList: baseDataListFromPB(pb.GetBaseDataList()),
AccumulateDataList: accumulateDataListFromPB(pb.GetAccumulateDataList()),
FinancialDataList: financialDataListFromPB(pb.GetFinancialDataList()),
}
}
func stockDataListFromPB(pb []*qotstockfilter.StockData) []*StockData {
if pb == nil {
return nil
}
list := make([]*StockData, len(pb))
for i, v := range pb {
list[i] = stockDataFromPB(v)
}
return list
}
// 简单属性数据
type BaseData struct {
FieldName qotstockfilter.StockField // StockField 简单属性
Value float64
}
func baseDataFromPB(pb *qotstockfilter.BaseData) *BaseData {
if pb == nil {
return nil
}
return &BaseData{
FieldName: qotstockfilter.StockField(pb.GetFieldName()),
Value: pb.GetValue(),
}
}
func baseDataListFromPB(pb []*qotstockfilter.BaseData) []*BaseData {
if pb == nil {
return nil
}
list := make([]*BaseData, len(pb))
for i, v := range pb {
list[i] = baseDataFromPB(v)
}
return list
}
// 累积属性数据
type AccumulateData struct {
FieldName qotstockfilter.AccumulateField // AccumulateField 累积属性
Value float64
Days int32 // 近几日,累积时间
}
func accumulateDataFromPB(pb *qotstockfilter.AccumulateData) *AccumulateData {
if pb == nil {
return nil
}
return &AccumulateData{
FieldName: qotstockfilter.AccumulateField(pb.GetFieldName()),
Value: pb.GetValue(),
Days: pb.GetDays(),
}
}
func accumulateDataListFromPB(pb []*qotstockfilter.AccumulateData) []*AccumulateData {
if pb == nil {
return nil
}
list := make([]*AccumulateData, len(pb))
for i, v := range pb {
list[i] = accumulateDataFromPB(v)
}
return list
}
// 财务属性数据
type FinancialData struct {
FieldName qotstockfilter.FinancialField // FinancialField 财务属性
Value float64
Quarter qotstockfilter.FinancialQuarter // FinancialQuarter 财报累积时间
}
func financialDataFromPB(pb *qotstockfilter.FinancialData) *FinancialData {
if pb == nil {
return nil
}
return &FinancialData{
FieldName: qotstockfilter.FinancialField(pb.GetFieldName()),
Value: pb.GetValue(),
Quarter: qotstockfilter.FinancialQuarter(pb.GetQuarter()),
}
}
func financialDataListFromPB(pb []*qotstockfilter.FinancialData) []*FinancialData {
if pb == nil {
return nil
}
list := make([]*FinancialData, len(pb))
for i, v := range pb {
list[i] = financialDataFromPB(v)
}
return list
}