-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdata.cpp
More file actions
276 lines (207 loc) · 7.18 KB
/
data.cpp
File metadata and controls
276 lines (207 loc) · 7.18 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//=======================================================================
// Copyright Baptiste Wicht 2019-2024.
// Distributed under the MIT License.
// (See accompanying file LICENSE or copy at
// http://opensource.org/licenses/MIT)
//=======================================================================
#include <fstream>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <unordered_map>
#include <mutex>
#include "data.hpp"
namespace {
std::mutex server_lock;
std::unordered_map<std::string, swr::data_vector> data_cache;
swr::data_vector load_data(const std::string& name, const std::string& path) {
{
const std::unique_lock l(server_lock);
if (data_cache.contains(name)) {
return data_cache[name];
}
}
swr::data_vector points;
std::ifstream file(path);
if (!file) {
std::cout << "Impossible to load data " << path << std::endl;
return {};
}
points.name = name;
std::string line;
while (std::getline(file, line)) {
auto index1 = line.find(',');
auto index2 = line.find(',', index1 + 1);
std::string month(line.begin(), line.begin() + index1);
std::string year(line.begin() + index1 + 1, line.begin() + index2);
std::string value(line.begin() + index2 + 1, line.end());
if (value[0] == '\"') {
value = value.substr(1, value.size() - 3);
}
std::erase(value, ',');
swr::data data;
data.month = atoi(month.c_str());
data.year = atoi(year.c_str());
data.value = atof(value.c_str());
points.data.push_back(data);
}
{
const std::unique_lock l(server_lock);
data_cache[name] = points;
}
return points;
}
// Make sure that the data ends with a full year
void fix_end(swr::data_vector& values) {
while (values.data.back().month != 12) {
values.data.pop_back();
}
}
// Make sure that the data starts with a full year
void fix_start(swr::data_vector& values) {
while (values.data.front().month != 1) {
values.data.erase(values.data.begin());
}
}
void normalize_data(swr::data_vector& values) {
fix_end(values);
fix_start(values);
if (values.data.front().value == 1.0f) {
return;
}
auto previous_value = values[0].value;
values[0].value = 1.0f;
for (size_t i = 1; i < values.size(); ++i) {
auto value = values[i].value;
values[i].value = values[i - 1].value * (value / previous_value);
previous_value = value;
}
}
void transform_to_returns(swr::data_vector& values) {
// Should already be normalized
float previous_value = values[0].value;
for (size_t i = 1; i < values.size(); ++i) {
auto new_value = values[i].value / previous_value;
previous_value = values[i].value;
values[i].value = new_value;
}
}
} // end of anonymous namespace
std::vector<swr::data_vector> swr::load_adjusted_values(const std::vector<swr::allocation>& portfolio) {
auto values = load_values(portfolio);
for (size_t i = 0; i < values.size(); ++i) {
if (portfolio[i].asset == "us_bonds") {
for (auto& v : values[i]) {
v.value -= 0.25f / 100.0f;
}
}
}
return values;
}
std::vector<swr::data_vector> swr::load_values(const std::vector<swr::allocation>& portfolio) {
std::vector<swr::data_vector> values;
for (auto& asset : portfolio) {
const auto& asset_name = asset.asset;
bool x2 = asset_name.ends_with("_x2");
std::string filename = x2 ? std::string(asset_name.begin(), asset_name.end() - 3) : asset_name;
auto data = load_data(filename, "stock-data/" + filename + ".csv");
if (data.empty()) {
std::cout << "Impossible to load data for asset " << asset_name << std::endl;
return {};
}
normalize_data(data);
transform_to_returns(data);
if (x2) {
auto copy = data;
std::ranges::copy(copy, std::back_inserter(data.data));
for (size_t i = 0; i < copy.size(); ++i) {
size_t j = copy.size() - 1 - i;
auto& curr = data[j];
const auto& prev = data[j + 1];
if (prev.month == 1) {
curr.month = 12;
curr.year = prev.year - 1;
} else {
curr.month = prev.month - 1;
curr.year = prev.year;
}
}
}
values.emplace_back(std::move(data));
}
return values;
}
swr::data_vector swr::load_inflation(const std::vector<swr::data_vector>& values, const std::string& inflation) {
swr::data_vector inflation_data;
if (inflation == "no_inflation") {
inflation_data = values.front();
for (auto& value : inflation_data) {
value.value = 1;
}
} else {
inflation_data = load_data(inflation, "stock-data/" + inflation + ".csv");
if (inflation_data.empty()) {
std::cout << "Impossible to load inflation data for asset " << inflation << std::endl;
return {};
}
normalize_data(inflation_data);
transform_to_returns(inflation_data);
}
return inflation_data;
}
swr::data_vector swr::load_exchange(const std::string& exchange) {
auto exchange_data = load_data(exchange, "stock-data/" + exchange + ".csv");
if (exchange_data.empty()) {
std::cout << "Impossible to load exchange data for " << exchange << std::endl;
return {};
}
normalize_data(exchange_data);
transform_to_returns(exchange_data);
return exchange_data;
}
swr::data_vector swr::load_exchange_inv(const std::string& exchange) {
auto exchange_data = load_data(exchange, "stock-data/" + exchange + ".csv");
if (exchange_data.empty()) {
std::cout << "Impossible to load exchange data for " << exchange << std::endl;
return {};
}
// Invert the exchange rate
for (auto& v : exchange_data) {
v.value = 1.0f / v.value;
}
normalize_data(exchange_data);
transform_to_returns(exchange_data);
return exchange_data;
}
float swr::get_value(const swr::data_vector& values, size_t year, size_t month) {
for (auto& data : values) {
if (data.year == year && data.month == month) {
return data.value;
}
}
std::cout << "This should not happen (value out of range)" << std::endl;
return 0.0f;
}
swr::data_vector::const_iterator swr::get_start(const swr::data_vector& values, size_t year, size_t month) {
auto it = values.begin();
auto end = values.end();
while (it != end) {
if (it->year == year && it->month == month) {
return it;
}
++it;
}
std::cout << "This should not happen (start out of range)" << std::endl;
return values.begin();
}
bool swr::is_start_valid(const swr::data_vector& values, size_t year, size_t month) {
auto it = values.begin();
auto end = values.end();
while (it != end) {
if (it->year == year && it->month == month) {
return true;
}
++it;
}
return false;
}