-
Notifications
You must be signed in to change notification settings - Fork 11
/
aroonosc.cc
87 lines (69 loc) · 2.13 KB
/
aroonosc.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
/*
* This file is part of tindicators, licensed under GNU LGPL v3.
* Author: Ilya Pikulin <ilya.pikulin@gmail.com>, 2019, 2021
* Author: Lewis Van Winkle <lv@codeplea.com>, 2016-2017
*/
#include "../indicators.h"
int ti_aroonosc_start(TI_REAL const *options) {
return (int)options[0];
}
int ti_aroonosc(int size, TI_REAL const *const *inputs, TI_REAL const *options, TI_REAL *const *outputs) {
const TI_REAL *high = inputs[0];
const TI_REAL *low = inputs[1];
TI_REAL *output = outputs[0];
const int period = (int)options[0];
if (period < 1) return TI_INVALID_OPTION;
if (size <= ti_aroon_start(options)) return TI_OKAY;
const TI_REAL scale = 100.0 / period;
int trail = 0, maxi = -1, mini = -1;
TI_REAL max = high[0];
TI_REAL min = low[0];
int i, j;
for (i = period; i < size; ++i, ++trail) {
/* Maintain highest. */
TI_REAL bar = high[i];
if (maxi < trail) {
maxi = trail;
max = high[maxi];
j = trail;
while(++j <= i) {
bar = high[j];
if (bar >= max) {
max = bar;
maxi = j;
}
}
} else if (bar >= max) {
maxi = i;
max = bar;
}
/* Maintain lowest. */
bar = low[i];
if (mini < trail) {
mini = trail;
min = low[mini];
j = trail;
while(++j <= i) {
bar = low[j];
if (bar <= min) {
min = bar;
mini = j;
}
}
} else if (bar <= min) {
mini = i;
min = bar;
}
/* Calculate the indicator. */
/*
const TI_REAL adown = ((TI_REAL)period - (i-mini)) * scale;
const TI_REAL aup = ((TI_REAL)period - (i-maxi)) * scale;
*output = aup - adown
That simplifies to:
(maxi-mini) * scale
*/
*output++ = (maxi-mini) * scale;
}
assert(output - outputs[0] == size - ti_aroonosc_start(options));
return TI_OKAY;
}