-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrading_bot_test_.cpp
122 lines (104 loc) · 3.8 KB
/
trading_bot_test_.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
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include "TradingBot.h"
// Simple test framework
class TestSuite {
private:
std::vector<std::pair<std::string, std::function<bool()>>> tests;
int passedTests = 0;
int totalTests = 0;
public:
void addTest(const std::string& name, std::function<bool()> test) {
tests.push_back({name, test});
}
void runTests() {
for (const auto& test : tests) {
std::cout << "Running test: " << test.first << "... ";
if (test.second()) {
std::cout << "PASSED" << std::endl;
passedTests++;
std::cout << "Test " << test.first << " passed!" << std::endl; // Added message for passing tests
} else {
std::cout << "FAILED" << std::endl;
}
totalTests++;
}
std::cout << "\nTest results: " << passedTests << " passed out of " << totalTests << " total tests." << std::endl;
}
};
// Helper macros for assertions
#define ASSERT_EQ(a, b) if ((a) != (b)) { std::cout << "Assertion failed: " << #a << " != " << #b << std::endl; return false; }
#define ASSERT_DOUBLE_EQ(a, b) if (std::abs((a) - (b)) > 1e-6) { std::cout << "Assertion failed: " << #a << " != " << #b << std::endl; return false; }
// Unit tests for TradingBot
int test() {
TestSuite suite;
// Test 1: Initial state of the TradingBot
suite.addTest("InitialState", []() {
TradingBot bot("TestUser");
ASSERT_DOUBLE_EQ(bot.getBalance(), 10000.0);
ASSERT_EQ(bot.getOwnedStocks(), 0);
ASSERT_DOUBLE_EQ(bot.getTotalReturn(), 0.0);
ASSERT_DOUBLE_EQ(bot.getMaxDrawdown(), 0.0);
ASSERT_DOUBLE_EQ(bot.getVolatility(), 0.0);
ASSERT_DOUBLE_EQ(bot.getSharpeRatio(), 0.0);
return true;
});
// Test 2: Setting and getting strategy
suite.addTest("SetAndGetStrategy", []() {
TradingBot bot("TestUser");
bot.setStrategy(0); // Assuming 0 is Buy and Hold
ASSERT_EQ(bot.getCurrentStrategyName(), "Buy and Hold");
bot.setStrategy(1); // Assuming 1 is Mean Reversion
ASSERT_EQ(bot.getCurrentStrategyName(), "Mean Reversion");
return true;
});
// Test 3: Simulation initialization
suite.addTest("SimulationInitialization", []() {
TradingBot bot("TestUser");
bot.initializeSimulation(100);
ASSERT_EQ(bot.getCurrentDay(), 0);
ASSERT_EQ(bot.isSimulationComplete(), false);
return true;
});
// Test 4: Executing a trading day
suite.addTest("ExecuteTradingDay", []() {
TradingBot bot("TestUser");
bot.initializeSimulation(100);
bot.setStrategy(0); // Set to Buy and Hold for predictable behavior
double initialBalance = bot.getBalance();
bot.executeNextDay();
ASSERT_EQ(bot.getCurrentDay(), 1);
ASSERT_EQ(bot.getBalance() != initialBalance, true); // Balance should change after a day of trading
return true;
});
// Test 5: Simulation completion
suite.addTest("SimulationCompletion", []() {
TradingBot bot("TestUser");
bot.initializeSimulation(5);
for (int i = 0; i < 5; ++i) {
bot.executeNextDay();
}
ASSERT_EQ(bot.isSimulationComplete(), true);
ASSERT_EQ(bot.getCurrentDay(), 5);
return true;
});
// Run all tests
suite.runTests();
return 0;
}
int test9() {
std::cout << "Starting test suite..." << std::endl;
TestSuite suite;
// Add tests as before...
std::cout << "Running all tests..." << std::endl;
suite.runTests();
std::cout << "Test suite completed." << std::endl;
return 0;
}
// Main function to run the tests
int main() {
std::cout << "Running TradingBot tests..." << std::endl;
return test();
}