-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEMA.cpp
More file actions
99 lines (86 loc) · 2.13 KB
/
EMA.cpp
File metadata and controls
99 lines (86 loc) · 2.13 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
/* EMA */
#include <iostream>
#include <vector>
using namespace std;
class EMA {
vector <double> *data; //buffer for the past EMA values
bool isEMAValid; //really shouldn't trust data until numPeriods have passed
int numPeriods;
int barSize; //how long to wait before updating
double alpha; //ema constant
void calculateEMA(); //updates data with ema values
public:
EMA(int barSize, int numPeriods);
~EMA();
void getEMA(); //in a file? a copy of my vector?
bool isValid();
};
//EMA constructor
EMA::EMA(int barSize, int numPeriods) : barSize(barSize), numPeriods(numPeriods), isEMAValid(false)
{
alpha = 2/(numPeriods+1);
data = new vector <double> (100); //arbitrary default size
//request real time bar data?
}
EMA::~EMA()
{
delete data;
}
bool EMA::isValid() {
return isEMAValid;
}
void EMA::calculateEMA() {
double priceToday = 3; //how do I get this?,
data->push_back(priceToday * alpha + data->back() * (1 - alpha)); //calculate EMA, store it
if (data->size() == numPeriods) {
isEMAValid = true;
}
}
void EMA::getEMA() {
return; //not sure yet
}
class MACD {
EMA *slow;
EMA *fast;
EMA *signal;
bool isMACDValid;
vector <double> *histogram;
vector <double> *macd;
int slowBars;
int fastBars;
int slowPeriod;
int fastPeriod;
int macdPeriod;
void calculateMACD();
public:
MACD(int slowBars, int fastBars, int slowPeriod, int fastPeriod, int macdPeriod);
~MACD();
bool isValid();
};
//MACD constructor
MACD::MACD(int slowBars, int fastBars, int slowPeriod, int fastPeriod, int macdPeriod) : slowBars(slowBars), fastBars(fastBars), slowPeriod(slowPeriod),
fastPeriod(fastPeriod), macdPeriod(macdPeriod), isMACDValid(false), signal(NULL)
{
histogram = new vector <double> (100); //arbitrary default size
macd = new vector <double> (100);
this->slow = new EMA(slowBars, slowPeriod);
this->fast = new EMA(fastBars, fastPeriod);
}
void MACD::calculateMACD()
{
unsigned int i = 0;
//macd[i] = fast->data[i] - slow->data[i];
//histogram[i] = macd[i] - signal->data[i];
}
MACD::~MACD()
{
delete slow;
delete fast;
delete signal;
delete histogram;
delete macd;
}
bool MACD::isValid()
{
return isMACDValid;
}