-
Notifications
You must be signed in to change notification settings - Fork 9
/
ohlc_memory_provider.cpp
46 lines (38 loc) · 1.3 KB
/
ohlc_memory_provider.cpp
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
#include "ohlc_memory_provider.h"
OHLCMemoryProvider::OHLCMemoryProvider(QDateTime minimum, QDateTime maximum, CandlestickInterval* interval) {
this->interval = interval;
this->minimum = interval->lastBefore(minimum);
this->maximum = interval->firstAfter(maximum);
length = interval->index(minimum, maximum) + 2;
data = vector<pair<OHLC, bool> >(length, make_pair(OHLC(), false));
}
long OHLCMemoryProvider::getTimeIndex(QDateTime time) {
int index = interval->index(minimum, time);
if (index < 0 || index > length) {
qDebug() << "OHLC memory provider out of range: minimum=" << minimum << "maximum=" << maximum << "t=" << time;
throw OHLCProvider::out_of_range();
}
return index;
}
void OHLCMemoryProvider::addData(QDateTime start, OHLC tick) {
data[getTimeIndex(start)] = make_pair(tick, true);
}
QDateTime OHLCMemoryProvider::getMinimum() {
return minimum;
}
QDateTime OHLCMemoryProvider::getMaximum() {
return maximum;
}
CandlestickInterval* OHLCMemoryProvider::getInterval() {
return interval;
}
bool OHLCMemoryProvider::tryGetData(QDateTime start, OHLC& output) {
try {
pair<OHLC, bool> tick = data[getTimeIndex(start)];
output = tick.first;
return tick.second;
} catch (OHLCProvider::out_of_range&) {
qDebug() << "get data for start=" << start << "failed: out of range";
return false;
}
}