-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcandlestick_interval.cpp
57 lines (47 loc) · 1.31 KB
/
candlestick_interval.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
#include "candlestick_interval.h"
#include <QDebug>
// TODO: optimize this, this gives quadratic complexity...
QDateTime CandlestickInterval::minus(QDateTime x, int n) {
for (int i = 0; i <= n; i++) {
QDateTime y = lastBefore(x);
// XXX HACK: lastBefore returns interval start...
if (x.toTime_t() == y.toTime_t()) y = lastBefore(y.addSecs(-1));
x = y;
}
return x;
}
int CandlestickInterval::index(QDateTime start, QDateTime end) {
int j = 0;
qDebug() << "index" << end << "before" << start;
if (end < start) return -1; // minus something...
for (QDateTime i = lastBefore(start);
i < firstAfter(end);
i = firstAfter(i), j++) {
qDebug() << "*" << i;
}
qDebug() << "->" << j;
return j;
}
QDateTime CI::Day::lastBefore(QDateTime x) {
x.setTime(QTime(0, 0, 0));
return x;
}
QDateTime CI::Day::firstAfter(QDateTime x) {
return QDateTime(x.date().addDays(1));
}
int CI::Day::index(QDateTime start, QDateTime endDT) {
QDate i = start.date();
QDate end = firstAfter(endDT).date();
int days = 0;
// Bigger jumps
while (i.month() < end.month() - 1 || i.year() < end.year()) {
days += i.daysInMonth();
i = i.addMonths(1);
// qDebug() << "*" << i << " #" << days;
}
for (; i < end; i = i.addDays(1), days++) {
// qDebug() << "*" << i << " #" << days;
}
// qDebug() << "->" << days;
return days;
}