forked from dcdillon/dependable-development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictor.hpp
117 lines (101 loc) · 2.46 KB
/
predictor.hpp
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
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <cstring>
#include <iostream>
#include <boost/algorithm/string.hpp>
class Predictor
{
public:
Predictor(double *betas, int count, double alpha)
: count_(count)
, alpha_(alpha)
{
betas_ = new double[count_];
emas_ = new double[count_];
prices_ = new double[count_];
for (int i = 0; i < count_; ++i)
{
betas_[i] = betas[i];
emas_[i] = 0.0;
prices_[i] = 0.0;
}
}
void update_prices(double *prices)
{
for (int i = 0; i < count_; ++i)
{
prices_[i] = prices[i];
if (emas_[i] == 0)
{
emas_[i] = prices_[i];
}
else
{
emas_[i] = (1 - alpha_) * emas_[i] + alpha_ * prices_[i];
}
}
}
double predict(double current_price)
{
double forecast = 0;
for (int i = 0; i < count_; ++i)
{
forecast += betas_[i] * (prices_[i] - emas_[i]);
}
return current_price + forecast;
}
private:
double *betas_;
double *emas_;
double *prices_;
int count_;
double alpha_;
};
class Reader
{
public:
bool init(const std::string &symbol, const std::string &date)
{
std::ostringstream buf;
buf << "../data/" << symbol << "/" << date << ".csv";
infile_.open(buf.str().c_str());
// remove the header
std::string line;
std::getline(infile_, line);
peek_next();
accept();
}
const std::string peek_next()
{
do
{
if (infile_.good())
{
std::string line;
std::getline(infile_, line);
boost::algorithm::split(elem_, line, boost::is_any_of(","));
}
else
{
return "";
}
} while (infile_.good() && elem_.size() != 8);
return elem_[0];
}
void accept()
{
if (elem_.size() == 8)
{
price_ = atof(elem_[4].c_str());
}
}
double price() { return price_; }
bool good() { return infile_.good(); }
const std::string &time() { return elem_[0]; }
private:
std::vector< std::string > elem_;
std::ifstream infile_;
double price_;
};