-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_hedging.cpp
49 lines (43 loc) · 1.56 KB
/
dynamic_hedging.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
#include <iostream>
#include <sstream>
#include <cstring>
#include "./includes/data_sys.hpp"
class DynamicHedging {
public:
DynamicHedging(const string& assetSymbol, const char* startDate, const char* endDate, const char* interval)
: priceHistory(assetSymbol) {
priceHistory.fetchHistoricalData(startDate, endDate, interval);
}
void performDynamicHedging() {
const size_t shortWindow = 20;
const size_t longWindow = 50;
size_t dataSize = priceHistory.dataPointsCount();
if (dataSize < longWindow) {
cout << "Insufficient data for hedging." << endl;
return;
}
vector<double> shortMA(dataSize - shortWindow + 1, 0.0);
vector<double> longMA(dataSize - longWindow + 1, 0.0);
for (size_t i = 0; i < dataSize - shortWindow + 1; ++i) {
double sumShort = 0.0;
double sumLong = 0.0;
for (size_t j = 0; j < shortWindow; ++j) {
sumShort += priceHistory.getDataPoint(i + j).getClosing();
if (j < longWindow) {
sumLong += priceHistory.getDataPoint(i + j).getClosing();
}
}
shortMA[i] = sumShort / shortWindow;
longMA[i] = sumLong / longWindow;
}
cout << "Moving Averages:" << endl;
for (size_t i = 0; i < shortMA.size(); ++i) {
PricePoint currentDataPoint = priceHistory.getDataPoint(i + shortWindow - 1);
cout << "Date: " << currentDataPoint.getDateString()
<< " Short MA: " << shortMA[i]
<< " Long MA: " << longMA[i] << endl;
}
}
private:
PriceHistory priceHistory;
};