-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (60 loc) · 2.81 KB
/
main.cpp
File metadata and controls
84 lines (60 loc) · 2.81 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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "./utils/binder.h"
#include "./utils/structs.h"
#include "./utils/lexer.h"
#include "./utils/parser.h"
#include "./utils/engine.h"
using namespace std;
//! not needed --> cmake build system
// run command: g++ -std=c++20 main.cpp -o main.out && ./main.out
// immutable array to define possible bar fields
const array<string, 5> bar_fields = {"open", "high", "low", "close", "volume"};
// read lines from strategy file
Strategy read_strategy_file(string filename) { // ref to struct
// pull temp backtesting strategy file
string strat_text;
ifstream strat_file(filename); // read, name, file
string full_text{};
while (getline(strat_file, strat_text)) {
full_text += strat_text + "\n"; // one large string
}
LexicalTokenizer build_lexer(full_text); // text lexer
vector<Token> tokens = build_lexer.tokenize(); // tokenize
TokenParser build_parser(tokens); // parse tokens
Strategy strategy = build_parser.parse_rules(); // assign tokens to strategy struct
cout << "\n============================\n" << endl;
cout << "PARSED STRATEGY:\n" << endl;
cout << full_text << endl;
cout << "============================\n" << endl;
strat_file.close(); // for memory space
return strategy; // populated structs
}
int main() {
cout << "Backtesting Engine!\n" << endl;
string filename = "./backtesting_strategy.strat";
Strategy strategy = read_strategy_file(filename); // built strategy struct
cout << "Strategy struct's members have been populate with file attributes!\n" << endl;
// timeline params
string ticker = "NVDA";
string start = "2024-01-01";
string end = "2025-01-01";
auto market_data = get_market_data_over_timeline(ticker, start, end); // fetch data
float capital = 100000.0f; // initial portfolio capital
BacktestingEngine engine(market_data, bar_fields, capital, strategy); // main engine
auto [num_entries, final_pnl, updated_capital, capital_diff_percent] = engine.run_data_through_engine_logic(); // simulate pnl
cout << "\n============================" << endl;
cout << "\nTotal Entries: " << num_entries << endl; // times entered
cout << "Final Realized PnL: $" << final_pnl << endl; // realized --> after exit
cout << "Updated Capital: $" << updated_capital << endl;
if (updated_capital < capital) { // case: loss
cout << "Percent Difference in Capital: (-" << capital_diff_percent << "%)" << endl;
} else if (updated_capital > capital) { // case: gain
cout << "Percent Difference in Capital: (+" << capital_diff_percent << "%)" << endl;
} else { // case: constant
cout << "Percent Difference in Capital: (" << capital_diff_percent << "%)" << endl;
}
return 0;
}