-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoving_average_cpp.cpp
More file actions
119 lines (96 loc) · 3.7 KB
/
moving_average_cpp.cpp
File metadata and controls
119 lines (96 loc) · 3.7 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <chrono>
#include <numeric>
#include <deque>
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<Candlestick>& data, int period) {
std::vector<double> ma;
if (data.size() < period) {
return ma;
}
// Calculate initial sum for the first window
double sum = 0.0;
for (int i = 0; i < period; ++i) {
sum += data[i].close;
}
ma.push_back(sum / period);
// Use sliding window technique for efficiency
for (size_t i = period; i < data.size(); ++i) {
sum = sum - data[i - period].close + data[i].close;
ma.push_back(sum / period);
}
return ma;
}
std::vector<double> calculateComplexMath(const std::vector<Candlestick>& data) {
std::vector<double> result;
result.reserve(data.size());
for (const auto& candle : data) {
// Complex mathematical function: weighted combination of OHLC values
double value = (candle.open * 0.1) + (candle.high * 0.3) + (candle.low * 0.2) + (candle.close * 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;
// Calculate multiple moving averages
std::cout << "Calculating 50-period MA..." << std::endl;
auto ma50 = calculateMovingAverage(data, 50);
std::cout << "Calculated " << ma50.size() << " MA50 values." << std::endl;
std::cout << "Calculating 200-period MA..." << std::endl;
auto ma200 = calculateMovingAverage(data, 200);
std::cout << "Calculated " << ma200.size() << " MA200 values." << std::endl;
std::cout << "Calculating 500-period MA..." << std::endl;
auto ma500 = calculateMovingAverage(data, 500);
std::cout << "Calculated " << ma500.size() << " MA500 values." << std::endl;
// Perform complex mathematical operations
std::cout << "Performing complex mathematical operations..." << std::endl;
auto complexResults = calculateComplexMath(data);
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;
}