-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStock.cpp
More file actions
135 lines (113 loc) · 2.77 KB
/
Stock.cpp
File metadata and controls
135 lines (113 loc) · 2.77 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
/*
* Stock class
*/
#include "Stock.h"
#include<fstream>
#include<ctime>
#define timestep 1
using namespace std;
// constructor
Stock::Stock(string _tick, vector <int> _EMA_Times, int _MACD_Time) {
// initialize tick and times for EMA and MACD
_tick = tick;
EMA_Times = _EMA_Times;
MACD_Time = _MACD_Time;
start = true;
// initialize EMA vectors
curEMA.resize(EMA_Times.size);
myEMA.resize(EMA_Times.size);
for (std::vector<EMA*>::iterator i = myEMA.begin(), e = myEMA.end();
i != e, ++i) {
(*i) = new EMA (???);
}
myMACD = new MACD(???);
curPrice = -1;
curEMA = -1;
curMACD = -1;
// Create and write to files
string filename;
// Price, ofstream name fPrice, filename RIM_Price.txt
filename = this->tick + "_Price.txt";
fPrice.open(filename.c_str());
fPrice << "Time\tPrice" << endl;
// MACD, ofstream name fMACD, filename RIM_MACD.txt
filename = this->tick + "_MACD.txt";
fMACD.open(filename.c_str());
fMACD << "Time\tMACD" << endl;
// EMA, ofstream name fEMA[i], filename RIM_EMA_Time20s.txt
for (int i=0; i<fEMA.length; ++i) {
int time_period = EMA_Times.at(i);
filename = this->tick + "_EMA_Time" + time_period + "s.txt";
fEMA.at(i).open(filename.c_str());
fEMA.at(i) << "Time\tEMA" << endl;
}
}
Stock::~Stock() {
delete myMACD;
for (std::vector<EMA*>::iterator i = myEMA.begin(), e = myEMA.end();
i != e, ++i) {
delete(*i);
}
// close files
fPrice.close();
fMACD.close();
for (int i=0; i<fEMA.length; ++i) {
fEMA.at(i).close();
}
}
double Stock::getEMA(int index) {
if ( (index >= curEMA.size) || (index < 0) ) {
return -1;
}
if (curEMA < 0) {
return -1;
}
return curEMA;
// return rand() % 100;
}
double Stock::getMACD() {
if (curMACD < 0) {
return -1;
return curMACD;
// return rand() % 100;
}
double Stock::getPrice() {
return curPrice;
// return rand() % 100;
}
string Stock::getTick() {
return tick;
}
void Stock::Start() {
// our time counter, should be GLOBAL for entire program
unsinged long long time = 0;
// multi-threaded? Runs forever
while (start) { // i.e. while(1)
// wait 1 sec
delay(timestep);
time += timestep;
curPrice = API GET PRICE;
// write to file
fPrice << timestep << "\t" << curPrice << endl;
curMACD = -1; // invalid
// if we're at a multiple of MACD_Time
if (time % MACD_Time == 0) {
if (MACD->valid()) {
curMACD = MACD->calculateMACD(price);
// write to file
fMACD << timestep << "\t" << curMACD << endl;
}
}
for (int i=0; i < myEMA.size; ++i) {
curEMA.at(i) = -1;
// if we're at a multiple of EMA_Time(i)
if (time % EMA_Times.at(i) == 0) {
if (myEMA.at(i)->valid()) {
curEMA.at(i) = myEMA.at(i)->calculateEMA(price);
// write to file
fEMA.at(i) << timestep << "\t" << curEMA.at(i) << endl;
}
}
}
}
}