Skip to content

Commit 1101dbe

Browse files
authored
Merge pull request ta4j#1251 from sgflt/ta4j#1250-parabolic-sar-optimization
ta4j#150 Use highest and lowest indicators as instance variables
2 parents 64896b0 + e7f0f8f commit 1101dbe

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

ta4j-core/src/main/java/org/ta4j/core/indicators/ParabolicSarIndicator.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
public class ParabolicSarIndicator extends RecursiveCachedIndicator<Num> {
4848

4949
private final LowPriceIndicator lowPriceIndicator;
50+
private final LowestValueIndicator lowestValueIndicator;
5051
private final HighPriceIndicator highPriceIndicator;
52+
private final HighestValueIndicator highestValueIndicator;
5153

5254
private final Num maxAcceleration;
5355
private final Num accelerationStart;
@@ -99,7 +101,9 @@ public ParabolicSarIndicator(BarSeries series, Num aF, Num maxA) {
99101
public ParabolicSarIndicator(BarSeries series, Num aF, Num maxA, Num increment) {
100102
super(series);
101103
this.lowPriceIndicator = new LowPriceIndicator(series);
104+
this.lowestValueIndicator = new LowestValueIndicator(lowPriceIndicator, 2);
102105
this.highPriceIndicator = new HighPriceIndicator(series);
106+
this.highestValueIndicator = new HighestValueIndicator(highPriceIndicator, 2);
103107
this.maxAcceleration = maxA;
104108
this.accelerationStart = aF;
105109
this.accelerationIncrement = increment;
@@ -149,13 +153,13 @@ private Num calculateInternal(int index) {
149153
isUpTrendMap.put(index, is_up_trend);
150154

151155
if (is_up_trend) { // up trend
152-
sar = new LowestValueIndicator(lowPriceIndicator, 2).getValue(index - 1); // put the lowest low value of
156+
sar = lowestValueIndicator.getValue(index - 1); // put the lowest low value of
153157
// two
154-
lastExtreme.put(index, new HighestValueIndicator(highPriceIndicator, 2).getValue(index - 1));
158+
lastExtreme.put(index, highestValueIndicator.getValue(index - 1));
155159
} else { // down trend
156-
sar = new HighestValueIndicator(highPriceIndicator, 2).getValue(index - 1); // put the highest high
160+
sar = highestValueIndicator.getValue(index - 1); // put the highest high
157161
// value of
158-
lastExtreme.put(index, new LowestValueIndicator(lowPriceIndicator, 2).getValue(index - 1));
162+
lastExtreme.put(index, lowestValueIndicator.getValue(index - 1));
159163
}
160164
return sar;
161165
}
@@ -206,12 +210,12 @@ private Num calculateInternal(int index) {
206210
}
207211

208212
if (is_up_trend) {
209-
Num lowestPriceOfTwoPreviousBars = new LowestValueIndicator(lowPriceIndicator, 2).getValue(index - 1);
213+
Num lowestPriceOfTwoPreviousBars = lowestValueIndicator.getValue(index - 1);
210214
if (sar.isGreaterThan(lowestPriceOfTwoPreviousBars)) {
211215
sar = lowestPriceOfTwoPreviousBars;
212216
}
213217
} else {
214-
Num highestPriceOfTwoPreviousBars = new HighestValueIndicator(highPriceIndicator, 2).getValue(index - 1);
218+
Num highestPriceOfTwoPreviousBars = highestValueIndicator.getValue(index - 1);
215219
if (sar.isLessThan(highestPriceOfTwoPreviousBars)) {
216220
sar = highestPriceOfTwoPreviousBars;
217221
}

0 commit comments

Comments
 (0)