From 1b23b6330d415583efea4308500d27387ca873d9 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 29 Nov 2023 13:32:01 -0500 Subject: [PATCH] minor cleanup of new recent swing indicators --- .../indicators/RecentSwingHighIndicator.java | 16 ++++++++++------ .../core/indicators/RecentSwingLowIndicator.java | 10 ++++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingHighIndicator.java b/ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingHighIndicator.java index 12115fd2..625ccc02 100644 --- a/ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingHighIndicator.java +++ b/ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingHighIndicator.java @@ -35,14 +35,15 @@ public class RecentSwingHighIndicator extends CachedIndicator { /** - * A swing high is a bar with a higher high than the bars both before and after - * it. Defines the number of bars to consider on each side (e.g., 2 bars on each - * side). + * A swing high is a bar with a higher high than the bars both before and + * after it. Defines the number of bars to consider on each side (e.g., 2 + * bars on each side). */ private final int surroundingBars; /** * * + * Full constructor * * @param series * @param surroundingBars @@ -57,7 +58,8 @@ public RecentSwingHighIndicator(BarSeries series, int surroundingBars) { } /** - * * g + * * + * Convenience constructor defaulting surroundingBars to 2 * * @param series */ @@ -77,12 +79,14 @@ protected Num calculate(int index) { return NaN; } - for (int i = index - 1; i >= surroundingBars; i--) { + int endIndex = getBarSeries().getEndIndex(); + + for (int i = Math.min(index - 1, endIndex); i >= surroundingBars; i--) { boolean isSwingHigh = true; Bar currentBar = getBarSeries().getBar(i); for (int j = 1; j <= surroundingBars; j++) { - if (i + j > getBarSeries().getEndIndex() + if (i + j > endIndex || i - j < 0 || currentBar.getHighPrice().isLessThanOrEqual(getBarSeries().getBar(i - j).getHighPrice()) || currentBar.getHighPrice().isLessThanOrEqual(getBarSeries().getBar(i + j).getHighPrice())) { isSwingHigh = false; diff --git a/ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingLowIndicator.java b/ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingLowIndicator.java index 2074cca7..e70d94b5 100644 --- a/ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingLowIndicator.java +++ b/ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingLowIndicator.java @@ -42,7 +42,7 @@ public class RecentSwingLowIndicator extends CachedIndicator { private final int surroundingBars; /** - * * + * Full constructor * * @param series * @param surroundingBars @@ -57,7 +57,7 @@ public RecentSwingLowIndicator(BarSeries series, int surroundingBars) { } /** - * * g + * Convenience constructor defaulting surroundingBars to 2 * * @param series */ @@ -77,12 +77,14 @@ protected Num calculate(int index) { return NaN; } - for (int i = index - 1; i >= surroundingBars; i--) { + int endIndex = getBarSeries().getEndIndex(); + + for (int i = Math.min(index - 1, endIndex); i >= surroundingBars; i--) { boolean isSwingLow = true; Bar currentBar = getBarSeries().getBar(i); for (int j = 1; j <= surroundingBars; j++) { - if (i + j > getBarSeries().getEndIndex() + if (i + j > endIndex || i - j < 0 || currentBar.getLowPrice().isGreaterThanOrEqual(getBarSeries().getBar(i - j).getLowPrice()) || currentBar.getLowPrice().isGreaterThanOrEqual(getBarSeries().getBar(i + j).getLowPrice())) { isSwingLow = false;