-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathAverageVolatility.hpp
90 lines (81 loc) · 2.3 KB
/
AverageVolatility.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
#include <queue>
#include <cmath>
#include <iostream>
class AverageVolatility
{
public:
AverageVolatility(int sampling_length, int processing_length)
{
m_sampling_length = sampling_length;
m_processing_length = processing_length;
m_sampling_sum = 0;
m_processing_sum = 0;
}
void add_sample(double value)
{
if (!is_sampling_buffer_full())
{
m_sampling_sum += value;
m_sampling_buffer.push_back(value);
}
else
{
m_sampling_sum -= m_sampling_buffer.front();
m_sampling_buffer.pop_front();
m_sampling_buffer.push_back(value);
m_sampling_sum += value;
}
double indicator = indicator_calculation();
if (!is_processing_buffer_full())
{
m_processing_sum += indicator;
m_processing_buffer.push_back(indicator);
}
else
{
m_processing_sum -= m_processing_buffer.front();
m_processing_buffer.pop_front();
m_processing_buffer.push_back(indicator);
m_processing_sum += indicator;
}
}
double current_value()
{
return processing_calculation();
}
bool is_sampling_buffer_full()
{
return m_sampling_buffer.size() >= m_sampling_length;
}
bool is_processing_buffer_full()
{
return m_processing_buffer.size() >= m_processing_length;
}
private:
int m_sampling_length;
std::deque<double> m_sampling_buffer;
int m_processing_length;
std::deque<double> m_processing_buffer;
double m_sampling_sum;
double m_processing_sum;
private:
double indicator_calculation()
{
double mean = m_sampling_sum / m_sampling_buffer.size();
double sum_temp = 0;
if (m_sampling_buffer.size() == 1)
return 0;
for (int i = 0; i < m_sampling_buffer.size(); i++)
{
double last = m_sampling_buffer.back();
sum_temp += std::pow(last - mean, 2);
m_sampling_buffer.pop_back();
m_sampling_buffer.push_front(last);
}
return (sum_temp / (m_sampling_buffer.size()));
}
double processing_calculation()
{
return std::sqrt(m_processing_sum / m_processing_buffer.size());
}
};