Skip to content

Commit

Permalink
minor cleanup of new recent swing indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCookieLab committed Nov 29, 2023
1 parent 339a6f7 commit 1b23b63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
public class RecentSwingHighIndicator extends CachedIndicator<Num> {

/**
* 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
Expand All @@ -57,7 +58,8 @@ public RecentSwingHighIndicator(BarSeries series, int surroundingBars) {
}

/**
* * g
* *
* Convenience constructor defaulting surroundingBars to 2
*
* @param series
*/
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class RecentSwingLowIndicator extends CachedIndicator<Num> {
private final int surroundingBars;

/**
* *
* Full constructor
*
* @param series
* @param surroundingBars
Expand All @@ -57,7 +57,7 @@ public RecentSwingLowIndicator(BarSeries series, int surroundingBars) {
}

/**
* * g
* Convenience constructor defaulting surroundingBars to 2
*
* @param series
*/
Expand All @@ -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;
Expand Down

0 comments on commit 1b23b63

Please sign in to comment.