-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
616f850
commit 339a6f7
Showing
4 changed files
with
195 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
ta4j-core/src/main/java/org/ta4j/core/indicators/RecentSwingLowIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* 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 org.ta4j.core.Bar; | ||
import org.ta4j.core.BarSeries; | ||
import org.ta4j.core.num.NaN; | ||
import static org.ta4j.core.num.NaN.NaN; | ||
import org.ta4j.core.num.Num; | ||
|
||
/** | ||
* Recent Swing Low Indicator. | ||
*/ | ||
public class RecentSwingLowIndicator extends CachedIndicator<Num> { | ||
|
||
/** | ||
* A swing low is a bar with a lower low 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; | ||
|
||
/** | ||
* * | ||
* | ||
* @param series | ||
* @param surroundingBars | ||
*/ | ||
public RecentSwingLowIndicator(BarSeries series, int surroundingBars) { | ||
super(series); | ||
|
||
if (surroundingBars <= 0) { | ||
throw new IllegalArgumentException("surroundingBars must be greater than 0"); | ||
} | ||
this.surroundingBars = surroundingBars; | ||
} | ||
|
||
/** | ||
* * g | ||
* | ||
* @param series | ||
*/ | ||
public RecentSwingLowIndicator(BarSeries series) { | ||
this(series, 2); | ||
} | ||
|
||
/** | ||
* Calculates the value of the most recent swing low | ||
* | ||
* @param index the bar index | ||
* @return the value of the most recent swing low, otherwise {@link NaN} | ||
*/ | ||
@Override | ||
protected Num calculate(int index) { | ||
if (index < surroundingBars) { | ||
return NaN; | ||
} | ||
|
||
for (int i = index - 1; i >= surroundingBars; i--) { | ||
boolean isSwingLow = true; | ||
Bar currentBar = getBarSeries().getBar(i); | ||
|
||
for (int j = 1; j <= surroundingBars; j++) { | ||
if (i + j > getBarSeries().getEndIndex() | ||
|| currentBar.getLowPrice().isGreaterThanOrEqual(getBarSeries().getBar(i - j).getLowPrice()) | ||
|| currentBar.getLowPrice().isGreaterThanOrEqual(getBarSeries().getBar(i + j).getLowPrice())) { | ||
isSwingLow = false; | ||
break; | ||
} | ||
} | ||
|
||
if (isSwingLow) { | ||
return currentBar.getLowPrice(); | ||
} | ||
} | ||
|
||
return NaN; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
ta4j-core/src/test/java/org/ta4j/core/indicators/RecentSwingLowIndicatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* 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 java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.ta4j.core.BarSeries; | ||
import org.ta4j.core.Indicator; | ||
import org.ta4j.core.mocks.MockBarSeries; | ||
import org.ta4j.core.num.NaN; | ||
import org.ta4j.core.num.Num; | ||
|
||
import java.util.function.Function; | ||
|
||
import org.ta4j.core.Bar; | ||
import static org.ta4j.core.TestUtils.assertNumEquals; | ||
import org.ta4j.core.mocks.MockBar; | ||
|
||
public class RecentSwingLowIndicatorTest extends AbstractIndicatorTest<Indicator<Num>, Num> { | ||
|
||
BarSeries series; | ||
|
||
public RecentSwingLowIndicatorTest(Function<Number, Num> numFunction) { | ||
super(numFunction); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
List<Bar> bars = new ArrayList<>(); | ||
bars.add(new MockBar(10, 10, 10, 10, numFunction)); | ||
bars.add(new MockBar(9, 9, 9, 9, numFunction)); | ||
bars.add(new MockBar(8, 8, 8, 8, numFunction)); | ||
bars.add(new MockBar(9, 9, 9, 9, numFunction)); | ||
bars.add(new MockBar(10, 10, 10, 10, numFunction)); | ||
bars.add(new MockBar(7, 7, 7, 7, numFunction)); | ||
bars.add(new MockBar(10, 10, 10, 10, numFunction)); | ||
this.series = new MockBarSeries(bars); | ||
} | ||
|
||
@Test | ||
public void testCalculate_BelowSurroundingBars_ReturnsNaN() { | ||
RecentSwingLowIndicator swingLowIndicator = new RecentSwingLowIndicator(series, 2); | ||
|
||
assertNumEquals(NaN.NaN, swingLowIndicator.getValue(0)); | ||
assertNumEquals(NaN.NaN, swingLowIndicator.getValue(1)); | ||
assertNumEquals(NaN.NaN, swingLowIndicator.getValue(2)); | ||
} | ||
|
||
@Test | ||
public void testCalculate_AboveSurroundingBars_ReturnsValue() { | ||
RecentSwingLowIndicator swingLowIndicator = new RecentSwingLowIndicator(series, 2); | ||
|
||
assertNumEquals(8, swingLowIndicator.getValue(3)); | ||
assertNumEquals(8, swingLowIndicator.getValue(4)); | ||
assertNumEquals(8, swingLowIndicator.getValue(5)); | ||
} | ||
|
||
@Test | ||
public void testCalculate_NotEnoughSurroundingBarsAfter_ReturnsPreviousValue() { | ||
RecentSwingLowIndicator swingLowIndicator = new RecentSwingLowIndicator(series, 2); | ||
|
||
assertNumEquals(8, swingLowIndicator.getValue(6)); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testConstructor_SurroundingBarsZero() { | ||
RecentSwingLowIndicator swingLowIndicator = new RecentSwingLowIndicator(series, 0); | ||
} | ||
|
||
} |