Skip to content

Commit

Permalink
added recentswinglowindicator
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCookieLab committed Nov 29, 2023
1 parent 616f850 commit 339a6f7
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@
* 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.
*/
/**
* This copy of Woodstox XML processor is licensed under the
* Apache (Software) License, version 2.0 ("the License").
* See the License for details about distribution rights, and the
* specific rights regarding derivate works.
*
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing Woodstox, in file "ASL2.0", under the same directory
* as this file.
*/
package org.ta4j.core.indicators;

import org.ta4j.core.Bar;
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,7 @@
* 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.
*/
/**
* This copy of Woodstox XML processor is licensed under the
* Apache (Software) License, version 2.0 ("the License").
* See the License for details about distribution rights, and the
* specific rights regarding derivate works.
*
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing Woodstox, in file "ASL2.0", under the same directory
* as this file.
*/

package org.ta4j.core.indicators;

import java.util.ArrayList;
Expand Down
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);
}

}

0 comments on commit 339a6f7

Please sign in to comment.