-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_wss.cpp
More file actions
205 lines (146 loc) · 5.66 KB
/
main_wss.cpp
File metadata and controls
205 lines (146 loc) · 5.66 KB
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
#define NOMINMAX // do not define min/max macro
#include <ta_libc.h>
#include "src/consolidate.h"
#include "src/wsclient.h"
#include "src/wsclient_ssl.h"
#include "core/data_controller.h"
#include "core/account_controller.h"
#include "algos/ma3_ema9.h"
#include "sim/simulator.h" // TODO not directly include this?
using namespace agpred;
// TODO
const real_t INITIAL_BALANCE = 100000.0;
const real_t MAX_LOSS_PER_TRADE = 175.0;
const real_t MAX_LOSS_DAILY = 350;
// TODO abstract this...
MA3EMA9Algo algo_ma_above("ma3_ema9", false);
MA3EMA9Algo algo_ma_below("ma3_ema9", true);
MA3EMA9Entry entry_ma("ma3_ema9 entry", 1, algo_ma_above);
MA3EMA9Exit exit_ma("ma3_ema9 exit", algo_ma_below);
//StopLossExit exit_stop_loss("stop_loss exit");
const std::array<AlgoBase2<1>* const, 2> algos({ &algo_ma_above, &algo_ma_below });
const std::array<EntryBase<1>* const, 1> entries({ &entry_ma });
const std::array<ExitBase* const, 2> exits({ &exit_ma });
using Ctrl = AccountController<algos.size(), entries.size(), exits.size(), 1>;
client c;
void on_timer(client* c, websocketpp::lib::error_code const& ec) {
std::cout << "> ON TIMER " << ec << std::endl;
// repeat ...
c->set_timer(250, bind(&on_timer, c, ::_1));
}
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
printf("Ctrl-C event\n\n");
Beep(750, 300);
c.stop();
return TRUE;
// CTRL-CLOSE: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
Beep(600, 200);
printf("Ctrl-Close event\n\n");
return TRUE;
// Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
Beep(900, 200);
printf("Ctrl-Break event\n\n");
c.stop();
return FALSE;
case CTRL_LOGOFF_EVENT:
Beep(1000, 200);
printf("Ctrl-Logoff event\n\n");
c.stop();
return FALSE;
case CTRL_SHUTDOWN_EVENT:
Beep(750, 500);
printf("Ctrl-Shutdown event\n\n");
c.stop();
return FALSE;
default:
return FALSE;
}
}
int main(int argc, char* argv[])
{
if (SetConsoleCtrlHandler(CtrlHandler, TRUE))
std::cout << "Signal Handler registered..." << std::endl;
else
std::cout << "ERROR Unable to register Signal Handler." << std::endl;
TA_RetCode retCode = TA_Initialize();
if (retCode != TA_SUCCESS)
{
std::cout << "TA-Lib initialize error: " << retCode << std::endl;
return retCode;
}
std::cout << "TA-Lib initialized.\n";
//// print numbers with 9 digit precision
//std::cout.precision(9);
std::string hostname = "*.polygon.io";
std::string uri = "wss://socket.polygon.io/stocks";
// TODO use lambda and call simulator and account in onSnapshot
Simulator simulator({
.account_balance = INITIAL_BALANCE,
.max_trade_loss = MAX_LOSS_PER_TRADE,
.max_daily_loss = MAX_LOSS_DAILY
});
const AGMode mode = AGMode::LIVE_TEST;
Ctrl account(simulator, mode, algos, entries, exits);
DataController ctrl(
mode,
[AccountPtr = &account, SimPtr = &simulator](const Symbol& symbol, const Snapshot& snapshot)
{
AccountPtr->onSnapshot(symbol, snapshot);
},
[AccountPtr = &account](const Symbol& symbol, const Snapshot& snapshot, const xtensor_ts_interval& data_ts, const xtensor_raw& data, const xtensor_processed& data_processed, const quotes_queue& quotes, const trades_queue& trades)
{
AccountPtr->onUpdate(symbol, snapshot, data_ts, data, data_processed, quotes, trades);
}
);
const Symbol& symbol = Symbol::get_symbol("AAPL");
ctrl.initSymbol(symbol);
std::cout << "Symbol: " << std::string(symbol.symbol) << std::endl;
try {
// Set logging to be pretty verbose (everything except message payloads)
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
c.clear_access_channels(websocketpp::log::alevel::frame_header);
c.clear_access_channels(websocketpp::log::alevel::control);
c.set_error_channels(websocketpp::log::elevel::all);
// Initialize ASIO
c.init_asio();
// Register handlers
c.set_message_handler(bind(&on_message, &c, &ctrl, ::_1, ::_2));
c.set_open_handler(bind(&on_conn_open, &c, ::_1));
c.set_close_handler(bind(&on_conn_close, &c, ::_1));
c.set_tls_init_handler(bind(&on_tls_init, hostname.c_str(), ::_1));
//c.set_timer(250, bind(&on_timer, &c, ::_1));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return 1;
}
// Note that connect here only requests a connection. No network messages are
// exchanged until the event loop starts running in the next line.
c.connect(con);
c.get_alog().write(websocketpp::log::alevel::app, "Connecting to " + uri);
// Start the ASIO io_service run loop
// this will cause a single connection to be made to the server. c.run() will exit when this connection is closed.
c.run();
// TODO properly close connection?
//con->close()
}
catch (websocketpp::exception const& e) {
std::cout << e.what() << std::endl;
}
retCode = TA_Shutdown();
if (retCode != TA_SUCCESS)
{
std::cout << "TA-Lib shutdown error: " << retCode << std::endl;
return retCode;
}
return 0;
}