-
Notifications
You must be signed in to change notification settings - Fork 9
/
macd_graph_view.cpp
72 lines (60 loc) · 1.94 KB
/
macd_graph_view.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
67
68
69
70
71
72
#include <QWheelEvent>
#include <QDebug>
#include "macd_graph_view.h"
#include "macd_overlay.h"
#include "graph_viewport.h"
#include "ohlc_provider.h"
#include "grid.h"
#include <assert.h>
MACDGraphView::MACDGraphView() {
m_viewport = NULL;
}
GraphViewport* MACDGraphView::viewport() {
return m_viewport;
}
void MACDGraphView::internalizeViewport(GraphViewport* viewport) {
qDebug() << "Internalizing new viewport";
m_viewport = viewport;
connect(viewport, SIGNAL(changed()), this, SLOT(notifyOverlaysProjectionChanged()));
connect(viewport, SIGNAL(changed()), this, SLOT(notifyOverlaysRangesChanged()));
connect(viewport, SIGNAL(changed()), this, SLOT(redraw()));
}
void MACDGraphView::addOverlays() {
qDebug() << "Adding overlays";
overlays.push_back(new Grid(false));
overlays.push_back(new MACDOverlay(viewport()));
}
NumberAxis MACDGraphView::numberAxis() {
MACDCalculator calculator;
OHLC closure(0, 0, 0, 0); // We always need to show the zero axis.
bool gotSomething = false, gotIt = false;
OHLC tick;
int N = 10;
// hack! duplicate code!
OHLCProvider* projection = viewport()->getSourceProjection();
QDateTime start = projection->getInterval()->minus(timeAxis().getMinTime(), N + 1);
for (QDateTime i = start; i < timeAxis().getMaxTime();
i = projection->getInterval()->firstAfter(i)) {
if (projection->tryGetData(i, tick)) {
gotSomething = true; // to skip empty days
}
if (gotSomething) {
calculator << tick;
MACDCalculator::Entry entry;
if (calculator.get(entry)) {
if (gotIt) {
closure << entry.macd;
closure << entry.signal;
closure << entry.histogram;
} else {
float min = fmin(fmin(entry.macd, entry.signal), entry.histogram);
float max = fmax(fmax(entry.macd, entry.signal), entry.histogram);
closure = OHLC(min, max, min, max);
gotIt = true;
}
}
}
}
// qDebug() << "MACD closure:" << closure;
return NumberAxis("MACD", closure.low, closure.high, 0.0f, height());
}