-
Notifications
You must be signed in to change notification settings - Fork 9
/
ohlc.cpp
66 lines (56 loc) · 1.34 KB
/
ohlc.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cmath>
#include <QDebug>
#include "ohlc.h"
#include "ohlc_provider.h"
using namespace std;
void OHLC::operator<<(const OHLC& other) {
high = fmax(other.high, high);
low = fmin(other.low, low);
close = other.close;
}
void OHLC::operator<<(float x) {
high = fmax(x, high);
low = fmin(x, low);
close = x;
}
bool OHLC::isUp() {
return open <= close;
}
static void standardizeTo(float &x, OHLC general) {
x = (x - general.low) / (general.high - general.low);
}
void OHLC::standardizeTo(OHLC general) {
::standardizeTo(open, general);
::standardizeTo(high, general);
::standardizeTo(low, general);
::standardizeTo(close, general);
}
QDebug operator<< (QDebug d, const OHLC &ohlc) {
d << "[O:" << ohlc.open << "H:" << ohlc.high << "L:" << ohlc.low << "C:" << ohlc.close << "]";
return d;
}
bool OHLC::span(OHLCProvider* provider, OHLC& result) {
bool sourceEmpty = true;
OHLC sourceClosure;
for (QDateTime start = provider->getMinimum();
start < provider->getMaximum();
start = provider->getInterval()->firstAfter(start)) {
OHLC tick;
if (!provider->tryGetData(start, tick)) continue;
if (sourceEmpty) {
sourceClosure = tick;
sourceEmpty = false;
} else {
sourceClosure << tick;
}
}
if (!sourceEmpty) {
result = sourceClosure;
return true;
} else {
return false;
}
}
OHLC::operator float() {
return close;
}