-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
executable file
·351 lines (291 loc) · 10.2 KB
/
main.cpp
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
#include "Md.hpp"
#include "Td.hpp"
#include <queue>
#include <string>
#include <map>
#include <vector>
#include <stdlib.h>
#include <thread>
#include <functional>
#include "market_info.h"
using namespace std::placeholders;
///////////////////////////////////////////////////////////////////////////////////////
// SECTION event engine
///////////////////////////////////////////////////////////////////////////////////////
enum class EventType {
TARGET_POSITION_EVENT,
TICK_EVENT,
BAR_EVENT,
};
template<class EventDataType>
class Event {
public:
EventType type;
EventDataType data;
Event(EventType type_, EventDataType data_): type(type_), data(data_) { };
};
template<class EventDataType>
class EventEngine {
private:
std::thread run_thread;
std::queue<Event<EventDataType> > q;
std::map<EventType, std::vector<std::function<void(EventDataType*)> > > process_func_map; //void(*)(EventDataType*)
public:
EventEngine() {};
//~EventEngine() {};
void register_process_function(EventType type, std::function<void(EventDataType*)> pfunc) { //void (*pfunc)(EventDataType*)
process_func_map[type].push_back(pfunc);
};
void add_event(EventType type, EventDataType data) {
auto event = Event<EventDataType>(type, data);
q.push(event);
};
void run() {
while (true) {
if (q.empty()) {
usleep(500e3);
printf("(EventEngine.run) q is empty, sleep 100ms.\n"); // ANCHOR DEBUG
} else {
auto event = q.front();
for (auto pfunc: process_func_map[event.type]) {
pfunc(&event.data);
}
q.pop();
printf(" (EventEngine.run) pop one.\n"); // ANCHOR DEBUG
}
}
};
void start() {
run_thread = std::thread(&EventEngine<EventDataType>::run, this);
};
void join() {
run_thread.join();
}
};
///////////////////////////////////////////////////////////////////////////////////////
// SECTION DataSource
///////////////////////////////////////////////////////////////////////////////////////
class MarketData { };
class Tick : public MarketData {
public:
std::string InstrumentID;
std::string ExchangeID;
std::string UpdateTime;
int UpdateMillisec;
double LastPrice;
double BidPrice1;
double AskPrice1;
int BidVolume1;
int AskVolume1;
Tick(CThostFtdcDepthMarketDataField *pDepthMarketData)
: InstrumentID(pDepthMarketData->InstrumentID)
, UpdateTime(pDepthMarketData->UpdateTime)
, UpdateMillisec(pDepthMarketData->UpdateMillisec)
, LastPrice(pDepthMarketData->LastPrice)
, BidPrice1(pDepthMarketData->BidPrice1)
, AskPrice1(pDepthMarketData->AskPrice1)
, BidVolume1(pDepthMarketData->BidVolume1)
, AskVolume1(pDepthMarketData->AskVolume1)
{
ExchangeID = get_exchange_id(InstrumentID);
};
void print() {
printf(
"\033[1;31m<Tick> %s %d [%s.%s] %.2f --- %.2f ---- %.2f \033[0m\n",
UpdateTime.c_str(), UpdateMillisec,
InstrumentID.c_str(), ExchangeID.c_str(),
AskPrice1, LastPrice, BidPrice1
);
}
};
class Bar : public MarketData {
public:
std::string InstrumentID;
std::string UpdateTime;
double Open;
double High;
double Low;
double Close;
int Volume;
int OpenInterest;
};
class DataSource : public MdHandler /* try inherit more data source eg.wind, webcrul */ {
public:
EventEngine<Tick>* p_eengine_market_data;
public:
DataSource() {};
~DataSource();
void start();
void set_engine(EventEngine<Tick>* engine) { p_eengine_market_data = engine; };
void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData);
};
void DataSource::start() {
connect();
login();
subscribe();
}
void DataSource::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) {
Tick* tick = new Tick(pDepthMarketData);
p_eengine_market_data->add_event(EventType::TICK_EVENT, *tick);
tick->print();
};
///////////////////////////////////////////////////////////////////////////////////////
// SECTION Strategy && Algo
///////////////////////////////////////////////////////////////////////////////////////
class TargetPosition {
public:
std::string InstrumentID;
std::string ExchangeID;
int TargetPos;
TargetPosition(std::string instrument_id, std::string exchange_id, int pos)
: InstrumentID(instrument_id)
, ExchangeID(exchange_id)
, TargetPos(pos)
{};
};
class Algo : public TraderHandler {
public:
std::map<std::string /* instrument_id */, int /* current pos */> current_position = {};
std::map<std::string /* instrument_id */, Tick*> current_price = {};
public:
//Algo() { };
// ~Algo();
void process_target_position(TargetPosition* pos);
void update_current_price(Tick* tick);
double get_current_price(std::string instrument_id);
//void OnRtnOrder(CThostFtdcOrderField *pOrder);
void OnRtnTrade(CThostFtdcTradeField *pTrade);
};
void Algo::process_target_position(TargetPosition* pos) {
std::string instrument_id = pos->InstrumentID;
std::string exchange_id = pos->ExchangeID;
int old_pos = current_position[instrument_id]; // in `current_position` pos is **singned** integer
int pos_increment = pos->TargetPos - old_pos;
char direction;
double price;
if (pos_increment > 0) {
direction = 'B';
} else if (pos_increment < 0) {
direction = 'S';
} else {
cancel_all(instrument_id, exchange_id);
return;
}
try {
price = get_current_price(instrument_id); // TODO ask_1, bid_1, tmp: last_price.
} catch (const std::out_of_range& e) {
// ANCHOR
printf("Algo::get_current_price out_of_range error. instrument_id = %s.\n", instrument_id.c_str());
return;
}
cancel_all(instrument_id, exchange_id);
unsigned int volume_to_close = (old_pos * pos_increment >= 0) ? 0 : std::max(abs(old_pos), abs(pos_increment));
unsigned int volume_to_open = (old_pos * pos_increment >= 0) ? abs(pos_increment) : std::max(0, abs(pos_increment) - abs(old_pos));
if (volume_to_close) {
if (exchange_id == "SHFE") {
// TODO
} else {
char offset = 'C';
order_insert(instrument_id, exchange_id, direction, offset, price, volume_to_close);
}
}
if (volume_to_open) {
char offset = 'O';
order_insert(instrument_id, exchange_id, direction, offset, price, volume_to_open);
}
};
void Algo::update_current_price(Tick* tick) {
std::string instrument_id = std::string(tick->InstrumentID);
current_price[instrument_id] = tick;
};
double Algo::get_current_price(std::string instrument_id) {
return current_price.at(instrument_id)->LastPrice;
};
void Algo::OnRtnTrade(CThostFtdcTradeField *pTrade) {
TraderHandler::OnRtnTrade(pTrade);
std::string instrument_id = std::string(pTrade->InstrumentID);
int sign = 0;
switch (pTrade->Direction)
{
case THOST_FTDC_D_Buy: sign = 1; break;
case THOST_FTDC_D_Sell: sign = 2; break;
default: break;
}
current_position[instrument_id] += sign * pTrade->Volume;
};
class Strategy {
private:
EventEngine<TargetPosition>* p_engine;
protected:
void target(std::string instrument_id, std::string exchange_id, int pos); // function that set target_postion
// TODO context obj that can bind anything to it
public:
Strategy() {};
~Strategy();
void set_engine(EventEngine<TargetPosition>* engine_) { p_engine = engine_; };
virtual void init() = 0;
virtual void on_tick(Tick* tick) = 0;
virtual void on_bar(Bar* Bar) = 0;
};
void Strategy::target(std::string instrument_id, std::string exchange_id, int pos) {
TargetPosition* target_pos = new TargetPosition(instrument_id, exchange_id, pos);
p_engine->add_event(EventType::TARGET_POSITION_EVENT, *target_pos);
};
///////////////////////////////////////////////////////////////////////////////////////
class RandomStrategy: public Strategy {
public:
RandomStrategy() {};
void init() {
};
void on_bar(Bar* bar) {
};
void on_tick(Tick* tick) {
int pos = rand() % 9 - 4; // [-4, 4]
target(tick->InstrumentID, tick->ExchangeID, pos);
};
};
///////////////////////////////////////////////////////////////////////////////////////
// SECTION event engine
///////////////////////////////////////////////////////////////////////////////////////
int main() {
DataSource* data_source = new DataSource();
Algo* algo = new Algo();
RandomStrategy* random_strategy = new RandomStrategy();
// 2 Event Engine
EventEngine<Tick>* eengine_market_data = new EventEngine<Tick>();
eengine_market_data->register_process_function(
EventType::TICK_EVENT,
// NOTE: figure out why these work / not work ?
//random_strategy->on_tick
//std::bind(Strategy::on_tick, random_strategy, _1);
[random_strategy] (Tick* tick) { random_strategy->on_tick(tick); }
);
eengine_market_data->register_process_function(
EventType::TICK_EVENT,
//algo->update_current_price
//std::bind(Algo::update_current_price, algo, _1);
[algo] (Tick* tick) { algo->update_current_price(tick); }
);
EventEngine<TargetPosition>* eengine_target_position = new EventEngine<TargetPosition>();
eengine_target_position->register_process_function(
EventType::TARGET_POSITION_EVENT,
//algo->process_target_position
// std::bind(Algo::process_target_position, algo, _1)
[algo] (TargetPosition* pos) { algo->process_target_position(pos); }
);
data_source->set_engine(eengine_market_data);
random_strategy->set_engine(eengine_target_position);
data_source->connect();
data_source->login();
data_source->subscribe();
algo->connect();
algo->login();
algo->settlement_info_confirm();
algo->qry_order();
printf("--------------- 1\n");
eengine_market_data->start();
eengine_target_position->start();
printf("--------------- 2\n");
eengine_market_data->join();
eengine_target_position->join();
}