-
Notifications
You must be signed in to change notification settings - Fork 11
/
er.cc
138 lines (102 loc) · 3.48 KB
/
er.cc
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
136
137
138
/*
* This file is part of tindicators, licensed under GNU LGPL v3.
* Author: Ilya Pikulin <ilya.pikulin@gmail.com>, 2019-2021
*/
#include <new>
#include "../indicators.h"
#include "../utils/ringbuf.hh"
int ti_er_start(TI_REAL const *options) {
const int period = options[0];
const int n = period;
return n;
}
int ti_er(int size, TI_REAL const *const *inputs, TI_REAL const *options, TI_REAL *const *outputs) {
TI_REAL const *const series = inputs[0];
const int period = options[0];
const int n = period;
TI_REAL *er = outputs[0];
if (period < 1) { return TI_INVALID_OPTION; }
TI_REAL trajectory = 0;
int i = 0;
for (i = 1; i < n && i < size; ++i) {
trajectory += fabs(series[i] - series[i-1]);
}
for (; i < size; ++i) {
trajectory += fabs(series[i] - series[i-1]);
TI_REAL net_change = fabs(series[i] - series[i-n]);
*er++ = net_change ? net_change / trajectory : 0;
trajectory -= fabs(series[i-n+1] - series[i-n]);
}
return TI_OKAY;
}
DONTOPTIMIZE int ti_er_ref(int size, TI_REAL const *const *inputs, TI_REAL const *options, TI_REAL *const *outputs) {
TI_REAL const *const series = inputs[0];
const int period = options[0];
const int n = period;
TI_REAL *er = outputs[0];
if (period < 1) { return TI_INVALID_OPTION; }
for (int i = n + 1; i <= size; ++i) {
TI_REAL net_change = fabs(series[i-1] - series[i-n-1]);
TI_REAL trajectory = 0;
for (int j = i - n; j < i; ++j) {
trajectory += fabs(series[j] - series[j-1]);
}
*er++ = net_change ? net_change / trajectory : 0;
}
return TI_OKAY;
}
struct ti_er_stream : ti_stream {
struct {
int period;
} options;
struct {
TI_REAL trajectory;
ringbuf<0> price;
} state;
struct {
} constants;
};
int ti_er_stream_new(TI_REAL const *options, ti_stream **stream) {
const int period = options[0];
const int n = period;
if (period < 1) { return TI_INVALID_OPTION; }
ti_er_stream *ptr = new(std::nothrow) ti_er_stream();
if (!ptr) { return TI_OUT_OF_MEMORY; }
*stream = ptr;
ptr->index = TI_INDICATOR_ER_INDEX;
ptr->progress = -ti_er_start(options);
ptr->options.period = period;
ptr->state.price.resize(period+1);
return TI_OKAY;
}
void ti_er_stream_free(ti_stream *stream) {
delete static_cast<ti_er_stream*>(stream);
}
int ti_er_stream_run(ti_stream *stream, int size, TI_REAL const *const *inputs, TI_REAL *const *outputs) {
ti_er_stream *ptr = static_cast<ti_er_stream*>(stream);
TI_REAL const *const series = inputs[0];
TI_REAL *er = outputs[0];
int progress = ptr->progress;
const int period = ptr->options.period;
const int n = period;
TI_REAL trajectory = ptr->state.trajectory;
auto &price = ptr->state.price;
int i = 0;
for (; i < size && progress < -n+1; ++i, ++progress, step(price)) {
price = series[i];
}
for (; i < size && progress < 0; ++i, ++progress, step(price)) {
price = series[i];
trajectory += fabs(price - price[1]);
}
for (; i < size; ++i, ++progress, step(price)) {
price = series[i];
trajectory += fabs(price - price[1]);
TI_REAL net_change = fabs(price - price[n]);
*er++ = net_change ? net_change / trajectory : 0;
trajectory -= fabs(price[n-1] - price[n]);
}
ptr->progress = progress;
ptr->state.trajectory = trajectory;
return TI_OKAY;
}