-
Notifications
You must be signed in to change notification settings - Fork 693
/
ParabolicSarIndicator.java
249 lines (217 loc) · 9.12 KB
/
ParabolicSarIndicator.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2023 Ta4j Organization & respective
* authors (see AUTHORS)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ta4j.core.indicators;
import static org.ta4j.core.num.NaN.NaN;
import java.util.HashMap;
import java.util.Map;
import org.ta4j.core.BarSeries;
import org.ta4j.core.indicators.helpers.HighPriceIndicator;
import org.ta4j.core.indicators.helpers.HighestValueIndicator;
import org.ta4j.core.indicators.helpers.LowPriceIndicator;
import org.ta4j.core.indicators.helpers.LowestValueIndicator;
import org.ta4j.core.num.Num;
/**
* Parabolic SAR indicator.
*
* @see <a href=
* "https://www.investopedia.com/trading/introduction-to-parabolic-sar/">
* https://www.investopedia.com/trading/introduction-to-parabolic-sar/</a>
* @see <a href="https://www.investopedia.com/terms/p/parabolicindicator.asp">
* https://www.investopedia.com/terms/p/parabolicindicator.asp</a>
*/
public class ParabolicSarIndicator extends RecursiveCachedIndicator<Num> {
private final LowPriceIndicator lowPriceIndicator;
private final HighPriceIndicator highPriceIndicator;
private final Num maxAcceleration;
private final Num accelerationStart;
private final Num accelerationIncrement;
private final Map<Integer, Boolean> isUpTrendMap = new HashMap<>();
private final Map<Integer, Num> lastExtreme = new HashMap<>();
private final Map<Integer, Num> lastAf = new HashMap<>();
/**
* If series have removed bars, first actual bar won't have 0 index.
*/
private int seriesStartIndex = getBarSeries().getBeginIndex();
/**
* Constructor with:
*
* <ul>
* <li>{@code aF} = 0.02
* <li>{@code maxA} = 0.2
* <li>{@code increment} = 0.02
* </ul>
*
* @param series the bar series for this indicator
*/
public ParabolicSarIndicator(BarSeries series) {
this(series, series.numOf(0.02), series.numOf(0.2), series.numOf(0.02));
}
/**
* Constructor with {@code increment} = 0.02.
*
* @param series the bar series for this indicator
* @param aF acceleration factor
* @param maxA maximum acceleration
*/
public ParabolicSarIndicator(BarSeries series, Num aF, Num maxA) {
this(series, aF, maxA, series.numOf(0.02));
}
/**
* Constructor.
*
* @param series the bar series for this indicator
* @param aF acceleration factor (usually 0.02)
* @param maxA maximum acceleration (usually 0.2)
* @param increment the increment step (usually 0.02)
*/
public ParabolicSarIndicator(BarSeries series, Num aF, Num maxA, Num increment) {
super(series);
this.lowPriceIndicator = new LowPriceIndicator(series);
this.highPriceIndicator = new HighPriceIndicator(series);
this.maxAcceleration = maxA;
this.accelerationStart = aF;
this.accelerationIncrement = increment;
}
@Override
protected Num calculate(int index) {
lastExtreme.clear();
lastAf.clear();
isUpTrendMap.clear();
// Caching of this indicator value calculation is essential for performance!
//
// clear the maps and recalculate the values for start to index
// the internal calculations until the previous index will fill the
// required maps for the acceleration factor, the trend direction and the
// last extreme value.
// Cache doesn't require more than current and previous values.
if (index < getBarSeries().getBeginIndex()) {
return NaN;
}
seriesStartIndex = getBarSeries().getRemovedBarsCount();
if (index < seriesStartIndex) {
index = seriesStartIndex;
}
for (int i = seriesStartIndex; i < index; i++) {
calculateInternal(i);
}
return calculateInternal(index);
}
private Num calculateInternal(int index) {
Num sar = NaN;
boolean is_up_trend;
if (index == seriesStartIndex) {
lastExtreme.put(index, getBarSeries().getBar(index).getClosePrice());
lastAf.put(index, zero());
isUpTrendMap.put(index, false);
return sar; // no trend detection possible for the first value
} else if (index == seriesStartIndex + 1) { // start trend detection
is_up_trend = defineUpTrend(index);
lastAf.put(index, accelerationStart);
isUpTrendMap.put(index, is_up_trend);
if (is_up_trend) { // up trend
sar = new LowestValueIndicator(lowPriceIndicator, 2).getValue(index - 1); // put the lowest low value of
// two
lastExtreme.put(index, new HighestValueIndicator(highPriceIndicator, 2).getValue(index - 1));
} else { // down trend
sar = new HighestValueIndicator(highPriceIndicator, 2).getValue(index - 1); // put the highest high
// value of
lastExtreme.put(index, new LowestValueIndicator(lowPriceIndicator, 2).getValue(index - 1));
}
return sar;
}
Num priorSar = getValue(index - 1);
is_up_trend = isUpTrendMap.get(index - 1);
Num currentExtremePoint = lastExtreme.get(index - 1);
Num cur_high = highPriceIndicator.getValue(index);
Num cur_low = lowPriceIndicator.getValue(index);
Num cur_af = lastAf.get(index - 1);
sar = priorSar.plus(cur_af.multipliedBy((currentExtremePoint.minus(priorSar))));
if (is_up_trend) { // if up trend
if (cur_low.isLessThan(sar)) { // check if sar touches the low price
sar = currentExtremePoint;
lastAf.put(index, accelerationStart);
lastExtreme.put(index, cur_low);
is_up_trend = false;
} else { // up trend is going on
if (cur_high.isGreaterThan(currentExtremePoint)) {
currentExtremePoint = cur_high;
cur_af = incrementAcceleration(index);
}
lastExtreme.put(index, currentExtremePoint);
lastAf.put(index, cur_af);
}
} else { // downtrend
if (cur_high.isGreaterThanOrEqual(sar)) { // check if switch to up trend
sar = currentExtremePoint;
lastAf.put(index, accelerationStart);
lastExtreme.put(index, cur_high);
is_up_trend = true;
} else { // down trend io going on
if (cur_low.isLessThan(currentExtremePoint)) {
currentExtremePoint = cur_low;
cur_af = incrementAcceleration(index);
}
lastExtreme.put(index, currentExtremePoint);
lastAf.put(index, cur_af);
}
}
if (is_up_trend) {
Num lowestPriceOfTwoPreviousBars = new LowestValueIndicator(lowPriceIndicator, 2).getValue(index - 1);
if (sar.isGreaterThan(lowestPriceOfTwoPreviousBars)) {
sar = lowestPriceOfTwoPreviousBars;
}
} else {
Num highestPriceOfTwoPreviousBars = new HighestValueIndicator(highPriceIndicator, 2).getValue(index - 1);
if (sar.isLessThan(highestPriceOfTwoPreviousBars)) {
sar = highestPriceOfTwoPreviousBars;
}
}
isUpTrendMap.put(index, is_up_trend);
return sar;
}
@Override
public int getUnstableBars() {
return 0;
}
private boolean defineUpTrend(final int barIndex) {
if (barIndex - 1 < seriesStartIndex) {
return false;
} else {
return getBarSeries().getBar(barIndex - 1)
.getClosePrice()
.isLessThan(getBarSeries().getBar(barIndex).getClosePrice());
}
}
/**
* Increments the acceleration factor.
*/
private Num incrementAcceleration(int index) {
Num cur_af = lastAf.get(index - 1);
cur_af = cur_af.plus(accelerationIncrement);
if (cur_af.isGreaterThan(maxAcceleration)) {
cur_af = maxAcceleration;
}
return cur_af;
}
}