This repository has been archived by the owner on Jun 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Decision.cpp
224 lines (193 loc) · 6.89 KB
/
Decision.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
#include "Headers/Decision.hpp"
int main(int argc, char *argv[]) {
Decision d = Decision(false, "EUR_USD_M1", "EUR_USD_H1", .0007, .0049);
if (argc > 1 && strcmp(argv[1], "-t") == 0) {
Decision d = Decision(true, "EUR_USD_M1", "EUR_USD_H1", .0007, .0049);
}
d.decide();
return EXIT_SUCCESS;
}
Decision::Decision(bool isTesting, std::string table, std::string longerTable) :
exec(),
back(6000.0, table, 50.0),
testing(isTesting),
table(table),
longerTable(longerTable) {
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "");
takeProfit = 10;
stopLoss = 10;
}
Decision::Decision(bool isTesting, std::string table, std::string longerTable, double takeProfit, double stopLoss) :
back(6000.0, table, 50.0),
testing(isTesting),
stopLoss(stopLoss),
exec(),
takeProfit(takeProfit),
table(table),
longerTable(longerTable) {
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "");
}
double Decision::decide() {
Decision::Position pos = HOLD;
Price p = Price(0, 0, 0);
double amount = 0;
if (testing) {
amount = testDecide(p, pos);
}else {
realityRun();
std::cout << "Heyo" << std::endl;
}
delete con;
return amount;
}
Decision::Position Decision::evalMACD() {
if (results[0] >= 0 && results[1] < 0) { // && MACD[1] < 0){
return BUY;
}else if (results[0] < 0 && results[1] >= 0) {
return SELL;
}else {
return HOLD;
}
}
Decision::Position Decision::evalEMA() {
//bool alreadySignaled = false;
double diff = EMA12[9] - EMA26[9];
if (((EMA12[8] - EMA26[8] < 0 && diff < 0) || (EMA12[8] - EMA26[8] > 0 && diff > 0))) {
return HOLD;
}else if (EMA12[9] > EMA26[9]) {
return BUY;
}else {
return SELL;
}
}
void Decision::fillResults(int date, std::string col) {
sql::Statement *stmt = con->createStatement();
sql::ResultSet *res = stmt->executeQuery("SELECT " + col + " FROM " + DATABASE_NAME + ".MACD_" + longerTable + " WHERE date = " + std::to_string(date));
res->next();
if (col == "MACD") {
MACD[0] = MACD[1];
MACD[1] = res->getDouble(col);
}else {
results[0] = results[1];
results[1] = res->getDouble(col);
}
delete res;
delete stmt;
}
void Decision::fillLongPrices(int date) {
sql::Statement *stmt = con->createStatement();
sql::ResultSet *res = stmt->executeQuery("SELECT * FROM " + DATABASE_NAME + ".MACD_" + longerTable + " WHERE date = " + std::to_string(date));
res->next();
for (int i = 0; i < 9; i++) {
EMA12[i] = EMA12[i + 1];
}
EMA12[9] = res->getDouble("EMA12");
for (int i = 0; i < 9; i++) {
EMA26[i] = EMA26[i + 1];
}
EMA26[9] = res->getDouble("EMA26");
delete res;
delete stmt;
}
void closeAllReal() {
}
void Decision::realityRun() {
int lastHourly = 0;
Position pos = HOLD;
double bal = 0;
while (true) {
bal = exec.getBalance();
sql::Statement *stmt = con->createStatement();
sql::ResultSet *res = stmt->executeQuery("SELECT date, closeAsk, closeBid FROM " + DATABASE_NAME + "." + table + " ORDER BY DATE DESC LIMIT 1");
delete stmt;
stmt = con->createStatement();
res->next();
Price p = Price(res->getInt("date"), res->getDouble("closeAsk"), res->getDouble("closeBid"));
json j = exec.getTradesJson();
exec.trades = Trade::translateTrades(j);
for (int i = 0; i < exec.trades.size(); i++) {
if (exec.trades[i].profit <-1 *stopLoss *exec.trades[i].units *p.ask || exec.trades[i].profit> takeProfit * exec.trades[i].units * p.ask) {
if (exec.trades[i].type == SHORT) {
std::cerr << "ending short" << '\n';
exec.buy(exec.trades[i].units, "EUR_USD");
}else if (exec.trades[i].type == LONG) {
std::cout << "ending long" << '\n';
exec.sell(exec.trades[i].units, "EUR_USD");
}
}
}
delete res;
delete stmt;
stmt = con->createStatement();
res = stmt->executeQuery("SELECT date, closeAsk, closeBid FROM " + DATABASE_NAME + "." + longerTable + " ORDER BY DATE DESC LIMIT 1");
res->next();
if (res->getInt("date") != lastHourly) {
std::cout << "HERE" << '\n';
lastHourly = res->getInt("date");
pos = evalMACD();
switch (pos) {
case BUY:
exec.sell(bal * .01, "EUR_USD");
std::cout << "Selling " << bal * .01 << '\n';
break;
case SELL:
exec.buy(bal * .01, "EUR_USD");
std::cout << "Buying " << bal * .01 << '\n';
break;
case HOLD:
std::cout << "Holding " << bal * .01 << '\n';
break;
}
}
auto t = std::time(0);
std::cout << "Current time: " << t << '\n';
std::cout << "SLEEPING..." << '\n';
usleep(30000000); // 30 seconds
delete res;
delete stmt;
}
}
double Decision::testDecide(Price p, Position pos) {
sql::Statement *stmt = con->createStatement();
sql::ResultSet *res = stmt->executeQuery("SELECT date, closeAsk, closeBid FROM " + DATABASE_NAME + "." + table + " WHERE date > 1187902800");
int date = 0;
int counter = 0;
while (res->next() && counter < 1 * 365 * 1440) {
if (counter % 1440 == 0) {
std::cout << "day " << counter / 1440 << "\n";
}
date = res->getInt("date");
p = Price(res->getInt("date"), res->getDouble("closeAsk"), res->getDouble("closeBid"));
for (int i = 0; i < back.trades.size(); i++) {
if (back.trades[i].calcProfit(p) < -1 * stopLoss * back.trades[i].units * p.ask || back.trades[i].calcProfit(p) > takeProfit * back.trades[i].units * p.ask) {
back.closeTrade(p, i);
}
}
if (date % 3600 == 0) {
fillLongPrices(date);
fillResults(date, "result");
fillResults(date, "MACD");
//pos = evalEMA();
pos = evalMACD();
switch (pos) {
case BUY:
back.closeAll(p);
back.makeTrade(SHORT, (int)(0.01 * back.USD * 50), p);
break;
case SELL:
back.closeAll(p);
back.makeTrade(LONG, (int)(0.01 * back.USD * 50), p);
break;
case HOLD: break;
}
}
counter++;
}
back.closeAll(Price(res->getInt("date"), res->getDouble("closeAsk"), res->getDouble("closeBid")));
back.reportTrades();
delete res;
delete stmt;
return back.USD;
}