forked from shxhid01/TradingSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TradingBot.cpp
547 lines (476 loc) · 20.9 KB
/
TradingBot.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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include "TradingBot.h"
#include "TradingStrategy.h"
#include "RiskManagement.h"
#include "Market.h"
#include "BuyAndHold.h"
#include "MeanReversion.h"
#include "MovingAvg.h"
#include "RandomStrategy.h"
#include "TrendFollowing.h"
#include <QFile>
#include <QTextStream>
#include <QStandardPaths>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <QMessageBox>
#include <QDir>
#include <QtCharts/QtCharts>
#include <QApplication>
#include <QMainWindow>
#include <QtCharts>
class TradingBot::TradingBotImpl {
public:
TradingBotImpl(const QString &username) :
currentStrategy(nullptr),
balance(10000),
ownedStocks(0),
riskManager(new RiskManagement(0.02, 0.1)),
lastUpdateValue(10000),
lastUpdateDay(0),
initialTotalValue(10000),
buysSinceLastUpdate(0),
sellsSinceLastUpdate(0),
holdsSinceLastUpdate(0),
totalBuys(0),
totalSells(0),
totalHolds(0),
maxDrawdown(0),
totalReturn(0),
volatility(0),
sharpeRatio(0),
currentDay(0),
totalDays(0),
transactionFeePercentage(0.005),
slippageFactor(0.001),
username(username)
{
// Constructor body
strategies.push_back(new BuyAndHoldStrategy());
strategies.push_back(new MeanReversionStrategy());
strategies.push_back(new TrendFollowingStrategy());
strategies.push_back(new RandomStrategy());
strategies.push_back(new MovingAverageStrategy());
QString documentsPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
tradesFilePath = documentsPath + "/TradingSimulation/" + username + "_trades_taken.txt";
}
~TradingBotImpl() {
for (auto strategy : strategies) {
delete strategy;
}
delete riskManager;
saveState(); // Save state when the bot is destroyed
}
std::vector<TradingStrategy*> strategies;
TradingStrategy* currentStrategy;
double balance;
int ownedStocks;
std::vector<double> balanceHistory;
Market market;
RiskManagement* riskManager;
QString tradesFilePath;
std::string lastMarketEvent;
double lastUpdateValue;
int lastUpdateDay;
double initialTotalValue;
int buysSinceLastUpdate, sellsSinceLastUpdate, holdsSinceLastUpdate;
int totalBuys, totalSells, totalHolds;
int currentDay;
int totalDays;
double maxDrawdown;
double totalReturn;
double volatility;
double sharpeRatio;
const double transactionFeePercentage;
const double slippageFactor;
std::string generateMarketEvent() {
std::vector<std::string> events = {
"Major product announcement",
"Market downturn indicators",
"Bull market predictions",
"Uncertainty due to policy changes",
"Competitor's actions impacting stock"
};
return events[std::rand() % events.size()];
}
double applyTransactionFee(double amount) {
return amount * (1.0 - transactionFeePercentage);
}
double applySlippage(double currentPrice) {
double slippage = currentPrice * slippageFactor * ((std::rand() % 2 == 0) ? 1 : -1);
return currentPrice + slippage;
}
double calculateMaxDrawdown() {
double peak = balanceHistory[0];
double maxDrawdown = 0;
for (double value : balanceHistory) {
peak = std::max(peak, value);
double drawdown = (peak - value) / peak;
maxDrawdown = std::max(maxDrawdown, drawdown);
}
return maxDrawdown;
}
double calculateCumulativeReturn() {
double finalValue = balanceHistory.back();
return (finalValue - initialTotalValue) / initialTotalValue;
}
double calculateVolatility() {
std::vector<double> returns;
for (size_t i = 1; i < balanceHistory.size(); ++i) {
double dailyReturn = (balanceHistory[i] - balanceHistory[i - 1]) / balanceHistory[i - 1];
returns.push_back(dailyReturn);
}
double meanReturn = std::accumulate(returns.begin(), returns.end(), 0.0) / returns.size();
double variance = 0;
for (double r : returns) {
variance += std::pow(r - meanReturn, 2);
}
variance /= returns.size();
return std::sqrt(variance);
}
double calculateSharpeRatio(double riskFreeRate = 0.0) {
double meanReturn = calculateCumulativeReturn() / balanceHistory.size();
return (meanReturn - riskFreeRate) / volatility;
}
void logTrade(int day, double currentPrice, const std::string& tradeAction, double actionTaken) {
QFile file(tradesFilePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Append)) {
QTextStream stream(&file);
stream << "Day " << day << ": Price = $" << currentPrice
<< ", Action = " << QString::fromStdString(tradeAction) << " " << actionTaken
<< " stocks, Balance = $" << balance
<< ", Owned Stocks = " << ownedStocks << "\n";
file.close();
}
}
void saveState() {
QString filePath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) +
"/TradingSimulation/" + username + "_saved_state.txt";
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
// Save all relevant data
out << "Balance: " << balance << "\n";
out << "OwnedStocks: " << ownedStocks << "\n";
out << "TotalBuys: " << totalBuys << "\n";
out << "TotalSells: " << totalSells << "\n";
out << "CurrentDay: " << currentDay << "\n";
out << "MaxDrawdown: " << maxDrawdown << "\n";
out << "TotalReturn: " << totalReturn << "\n";
out << "Volatility: " << volatility << "\n";
out << "SharpeRatio: " << sharpeRatio << "\n";
out << "BuysSinceLastUpdate: " << buysSinceLastUpdate << "\n";
out << "SellsSinceLastUpdate: " << sellsSinceLastUpdate << "\n";
out << "HoldsSinceLastUpdate: " << holdsSinceLastUpdate << "\n";
file.close();
} else {
QMessageBox::warning(nullptr, "File Error", "Unable to save state.");
}
}
void loadState() {
QString filePath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) +
"/TradingSimulation/" + username + "_saved_state.txt";
QFile file(filePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
QString line;
while (in.readLineInto(&line)) {
QStringList parts = line.split(": ");
if (parts.size() == 2) {
if (parts[0] == "Balance") {
balance = parts[1].toDouble();
} else if (parts[0] == "OwnedStocks") {
ownedStocks = parts[1].toInt();
} else if (parts[0] == "TotalBuys") {
totalBuys = parts[1].toInt();
} else if (parts[0] == "TotalSells") {
totalSells = parts[1].toInt();
} else if (parts[0] == "CurrentDay") {
currentDay = parts[1].toInt();
} else if (parts[0] == "MaxDrawdown") {
maxDrawdown = parts[1].toDouble();
} else if (parts[0] == "TotalReturn") {
totalReturn = parts[1].toDouble();
} else if (parts[0] == "Volatility") {
volatility = parts[1].toDouble();
} else if (parts[0] == "SharpeRatio") {
sharpeRatio = parts[1].toDouble();
} else if (parts[0] == "BuysSinceLastUpdate") {
buysSinceLastUpdate = parts[1].toInt();
} else if (parts[0] == "SellsSinceLastUpdate") {
sellsSinceLastUpdate = parts[1].toInt();
} else if (parts[0] == "HoldsSinceLastUpdate") {
holdsSinceLastUpdate = parts[1].toInt();
}
}
}
file.close();
} else {
// Initialize to default values if the file doesn't exist
resetToInitialState();
}
}
void resetToInitialState() {
balance = 10000.0;
ownedStocks = 0;
totalReturn = 0.0;
maxDrawdown = 0.0;
volatility = 0.0;
sharpeRatio = 0.0;
lastUpdateValue = balance;
lastUpdateDay = 0;
buysSinceLastUpdate = 0;
sellsSinceLastUpdate = 0;
holdsSinceLastUpdate = 0;
totalBuys = 0;
totalSells = 0;
totalHolds = 0;
currentDay = 0;
balanceHistory.clear();
balanceHistory.push_back(balance);
stockPriceHistory.clear();
stockPriceHistory.push_back(market.getPrice());
}
QString username;
std::vector<double> stockPriceHistory;
};
// Constructor
TradingBot::TradingBot(const QString &username, QObject *parent)
: QObject(parent), pImpl(new TradingBotImpl(username)) {
pImpl->loadState();
pImpl->username = username;
QString documentsPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
QString tradingSimFolder = documentsPath + "/TradingSimulation";
QDir().mkpath(tradingSimFolder);
QString tradeFilePath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/tradingsimulation/"
+ username + "_trades_taken.txt";
tradeFile.open(tradeFilePath.toStdString(), std::ios::out | std::ios::app);
if (!tradeFile.is_open()) {
QMessageBox::warning(nullptr, "Error", "Couldn't open trades_taken.txt file. Please check file permissions.");
}
}
TradingBot::~TradingBot() {
if (tradeFile.is_open()) {
tradeFile.close();
}
delete pImpl;
}
double TradingBot::getCurrentPrice() const { return pImpl->market.getPrice(); }
double TradingBot::getTotalValue() const { return pImpl->balance + pImpl->ownedStocks * pImpl->market.getPrice(); }
const std::string& TradingBot::getLastMarketEvent() const { return pImpl->lastMarketEvent; }
double TradingBot::getBalance() const { return pImpl->balance; }
int TradingBot::getOwnedStocks() const { return pImpl->ownedStocks; }
double TradingBot::getTotalReturn() const { return pImpl->totalReturn; }
double TradingBot::getMaxDrawdown() const { return pImpl->maxDrawdown; }
double TradingBot::getVolatility() const { return pImpl->volatility; }
double TradingBot::getMarketVolatility() const { return pImpl->market.getVolatility(); }
double TradingBot::getSharpeRatio() const { return pImpl->sharpeRatio; }
std::string TradingBot::getCurrentStrategyName() const { return pImpl->currentStrategy ? pImpl->currentStrategy->getName() : "No strategy set"; }
double TradingBot::getProfitLossSinceStart() const { return getTotalValue() - pImpl->initialTotalValue; }
double TradingBot::getProfitLossSinceLastUpdate() const { return getTotalValue() - pImpl->lastUpdateValue; }
int TradingBot::getCurrentDay() const { return pImpl->currentDay; }
int TradingBot::getLastBuys() const { return pImpl->buysSinceLastUpdate; }
int TradingBot::getLastSells() const { return pImpl->sellsSinceLastUpdate; }
int TradingBot::getLastHolds() const { return pImpl->holdsSinceLastUpdate; }
int TradingBot::getTotalBuys() const { return pImpl->totalBuys; }
int TradingBot::getTotalSells() const { return pImpl->totalSells; }
int TradingBot::getTotalHolds() const { return pImpl->totalHolds; }
void TradingBot::setStrategy(int index) {
if (index >= 0 && index < pImpl->strategies.size()) {
pImpl->currentStrategy = pImpl->strategies[index];
}
}
void TradingBot::initializeSimulation(int days) {
pImpl->totalDays = days;
pImpl->currentDay = 0;
pImpl->initialTotalValue = pImpl->balance + pImpl->ownedStocks * pImpl->market.getPrice();
pImpl->balanceHistory.clear();
pImpl->balanceHistory.push_back(pImpl->initialTotalValue);
QFile file(pImpl->tradesFilePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
file.close();
}
pImpl->buysSinceLastUpdate = pImpl->sellsSinceLastUpdate = pImpl->holdsSinceLastUpdate = 0;
pImpl->totalBuys = pImpl->totalSells = pImpl->totalHolds = 0;
QString tradeFilePath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/TradingSimulation/" + pImpl->username + "_trades_taken.txt";
tradeFile.open(tradeFilePath.toStdString(), std::ios::out | std::ios::trunc);
if (!tradeFile.is_open()) {
QMessageBox::warning(nullptr, "Error",
"Couldn't open " + QString(pImpl->username + "_trades_taken.txt") + " file. Please check file permissions.");
}
}
void TradingBot::executeNextDay() {
// Update the day and generate market event
double currentPrice = pImpl->market.getPrice();
double totalValue = pImpl->balance + pImpl->ownedStocks * currentPrice;
pImpl->balanceHistory.push_back(totalValue);
pImpl->stockPriceHistory.push_back(currentPrice);
pImpl->currentDay++;
pImpl->lastMarketEvent = pImpl->generateMarketEvent();
// Adjust market conditions and risk based on the event
pImpl->market.adjustVolatility(pImpl->lastMarketEvent);
pImpl->market.adjustOverallTrend(pImpl->lastMarketEvent);
pImpl->riskManager->adjustRiskTolerance(pImpl->lastMarketEvent);
// Execute trading strategy
double action = pImpl->currentStrategy->execute(currentPrice, pImpl->lastMarketEvent);
double adjustedAction = pImpl->riskManager->adjustPosition(action, currentPrice, pImpl->balance);
currentPrice = pImpl->applySlippage(currentPrice);
// Determine trade action based on adjusted action
std::string tradeAction;
double actionTaken = 0;
if (adjustedAction > 0) { // Buy
int stocksToBuy = static_cast<int>(adjustedAction * pImpl->balance / currentPrice);
double cost = pImpl->applyTransactionFee(stocksToBuy * currentPrice);
if (pImpl->balance >= cost) {
pImpl->ownedStocks += stocksToBuy;
pImpl->balance -= cost;
tradeAction = "Buy";
actionTaken = stocksToBuy;
pImpl->buysSinceLastUpdate++;
} else {
tradeAction = "Hold (Insufficient funds)";
pImpl->holdsSinceLastUpdate++;
}
} else if (adjustedAction < 0 && pImpl->ownedStocks > 0) { // Sell
int stocksToSell = std::min(static_cast<int>(-adjustedAction * pImpl->ownedStocks), pImpl->ownedStocks);
double revenue = pImpl->applyTransactionFee(stocksToSell * currentPrice);
pImpl->ownedStocks -= stocksToSell;
pImpl->balance += revenue;
tradeAction = "Sell";
actionTaken = stocksToSell;
pImpl->sellsSinceLastUpdate++;
} else { // Hold
tradeAction = "Hold";
pImpl->holdsSinceLastUpdate++;
}
// Log trade and save the state
pImpl->logTrade(pImpl->currentDay, currentPrice, tradeAction, actionTaken);
pImpl->saveState();
// Update statistics and balance history
pImpl->balanceHistory.push_back(totalValue);
// pImpl->stockPriceHistory.push_back(currentPrice);
pImpl->lastUpdateValue = totalValue;
pImpl->lastUpdateDay = pImpl->currentDay;
pImpl->maxDrawdown = pImpl->calculateMaxDrawdown();
pImpl->totalReturn = pImpl->calculateCumulativeReturn();
pImpl->volatility = pImpl->calculateVolatility();
pImpl->sharpeRatio = pImpl->calculateSharpeRatio();
// Emit update UI signal
emit updateUI();
// Check if simulation is complete and emit signal
if (isSimulationComplete()) {
emit simulationComplete();
}
}
bool TradingBot::isSimulationComplete() const {
return pImpl->currentDay >= pImpl->totalDays;
}
void TradingBot::logTrade(int day, double currentPrice, const std::string& tradeAction, double actionTaken) {
if (tradeFile.is_open()) {
double totalValue = pImpl->balance + pImpl->ownedStocks * currentPrice;
tradeFile << "Day " << std::setw(3) << day
<< ": Price = $" << std::fixed << std::setprecision(2) << currentPrice
<< ", Action = " << std::setw(4) << tradeAction << " " << std::setw(3) << actionTaken
<< " stocks, Balance = $" << std::setw(10) << pImpl->balance
<< ", Owned Stocks = " << std::setw(4) << pImpl->ownedStocks
<< ", Total Value = $" << std::setw(10) << totalValue << "\n";
tradeFile.flush(); // Ensure the data is written immediately
}
};
void TradingBot::resetToInitialState() {
// Reset the trading bot's state to its initial values
balanceHistory.clear();
stockPriceHistory.clear();
balanceHistory.clear();
stockPriceHistory.clear();
pImpl->balance = 10000.0; // Initial balance
pImpl->ownedStocks = 0; // Reset owned stocks
pImpl->totalReturn = 0.0; // Reset total return
pImpl->maxDrawdown = 0.0; // Reset max drawdown
pImpl->volatility = 0.0; // Reset volatility
pImpl->sharpeRatio = 0.0; // Reset Sharpe ratio
pImpl->lastUpdateValue = pImpl->balance;
pImpl->lastUpdateDay = 0;
pImpl->buysSinceLastUpdate = 0;
pImpl->sellsSinceLastUpdate = 0;
pImpl->holdsSinceLastUpdate = 0;
pImpl->totalBuys = 0;
pImpl->totalSells = 0;
pImpl->totalHolds = 0;
pImpl->currentDay = 0;
pImpl->balanceHistory.clear();
pImpl->balanceHistory.push_back(pImpl->balance);
pImpl->saveState();
pImpl->resetToInitialState();
pImpl->saveState();
}
void TradingBot::generateStockPriceGraph() {
QLineSeries *series = new QLineSeries();
for (size_t i = 0; i < pImpl->stockPriceHistory.size(); ++i) {
series->append(i, pImpl->stockPriceHistory[i]);
}
QChart *chart = new QChart();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Stock Price History");
chart->setBackgroundBrush(QBrush(Qt::white));
chart->setTitleBrush(QBrush(Qt::black));
// Update to avoid deprecated methods
QList<QAbstractAxis *> axesX = chart->axes(Qt::Horizontal);
QList<QAbstractAxis *> axesY = chart->axes(Qt::Vertical);
if (!axesX.isEmpty()) axesX.first()->setLabelsBrush(QBrush(Qt::black));
if (!axesY.isEmpty()) axesY.first()->setLabelsBrush(QBrush(Qt::black));
// Color the line based on trend
QLinearGradient gradient(QPointF(0, 0), QPointF(1, 1));
for (size_t i = 1; i < pImpl->stockPriceHistory.size(); ++i) {
qreal pos = i / qreal(pImpl->stockPriceHistory.size() - 1);
gradient.setColorAt(pos, (pImpl->stockPriceHistory[i] >= pImpl->stockPriceHistory[i-1]) ? Qt::darkGreen : Qt::darkRed);
}
series->setBrush(gradient);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->resize(1031, 304);
QString folderPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/TradingSimulation";
QDir().mkpath(folderPath);
QPixmap pixmap = chartView->grab();
pixmap.save(folderPath + "/" + username + "_stock_price_history.png");
delete chartView;
}
void TradingBot::generateBalanceGraph() {
QLineSeries *series = new QLineSeries();
for (size_t i = 0; i < pImpl->balanceHistory.size(); ++i) {
series->append(i, pImpl->balanceHistory[i]);
}
QChart *chart = new QChart();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Balance History");
chart->setBackgroundBrush(QBrush(Qt::white));
chart->setTitleBrush(QBrush(Qt::black));
// Update to avoid deprecated methods
QList<QAbstractAxis *> axesX = chart->axes(Qt::Horizontal);
QList<QAbstractAxis *> axesY = chart->axes(Qt::Vertical);
if (!axesX.isEmpty()) axesX.first()->setLabelsBrush(QBrush(Qt::black));
if (!axesY.isEmpty()) axesY.first()->setLabelsBrush(QBrush(Qt::black));
// Color the line based on trend
QLinearGradient gradient(QPointF(0, 0), QPointF(1, 1));
for (size_t i = 1; i < pImpl->balanceHistory.size(); ++i) {
qreal pos = i / qreal(pImpl->balanceHistory.size() - 1);
gradient.setColorAt(pos, (pImpl->balanceHistory[i] >= pImpl->balanceHistory[i-1]) ? Qt::darkGreen : Qt::darkRed);
}
series->setBrush(gradient);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->resize(1031, 305);
QString folderPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/TradingSimulation";
QDir().mkpath(folderPath);
QPixmap pixmap = chartView->grab();
pixmap.save(folderPath + "/" + username + "_balance_history.png");
delete chartView;
}
void TradingBot::generateGraphs() {
generateBalanceGraph();
generateStockPriceGraph();
}