# 策略 Demo3:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <kungfu/yijinjing/common.h>
#include <kungfu/yijinjing/log/setup.h>
#include <kungfu/wingchun/strategy/context.h>
#include <kungfu/wingchun/strategy/strategy.h>
#include <thread_pool.h>
namespace py = pybind11;
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
using namespace kungfu;
using namespace kungfu::wingchun;
using namespace kungfu::wingchun::strategy;
std::string info_bar(const wingchun::msg::data::Bar& bar) {
nlohmann::json bar_json;
wingchun::msg::data::to_json(bar_json, bar);
return safe_dump(bar_json);
}
std::string info_quote(const wingchun::msg::data::Quote& quote) {
nlohmann::json quote_json;
wingchun::msg::data::to_json(quote_json, quote);
return safe_dump(quote_json);
}
void today_bar_callback(Context_ptr context, const std::map<std::string, std::vector<wingchun::msg::data::Bar>>& bar_map) {
SPDLOG_INFO("{} called", __func__);
for (const auto& item : bar_map) {
SPDLOG_INFO("code: {}", item.first);
for (const wingchun::msg::data::Bar& bar : item.second) {
SPDLOG_INFO("code: {}, open: {}, close: {}", bar.code, bar.open, bar.close);
}
}
}
class DemoStrategy : public StrategyWithBook
{
public:
DemoStrategy(yijinjing::data::location_ptr home):StrategyWithBook(home), count_(0)
{
yijinjing::log::copy_log_settings(home, home->name);
};
// bar 分钟级别行情回调
void on_bar(Context_ptr context, const wingchun::msg::data::Bar &bar) override
{
SPDLOG_INFO("{} called, data: {}", __func__, info_bar(bar));
}
void on_indicator(Context_ptr context, const std::string& type, const nlohmann::json& indicator) override {
SPDLOG_INFO("{} called, type: {}, data: {}", __func__, type, safe_dump(indicator));
}
// 启动前
void pre_start(Context_ptr context) override
{
SPDLOG_INFO("pre run strategy");
StrategyWithBook::pre_start(context);
}
// 启动后
void post_start(Context_ptr context) override
{
SPDLOG_INFO("cpp demo post start");
test(context);
}
std::string info_bar(const wingchun::msg::data::Bar& bar) {
std::ostringstream ostr;
ostr << "Bar data, trading_day: " << bar.trading_day
<< ", source_id: " << bar.source_id
<< ", instrument_id: " << bar.instrument_id
<< ", exchange_id: " << bar.exchange_id
<< ", code: " << bar.code
<< ", type: " << bar.type
<< ", start_time: " << bar.start_time
<< ", end_time: " << bar.end_time
<< ", time_interval: " << bar.time_interval
<< ", open: " << bar.open
<< ", close: " << bar.close
<< ", low: " << bar.low
<< ", high: " << bar.high
<< ", volume: " << bar.volume
<< ", start_volume: " << bar.start_volume
<< ", turnover: " << bar.turnover
<< ", start_turnover: " << bar.start_turnover
<< ", period: " << bar.period;
return ostr.str();
}
std::string info_quote(const wingchun::msg::data::Quote& quote) {
std::ostringstream ostr;
ostr << "Quote data, source_id: " << quote.source_id
<< ", trading_day: " << quote.trading_day
<< ", data_time: " << quote.data_time
<< ", instrument_id: " << quote.instrument_id
<< ", exchange_id: " << quote.exchange_id
<< ", instrument_type: " << int(quote.instrument_type)
<< ", kungfu_time_flag: " << quote.kungfu_time_flag
<< ", pre_close_price: " << quote.pre_close_price
<< ", pre_settlement_price: " << quote.pre_settlement_price
<< ", last_price: " << quote.last_price
<< ", volume: " << quote.volume
<< ", turnover: " << quote.turnover
<< ", pre_open_interest: " << quote.pre_open_interest
<< ", open_interest: " << quote.open_interest
<< ", open_price: " << quote.open_price
<< ", high_price: " << quote.high_price
<< ", low_price: " << quote.low_price
<< ", upper_limit_price: " << quote.upper_limit_price
<< ", lower_limit_price: " << quote.lower_limit_price
<< ", close_price: " << quote.close_price
<< ", settlement_price: " << quote.settlement_price
<< ", iopv: " << quote.iopv
<< ", pre_iopv: " << quote.pre_iopv
<< ", ticker_status: " << quote.ticker_status
<< ", total_bid_volume: " << quote.total_bid_volume
<< ", total_ask_volume: " << quote.total_ask_volume
<< ", ma_bid_price: " << quote.ma_bid_price
<< ", ma_ask_price: " << quote.ma_ask_price
<< ", bid_price: ";
for (double price : quote.bid_price) {
ostr << price << ",";
}
ostr << " ask_price: ";
for (double price : quote.ask_price) {
ostr << price << ",";
}
ostr << " bid_volume: ";
for (int64_t volume : quote.bid_volume) {
ostr << volume << ",";
}
ostr << " ask_volume: ";
for (int64_t volume : quote.ask_volume) {
ostr << volume << ",";
}
// return ostr.str().pop_back();
return ostr.str();
}
void test_query_bar_today(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
std::vector<std::string> codes = {"600000.SSE", "000002.SZ"};
std::map<std::string, std::vector<wingchun::msg::data::Bar>> today_bar = context->query_bar_today(codes, "5m");
for (auto& kv : today_bar) {
for (auto& bar : kv.second) {
SPDLOG_INFO("{}", info_bar(bar));
}
}
SPDLOG_INFO("{} end", __func__);
}
void today_bar_callback(Context_ptr context, const std::map<std::string, std::vector<wingchun::msg::data::Bar>>& bar_map, uint64_t query_id) {
SPDLOG_INFO("{} called, query id: {}", __func__, query_id);
for (const auto& item : bar_map) {
SPDLOG_INFO("code: {}", item.first);
for (const wingchun::msg::data::Bar& bar : item.second) {
SPDLOG_INFO("{}", info_bar(bar));
}
}
}
void test_query_bar_today_async(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
std::vector<std::string> codes = {"600000.SSE", "000002.SZ"};
context->query_bar_today_async(codes, std::bind(&DemoStrategy::today_bar_callback, this, _1, _2, _3), "2m", 51);
// context->query_bar_today_async(codes, today_bar_callback, "2m");
}
void test_query_bar(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
nlohmann::json params = {
{"code", "000001.SZ"},
{"start_date", "2013-01-04"},
{"end_date", "2014-01-04"},
{"period", "1d"},
{"adjust_type", "pre"}
};
std::vector<wingchun::msg::data::Bar> bars = context->query_bar(params);
for (const auto bar : bars) {
SPDLOG_INFO("{}", info_bar(bar));
}
}
void query_bar_callback(Context_ptr context, const std::vector<wingchun::msg::data::Bar>& bars, uint64_t query_id) {
SPDLOG_INFO("{} called, query_id: {}", __func__, query_id);
for (const auto bar : bars) {
SPDLOG_INFO("receive bar: {}", info_bar(bar));
}
}
void test_query_bar_async(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
nlohmann::json params = {
{"code", "000001.SZ"},
{"start_date", "2013-01-04"},
{"end_date", "2014-01-04"},
{"period", "1d"},
{"adjust_type", "pre"}
};
context->query_bar_async(params, std::bind(&DemoStrategy::query_bar_callback, this, _1, _2, _3), 52);
}
void test_query_market_data(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
nlohmann::json params = {
{"code", "000001.SZ"},
{"start_date", "2013-01-04"},
{"end_date", "2014-01-04"}
};
std::vector<wingchun::msg::data::Quote> quotes = context->query_market_data(params);
for (const auto quote : quotes) {
SPDLOG_INFO("quote data: {}", info_quote(quote));
}
}
void query_market_data_callback(Context_ptr context, const std::vector<wingchun::msg::data::Quote>& quotes, uint64_t query_id) {
SPDLOG_INFO("{} called, query id: {}", __func__, query_id);
for (const auto quote : quotes) {
SPDLOG_INFO("{}", info_quote(quote));
}
}
void test_query_market_data_async(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
nlohmann::json params = {
{"code", "000001.SZ"},
{"start_date", "2014-01-04"},
{"end_date", "2014-01-08"}
};
context->query_market_data_async(params, std::bind(&DemoStrategy::query_market_data_callback, this, _1, _2, _3), 53);
}
void test_query_data(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
std::string method = "bar";
nlohmann::json params = {
{"code", "000001.SZ"},
{"start_date", "2014-01-04"},
{"end_date", "2014-02-04"},
{"period", "1d"},
{"adjust_type", "pre"}
};
nlohmann::json result = context->query_data(method, params);
int code = result["code"];
std::string msg = result["msg"].get<std::string>();
SPDLOG_INFO("query data, code: {}, msg: {}", code, msg);
if (code == 200) {
for (const auto& bar : result["data"]) {
SPDLOG_INFO("Bar data: {}", safe_dump(bar));
}
}
SPDLOG_INFO("{} end", __func__);
}
void query_data_callback(Context_ptr context, nlohmann::json result, uint64_t query_id) {
SPDLOG_INFO("{} called", __func__);
int code = result["code"];
std::string msg = result["msg"].get<std::string>();
SPDLOG_INFO("query data, code: {}, msg: {}", code, msg);
if (code == 200) {
for (const auto& data : result["data"]) {
SPDLOG_INFO("data: {}", safe_dump(data));
}
}
}
void test_query_data_async(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
std::string method = "market_data";
nlohmann::json params = {
{"code", "000001.SZ"},
{"start_date", "2013-01-04"},
{"end_date", "2014-01-04"}
};
context->query_data_async(method, params, std::bind(&DemoStrategy::query_data_callback, this, _1, _2, _3), 54);
SPDLOG_INFO("{} end", __func__);
}
void test_query_data_page(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
std::string method = "bar";
int page_size = 100;
int current_page = 1;
nlohmann::json params = {
{"code", "000001.SZ"},
{"start_date", "2014-01-04"},
{"end_date", "2014-02-04"},
{"period", "1d"},
{"adjust_type", "pre"},
{"page_size", 100},
{"current_page", 1}
};
wingchun::msg::data::DataPageInfo data_page_info = context->query_data_page(method, params);
SPDLOG_INFO("{}, currentPage: {}, totalPage: {}, pageSize: {}, totalCount: {}", __func__, data_page_info.currentPage, data_page_info.totalPage, data_page_info.pageSize, data_page_info.totalCount);
int code = data_page_info.data["code"];
std::string msg = data_page_info.data["msg"].get<std::string>();
SPDLOG_INFO("code: {}, msg: {}", code, msg);
for (const auto& bar : data_page_info.data["data"]) {
SPDLOG_INFO("{}, bar data: {}", __func__, safe_dump(bar));
}
SPDLOG_INFO("{} end", __func__);
}
void query_data_page_callback(Context_ptr context, const DataPageInfo& data_page_info, uint64_t query_id) {
SPDLOG_INFO("{}, query id: {}, currentPage: {}, totalPage: {}, pageSize: {}, totalCount: {}", __func__, query_id, data_page_info.currentPage, data_page_info.totalPage, data_page_info.pageSize, data_page_info.totalCount);
int code = data_page_info.data["code"];
std::string msg = data_page_info.data["msg"].get<std::string>();
SPDLOG_INFO("code: {}, msg: {}", code, msg);
for (const auto& bar : data_page_info.data["data"]) {
SPDLOG_INFO("{}, bar data: {}", __func__, safe_dump(bar));
}
}
void test_query_data_page_async(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
std::string method = "bar";
int page_size = 100;
int current_page = 1;
nlohmann::json params = {
{"code", "000001.SZ"},
{"start_date", "2014-01-04"},
{"end_date", "2014-02-04"},
{"period", "1d"},
{"adjust_type", "pre"},
{"page_size", 100},
{"current_page", 1}
};
context->query_data_page_async(method, params, std::bind(&DemoStrategy::query_data_page_callback, this, _1, _2,_3), 55);
SPDLOG_INFO("{} end", __func__);
}
void indicator_callback(Context_ptr context, const std::string& indicator_type, const nlohmann::json& indicator) {
SPDLOG_INFO("indicator type: {}, data: {}", indicator_type, safe_dump(indicator));
}
void test_sub_indicator(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
std::vector<std::string> codes = {"512160.SSE", "159631.SZ"};
std::vector<std::string> failed_codes;
failed_codes = context->subscribe_indicator("etf", codes, std::bind(&DemoStrategy::indicator_callback, this, _1, _2, _3));
// 或者使用如下订阅方式, indicator 通过 on_indicator 接收
// failed_codes = context->subscribe_indicator("etf", codes);
SPDLOG_INFO("failed codes: {}", safe_dump(nlohmann::json(failed_codes)));
SPDLOG_INFO("{} end", __func__);
}
void bar_callback(Context_ptr context, const wingchun::msg::data::Bar &bar)
{
SPDLOG_INFO("{} called, data: {}", __func__, info_bar(bar));
}
void test_sub_bar(Context_ptr context) {
SPDLOG_INFO("{} start", __func__);
std::vector<std::string> codes = {"000002.SZ", "600004.SSE"};
std::vector<std::string> failed_codes;
failed_codes = context->subscribe_bar(codes, "2m", std::bind(&DemoStrategy::bar_callback, this, _1, _2));
// 或者使用如下方式订阅, Bar 通过 on_bar 接收
// failed_codes = context->subscribe_bar(codes, "2m", std::bind(&DemoStrategy::bar_callback, this, _1, _2));
SPDLOG_INFO("failed codes: {}", safe_dump(nlohmann::json(failed_codes)));
SPDLOG_INFO("{} end", __func__);
}
void test(Context_ptr context) {
// test_query_bar_today(context);
// test_query_bar_today_async(context);
// test_query_bar(context);
// test_query_bar_async(context);
// test_query_market_data(context);
// test_query_market_data_async(context);
// test_query_data(context);
// test_query_data_async(context);
// test_query_data_page(context);
// test_query_data_page_async(context);
test_sub_indicator(context);
test_sub_bar(context);
}
// 获取编译策略的kungfu版本号,本函数不允许修改。
virtual const std::string getCompileVersion() const override
{
std::ostringstream ver;
#ifdef ALPHAX_VERSION
ver<<ALPHAX_VERSION;
#else
ver<<DEFAULT_COMPILE_VERSION;
#endif
SPDLOG_INFO("Version: {}.", ver.str());
return ver.str();
}
private:
// 静态行情整个交易日都不会变,启动策略时取到所有证券的静态数据后,需要时直接使用
// 而不需要用一次查一次,性能很差(因为需要读写文件)
// add by zz on 20210112
std::unordered_map<std::string, QuoteStaticFullInfo> quotes;
uint64_t count_;
};
PYBIND11_MODULE(PY_MODULE, m)
{
py::class_<DemoStrategy, Strategy, std::shared_ptr<DemoStrategy>>(m, "Strategy")
.def(py::init<yijinjing::data::location_ptr>())
.def("pre_start", &DemoStrategy::pre_start)
.def("post_start", &DemoStrategy::post_start)
.def("pre_stop", &DemoStrategy::pre_stop)
.def("post_stop", &DemoStrategy::post_stop)
.def("on_trading_day", &DemoStrategy::on_trading_day)
.def("on_quote", &DemoStrategy::on_quote)
.def("on_bar", &DemoStrategy::on_bar)
.def("on_entrust", &DemoStrategy::on_entrust)
.def("on_transaction", &DemoStrategy::on_transaction)
.def("on_order", &DemoStrategy::on_order)
.def("on_order_action_error", &DemoStrategy::on_order_action_error)
.def("on_trade", &DemoStrategy::on_trade)
.def("on_order_book", &DemoStrategy::on_order_book)
.def("on_cancelorder_feedback", &DemoStrategy::on_cancelorder_feedback)
.def("on_client_msg", &DemoStrategy::on_client_msg)
.def("on_query_bondIPO_info", &DemoStrategy::on_query_bondIPO_info)
.def("on_query_bondIPO_end", &DemoStrategy::on_query_bondIPO_end)
.def("on_query_bondswapstock_info", &DemoStrategy::on_query_bondswapstock_info)
.def("on_query_bondswapstock_end", &DemoStrategy::on_query_bondswapstock_end)
.def("on_query_IPO_quota_info", &DemoStrategy::on_query_IPO_quota_info)
.def("on_query_IPO_quota_info_end", &DemoStrategy::on_query_IPO_quota_info_end)
.def("on_query_IPO_info_list", &DemoStrategy::on_query_IPO_info_list)
.def("on_query_IPO_info_list_end", &DemoStrategy::on_query_IPO_info_list_end)
.def("getCompileVersion", &DemoStrategy::getCompileVersion);
}
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
备注:“策略 Demo3”,从 2.1.2 版本开始支持
← C++ 策略 2