forked from lava/matplotlib-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdist.cpp
More file actions
214 lines (182 loc) · 6.51 KB
/
dist.cpp
File metadata and controls
214 lines (182 loc) · 6.51 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
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <time.h>
#include <vector>
#include <map>
#include <curl/curl.h>
#include <algorithm>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <Python.h>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
using namespace boost::property_tree;
// Importing stock data from Polygon.io
std::string address(std::string ticker){
std::string key = "";
std::string url = "https://api.polygon.io/v2/aggs/ticker/" + ticker + "/range/1/day/2024-01-09/2024-09-29?adjusted=true&sort=asc&limit=300&apiKey=" + key;
return url;
}
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response) {
size_t totalSize = size * nmemb;
response->append((char*)contents, totalSize);
return totalSize;
}
std::string RequestData(const std::string& url) {
CURL* curl;
CURLcode res;
std::string response;
curl = curl_easy_init(); // Initialize CURL
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // Set the URL
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // Set the callback function
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); // Pass the response string to the callback
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects if necessary
// Perform the request
res = curl_easy_perform(curl);
// Check for errors
if(res != CURLE_OK) {
std::cerr << "CURL request failed: " << curl_easy_strerror(res) << std::endl;
}
// Cleanup
curl_easy_cleanup(curl);
}
return response; // Return the response
}
void Sleep(int wait_time){
std::this_thread::sleep_for(std::chrono::seconds(wait_time));
}
std::map<std::string, std::vector<double>> ImportHistoricalData(std::vector<std::string> tickers, int wait_time){
std::map<std::string, std::vector<double>> result;
std::cout << "Stock data is loading" << std::endl;
for(auto & stock : tickers){
Sleep(wait_time);
std::cout << stock << " is imported" << std::endl;
std::string response = RequestData(address(stock));
ptree data;
std::stringstream ss(response);
read_json(ss, data);
for(ptree::const_iterator it = data.begin(); it != data.end(); ++it){
if(it->first == "results"){
for(ptree::const_iterator jt = it->second.begin(); jt != it->second.end(); ++jt){
for(ptree::const_iterator kt = jt->second.begin(); kt != jt->second.end(); ++kt){
if(kt->first == "l"){
result[stock].push_back(atof(kt->second.get_value<std::string>().c_str()));
}
}
}
}
}
}
return result;
}
void ComputeData(std::vector<double> close, std::string ticker, std::map<std::string, std::map<std::string, std::vector<double>>> & modify){
auto average = [](std::vector<double> x){
double total = 0;
for(auto & i : x){
total += i;
}
total /= x.size();
return total;
};
auto volatility = [&](std::vector<double> x){
double mu = average(x);
double total = 0;
for(auto & i : x){
total += pow(i - mu, 2);
}
total /= ((double) x.size() - 1);
return pow(total, 0.5);
};
auto dWT = [](){
int num = 10;
double dw = (rand() % (2*num + 1)) - num;
return dw/100.0;
};
auto histogram = [](std::vector<double> returns, int bins){
std::map<std::string, std::vector<double>> res;
std::sort(returns.begin(), returns.end());
double m0 = returns[0];
double m1 = returns[returns.size() - 1];
double dm = (m1 - m0)/((double) bins);
for(int i = 0; i < bins; ++i){
double a = m0 + i*dm;
double b = m0 + (i+1)*dm;
int count = 0;
for(auto & r : returns){
if(i == bins - 1){
if(r >= a && r <= b){
count += 1;
}
} else {
if(r >= a && r < b){
count += 1;
}
}
}
double mid = 0.5*(a + b);
res["x"].push_back(mid);
res["y"].push_back(count);
}
return res;
};
std::vector<double> ror, ror_predict, stock_paths;
for(int i = 1; i < close.size(); ++i){
ror.push_back(close[i]/close[i-1] - 1.0);
}
double S = close[close.size() - 1];
double mu = average(ror);
double t = 1.0/12.0;
double v = volatility(ror);
int N = 1000;
int P = 100;
double dt = t / (double) N;
// mu*S*dt + v*S*dWT()
srand(time(NULL));
for(int p = 0; p < P; ++p){
double S0 = S;
for(int t = 0; t < N; ++t){
S0 += mu*S0*dt + v*S0*dWT();
}
stock_paths.push_back(S0);
}
for(int i = 1; i < stock_paths.size(); ++i){
ror_predict.push_back(stock_paths[i]/stock_paths[i-1] - 1.0);
}
std::map<std::string, std::vector<double>> RHist = histogram(ror, 30);
std::map<std::string, std::vector<double>> RPred = histogram(ror_predict, 30);
modify[ticker]["xhist"] = RHist["x"];
modify[ticker]["yhist"] = RHist["y"];
modify[ticker]["xpred"] = RPred["x"];
modify[ticker]["ypred"] = RPred["y"];
}
int main()
{
std::vector<std::string> tickers = {"MSFT","AAPL","NVDA","AMZN","IBM","ORCL"};
std::vector<PyObject*> plots;
std::vector<int> pnum = {231, 232, 233, 234, 235, 236};
for(auto & number : pnum){
plots.push_back(plt::chart2D(number));
}
int sleep_for_time = 10;
std::map<std::string, std::vector<double>> close = ImportHistoricalData(tickers, sleep_for_time);
std::map<std::string, std::map<std::string, std::vector<double>>> modify;
std::vector<std::thread> items;
for(auto & ticker : tickers){
items.emplace_back(ComputeData, close[ticker], ticker, std::ref(modify));
}
for(auto & plane : items){
plane.join();
}
for(int i = 0; i < plots.size(); ++i){
PyObject * ax = plots[i];
std::string tick = tickers[i];
plt::PlotTitle(ax, tick);
plt::plot2D(ax, modify[tick]["xhist"], modify[tick]["yhist"], "red");
plt::plot2D(ax, modify[tick]["xpred"], modify[tick]["ypred"], "limegreen");
}
plt::show();
return 0;
}