-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket_data_analysis.cpp
112 lines (82 loc) · 2.91 KB
/
market_data_analysis.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
#include <cmath>
#include <algorithm>
#include <cmath>
#include "sys.cpp"
using namespace std;
class MarketDataAnalysis {
public:
MarketDataAnalysis(string assetSymbol);
double calculateAveragePrice();
double calculateVolatility();
vector<PricePoint> getUpwardTrends();
vector<PricePoint> getDownwardTrends();
double calculateRelativeStrengthIndex(size_t period);
PriceHistory priceHistory_;
};
MarketDataAnalysis::MarketDataAnalysis(string assetSymbol) : priceHistory_(assetSymbol) {}
double MarketDataAnalysis::calculateAveragePrice() {
double total = 0.0;
size_t count = priceHistory_.dataPointsCount();
for (size_t i = 0; i < count; ++i) {
total += priceHistory_.getDataPoint(i).getClosing();
}
return total / count;
}
double MarketDataAnalysis::calculateVolatility() {
size_t count = priceHistory_.dataPointsCount();
if (count < 2) {
return 0.0;
}
double sumSquaredDifferences = 0.0;
double averagePrice = calculateAveragePrice();
for (size_t i = 0; i < count; ++i) {
double price = priceHistory_.getDataPoint(i).getClosing();
sumSquaredDifferences += (price - averagePrice) * (price - averagePrice);
}
return sqrt(sumSquaredDifferences / (count - 1));
}
vector<PricePoint> MarketDataAnalysis::getUpwardTrends() {
vector<PricePoint> upwardTrends;
size_t count = priceHistory_.dataPointsCount();
for (size_t i = 1; i < count; ++i) {
PricePoint prevPoint = priceHistory_.getDataPoint(i - 1);
PricePoint currentPoint = priceHistory_.getDataPoint(i);
if (currentPoint.getClosing() > prevPoint.getClosing()) {
upwardTrends.push_back(currentPoint);
}
}
return upwardTrends;
}
vector<PricePoint> MarketDataAnalysis::getDownwardTrends() {
vector<PricePoint> downwardTrends;
size_t count = priceHistory_.dataPointsCount();
for (size_t i = 1; i < count; ++i) {
PricePoint prevPoint = priceHistory_.getDataPoint(i - 1);
PricePoint currentPoint = priceHistory_.getDataPoint(i);
if (currentPoint.getClosing() < prevPoint.getClosing()) {
downwardTrends.push_back(currentPoint);
}
}
return downwardTrends;
}
double MarketDataAnalysis::calculateRelativeStrengthIndex(size_t period) {
size_t count = priceHistory_.dataPointsCount();
if (count <= period) {
return 0.0;
}
double avgGain = 0.0;
double avgLoss = 0.0;
for (size_t i = 1; i < period; ++i) {
double diff = priceHistory_.getDataPoint(i).getClosing() - priceHistory_.getDataPoint(i - 1).getClosing();
if (diff > 0) {
avgGain += diff;
} else {
avgLoss -= diff;
}
}
avgGain /= period;
avgLoss /= period;
double relativeStrength = avgGain / avgLoss;
double rsi = 100.0 - (100.0 / (1.0 + relativeStrength));
return rsi;
}