forked from shxhid01/TradingSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
executable file
·311 lines (254 loc) · 11.4 KB
/
mainwindow.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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QDebug>
#include <QTimer>
#include "TradingBot.h"
#include <QStandardPaths>
#include <QInputDialog>
#include <QPixmap>
MainWindow::MainWindow(const QString &username, QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow),
updateTimer(nullptr), tradingBot(nullptr)
{
ui->setupUi(this); // Setup the UI elements
ui->UserName->setText(username); // Set the username display
ui->StockGraph->setScene(new QGraphicsScene(this));
ui->BalGraph->setScene(new QGraphicsScene(this));
tradingBot = new TradingBot(username,this);
connect(tradingBot, &TradingBot::updateUI, this, &MainWindow::updateUIFromBot);
connect(tradingBot, &TradingBot::simulationComplete, this, &MainWindow::onSimulationComplete);
connect(tradingBot, &TradingBot::simulationComplete, this, &MainWindow::updateGraphs);
// Connect button signals to corresponding slots
connect(ui->StartSim, &QPushButton::clicked, this, &MainWindow::onConfirmButtonClicked);
connect(ui->exitButton, &QPushButton::clicked, this, &MainWindow::on_exitButton_clicked);
connect(ui->HelpButton, &QPushButton::clicked, this, &MainWindow::on_HelpButton_clicked);
connect(ui->ResetButton, &QPushButton::clicked, this, &MainWindow::OnResetButtonClicked);
// Set window title
setWindowTitle("Trading Stock Simulation - " + username);
// Set initial values for UI elements
ValuesSet();
}
// Destructor for the MainWindow class
MainWindow::~MainWindow()
{
// Stop and delete the update timer if it exists
if (updateTimer) {
updateTimer->stop(); // Stop the timer
delete updateTimer; // Delete the timer object
}
delete tradingBot; // Delete TradingBot object
delete ui; // Delete UI object
}
// Function to update UI elements with values from the trading bot
void MainWindow::ValuesSet()
{
// Update various UI components with current values from the trading bot
ui->CurrentPriceNum->setNum(tradingBot->getCurrentPrice());
ui->TotalCurrentValueNum->setNum(tradingBot->getTotalValue());
// Update additional UI elements with trading bot values
ui->Bal_User->setNum(tradingBot->getBalance());
ui->StockNum->setNum(tradingBot->getOwnedStocks());
ui->CumReturnNum->setNum(tradingBot->getTotalReturn() * 100);
ui->MaxDrawDownNum->setNum(tradingBot->getMaxDrawdown() * 100);
ui->VolatilityNum->setNum(tradingBot->getVolatility() * 100);
ui->MarketVolatilityNum->setNum(tradingBot->getMarketVolatility() * 100);
ui->SharpeRatioNum->setNum(tradingBot->getSharpeRatio());
ui->CurrentStrategyNum->setText(QString::fromStdString(tradingBot->getCurrentStrategyName()));
ui->CurrentBalanceNum->setNum(tradingBot->getBalance());
ui->PNLnum_2->setNum(tradingBot->getProfitLossSinceStart());
ui->PNLnum->setNum(tradingBot->getProfitLossSinceStart());
ui->PNLnumLastUpdate->setNum(tradingBot->getProfitLossSinceLastUpdate());
ui->DayNum->setNum(tradingBot->getCurrentDay());
ui->NumOfUpdates->setText(QString::number(updateFrequency));
}
// Slot function triggered when the Start Simulation button is clicked
void MainWindow::onConfirmButtonClicked()
{
int strategyIndex = getSelectedStrategyIndex();
int days = getSelectedDays();
// Check if a strategy has been selected
if (strategyIndex == -1) {
QMessageBox::warning(this, "No Strategy Selection", "Please select a strategy before confirming.");
return;
}
// Check if a valid number of days has been selected
if (days == 0) {
QMessageBox::warning(this, "No Day Selection", "Please select a number of days before confirming.");
return;
}
days = getSelectedDays();
updateFrequency = ui->NumOfUpdates->text().toInt();
if (updateFrequency <= 0 || updateFrequency > days) {
QMessageBox::warning(this, "Invalid Update Frequency", "Please enter a valid update frequency (between 1 and the number of simulation days).");
return;
}
currentSimulationDay = 0;
tradingBot->setStrategy(strategyIndex);
tradingBot->initializeSimulation(days);
// Initialize the update timer if it hasn't been already
if (!updateTimer) {
updateTimer = new QTimer(this);
connect(updateTimer, &QTimer::timeout, this, &MainWindow::executeNextTradingDay);
}
updateTimer->start(1); // Execute a day every 0.01 seconds or 10 milliseconds
}
// Function to execute the next trading day
void MainWindow::executeNextTradingDay()
{
if (!tradingBot->isSimulationComplete()) {
currentSimulationDay++;
tradingBot->executeNextDay();
updateUIFromBot();
if (currentSimulationDay % updateFrequency == 0) {
promptForStrategyChange();
}
} else {
updateTimer->stop();
onSimulationComplete();
}
}
void MainWindow::promptForStrategyChange()
{
updateTimer->stop(); // Pause the simulation
QMessageBox msgBox;
msgBox.setText("Do you want to change the trading strategy?");
msgBox.setInformativeText("Market Event: " + QString::fromStdString(tradingBot->getLastMarketEvent()));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
int ret = msgBox.exec();
if (ret == QMessageBox::Yes) {
// Show strategy selection dialog
QStringList strategies;
strategies << "Buy and Hold" << "Mean Reversion" << "Trend Following" << "Random" << "Moving Average";
bool ok;
QString strategy = QInputDialog::getItem(this, "Select Strategy", "Choose a new strategy:", strategies, 0, false, &ok);
if (ok && !strategy.isEmpty()) {
int strategyIndex = strategies.indexOf(strategy);
ui->NumOfUpdates->setText(QString::number(updateFrequency)); // Assuming you have a QLabel named UpdateFrequencyDisplay
tradingBot->setStrategy(strategyIndex);
QMessageBox::information(this, "Strategy Changed", "New strategy: " + strategy);
}
}
updateTimer->start(); // Resume the simulation
}
// Function to update values from the trading bot
void MainWindow::updateUIFromBot()
{
ValuesSet(); // Refresh the UI with the latest values from the trading bot
}
// Slot function triggered when the simulation is complete
void MainWindow::onSimulationComplete()
{
// Generate graphs
tradingBot->generateGraphs();
// Construct the path to trades_taken.txt dynamically
QString homeDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
QString tradesFilePath = homeDir + "/Documents/trades_taken.txt";
// Read and display the contents of trades_taken.txt
QFile tradesFile(tradesFilePath);
if (tradesFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&tradesFile);
QString tradesContent = in.readAll();
ui->TradesTakenBox->setPlainText(tradesContent);
tradesFile.close();
} else {
QMessageBox::warning(this, "File Error", "Unable to open trades_taken.txt");
}
}
// Function to get the selected strategy index
int MainWindow::getSelectedStrategyIndex() const
{
if (ui->BuyAndHold->isChecked()) return 0;
if (ui->MeanReversion->isChecked()) return 1;
if (ui->MovingAvg->isChecked()) return 2;
if (ui->TrendFollowing->isChecked()) return 3;
if (ui->Random->isChecked()) return 4;
return -1; // No strategy selected
}
// Function to get the selected number of days
int MainWindow::getSelectedDays() const
{
if (ui->Day50->isChecked()) return 50;
if (ui->Day100->isChecked()) return 100;
if (ui->Day150->isChecked()) return 150;
if (ui->Day200->isChecked()) return 200;
if (ui->Day400->isChecked()) return 400;
if (ui->Day800->isChecked()) return 800;
if (ui->Day1000->isChecked()) return 1000;
if (ui->Day2000->isChecked()) return 2000;
return 0; // No days selected
}
// Slot function triggered when the exit button is clicked
void MainWindow::on_exitButton_clicked()
{
QApplication::quit(); // Close the application
}
void MainWindow::on_HelpButton_clicked()
{
QString instructions = "Instructions on how to use the application:\n\n"
"1. Select a strategy to use on the top right.\n"
"2. Choose the number of days you want to simulate.\n"
"3. Enter how frequently you want updates to change strategies or not.\n"
"4. On the left, you can see your stats and trading information.\n"
"5. On the bottom, after the simulation is complete, you can see the actions the bot took.";
QMessageBox::information(this, "Help - Instructions", instructions);
}
void MainWindow::OnResetButtonClicked()
// Slot function triggered when the reset button is clicked
{
// Reset the TradingBot to its initial state
tradingBot->resetToInitialState();
// Reset the UI elements to reflect initial state
ValuesSet(); // This will refresh the UI with the initial values from TradingBot
// Optionally reset any other UI elements, like radio buttons or text fields
ui->BuyAndHold->setChecked(false);
ui->MeanReversion->setChecked(false);
ui->MovingAvg->setChecked(false);
ui->TrendFollowing->setChecked(false);
ui->Random->setChecked(false);
// Reset day selection buttons (if needed)
ui->Day50->setChecked(false);
ui->Day100->setChecked(false);
ui->Day150->setChecked(false);
ui->Day200->setChecked(false);
ui->Day400->setChecked(false);
ui->Day800->setChecked(false);
ui->Day1000->setChecked(false);
ui->Day2000->setChecked(false);
QMessageBox::information(this, "Reset Complete", "All values have been reset to their initial states.");
}
void MainWindow::updateGraphs() {
// Load stock graph
QString stockGraphPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/TradingSimulation/" + username + "_stock_price_history.png";
QPixmap stockGraph(stockGraphPath);
if (!stockGraph.isNull()) {
// Clear any existing items in StockGraph
ui->StockGraph->scene()->clear();
// Create a pixmap item and add it to the existing scene
QGraphicsPixmapItem *stockPixmapItem = new QGraphicsPixmapItem(stockGraph);
ui->StockGraph->scene()->addItem(stockPixmapItem);
// Fit the view to the pixmap item
ui->StockGraph->fitInView(stockPixmapItem, Qt::KeepAspectRatio);
} else {
QMessageBox::warning(this, "Error", "Stock graph image could not be loaded.");
}
// Load balance graph
QString balanceGraphPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/TradingSimulation/" + username + "_balance_history.png";
QPixmap balanceGraph(balanceGraphPath);
if (!balanceGraph.isNull()) {
// Clear any existing items in BalGraph
ui->BalGraph->scene()->clear();
// Create a pixmap item and add it to the existing scene
QGraphicsPixmapItem *balancePixmapItem = new QGraphicsPixmapItem(balanceGraph);
ui->BalGraph->scene()->addItem(balancePixmapItem);
// Fit the view to the pixmap item
ui->BalGraph->fitInView(balancePixmapItem, Qt::KeepAspectRatio);
} else {
QMessageBox::warning(this, "Error", "Balance graph image could not be loaded.");
}
}