-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmean_reversion.cpp
49 lines (39 loc) · 1.57 KB
/
mean_reversion.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
#include <iostream>
#include <vector>
#include "./includes/data_sys.hpp"
class MeanReversion {
public:
MeanReversion(const PriceHistory& history, int windowSize);
void calculateMeanReversion();
void printMeanReversionResults();
double getMeanReversionValue(size_t index) const;
double getAverageMeanReversion() const;
size_t getMaxMeanReversionIndex() const;
size_t getMinMeanReversionIndex() const;
private:
const PriceHistory& priceHistory;
int movingWindowSize;
std::vector<double> meanReversionValues;
double calculateMovingAverage(size_t startIndex) const;
};
double MeanReversion::getMeanReversionValue(size_t index) const {
if (index < meanReversionValues.size()) {
return meanReversionValues[index];
}
throw std::out_of_range("Index out of range.");
}
double MeanReversion::getAverageMeanReversion() const {
double sum = 0.0;
for (double value : meanReversionValues) {
sum += value;
}
return sum / meanReversionValues.size();
}
size_t MeanReversion::getMaxMeanReversionIndex() const {
double maxValue = *std::max_element(meanReversionValues.begin(), meanReversionValues.end());
return std::distance(meanReversionValues.begin(), std::find(meanReversionValues.begin(), meanReversionValues.end(), maxValue));
}
size_t MeanReversion::getMinMeanReversionIndex() const {
double minValue = *std::min_element(meanReversionValues.begin(), meanReversionValues.end());
return std::distance(meanReversionValues.begin(), std::find(meanReversionValues.begin(), meanReversionValues.end(), minValue));
}