-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoving_average_cpp_enhanced.cpp
More file actions
156 lines (126 loc) · 4.81 KB
/
moving_average_cpp_enhanced.cpp
File metadata and controls
156 lines (126 loc) · 4.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
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <chrono>
#include <numeric>
#include <unordered_map>
#include <algorithm>
#include <cmath>
struct Candlestick {
std::string timestamp;
double open;
double high;
double low;
double close;
double volume;
};
std::vector<Candlestick> readCSV(const std::string& filename) {
std::vector<Candlestick> data;
std::ifstream file(filename);
std::string line;
// Skip header
std::getline(file, line);
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string cell;
std::vector<std::string> row;
while (std::getline(ss, cell, ',')) {
row.push_back(cell);
}
if (row.size() >= 6) {
Candlestick candle;
candle.timestamp = row[0];
candle.open = std::stod(row[1]);
candle.high = std::stod(row[2]);
candle.low = std::stod(row[3]);
candle.close = std::stod(row[4]);
candle.volume = std::stod(row[5]);
data.push_back(candle);
}
}
return data;
}
std::vector<double> calculateMovingAverage(const std::vector<double>& prices, int period) {
std::vector<double> ma;
if (prices.size() < period) {
return ma;
}
ma.reserve(prices.size() - period + 1);
// Calculate initial sum for the first window
double sum = 0.0;
for (int i = 0; i < period; ++i) {
sum += prices[i];
}
ma.push_back(sum / period);
// Use sliding window technique for efficiency
for (size_t i = period; i < prices.size(); ++i) {
sum = sum - prices[i - period] + prices[i];
ma.push_back(sum / period);
}
return ma;
}
std::unordered_map<std::string, std::vector<double>> calculateMultipleMovingAverages(
const std::vector<double>& prices,
const std::vector<int>& periods) {
std::unordered_map<std::string, std::vector<double>> results;
for (int period : periods) {
std::string key = "MA_" + std::to_string(period);
results[key] = calculateMovingAverage(prices, period);
}
return results;
}
std::vector<double> calculateComplexMath(const std::vector<double>& opens,
const std::vector<double>& highs,
const std::vector<double>& lows,
const std::vector<double>& closes) {
std::vector<double> result;
result.reserve(closes.size());
for (size_t i = 0; i < closes.size(); ++i) {
// Complex mathematical function: weighted combination of OHLC values
double value = (opens[i] * 0.1) + (highs[i] * 0.3) + (lows[i] * 0.2) + (closes[i] * 0.4);
// Apply exponential and logarithmic transformations
value = std::exp(value / 100.0) * std::log(std::abs(value) + 1.0);
result.push_back(value);
}
return result;
}
int main() {
auto start_time = std::chrono::high_resolution_clock::now();
std::cout << "Reading CSV file..." << std::endl;
std::vector<Candlestick> data = readCSV("USDJPY2.csv");
std::cout << "Loaded " << data.size() << " records." << std::endl;
// Extract price vectors
std::vector<double> opens, highs, lows, closes;
opens.reserve(data.size());
highs.reserve(data.size());
lows.reserve(data.size());
closes.reserve(data.size());
for (const auto& candle : data) {
opens.push_back(candle.open);
highs.push_back(candle.high);
lows.push_back(candle.low);
closes.push_back(candle.close);
}
// Calculate multiple moving averages for periods 200-220
std::cout << "Calculating moving averages for periods 200-220..." << std::endl;
std::vector<int> ma_periods;
for (int i = 200; i <= 220; ++i) {
ma_periods.push_back(i);
}
auto all_mas = calculateMultipleMovingAverages(closes, ma_periods);
for (const auto& pair : all_mas) {
const std::string& period = pair.first;
const std::vector<double>& ma_values = pair.second;
std::cout << "Calculated " << ma_values.size() << " " << period << " values." << std::endl;
}
// Perform complex mathematical operations
std::cout << "Performing complex mathematical operations..." << std::endl;
auto complexResults = calculateComplexMath(opens, highs, lows, closes);
std::cout << "Completed " << complexResults.size() << " complex calculations." << std::endl;
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
std::cout << "Total execution time: " << duration.count() << " ms" << std::endl;
return 0;
}