Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Candle indicators #1146

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog for `ta4j`, roughly following [keepachangelog.com](http://keepachangelog.com/en/1.0.0/) from version 0.9 onwards.

## 0.16 (unreleased)
- **Implemented trend reversal candles**
- **Fixed NaN in DXIndicator, MinusDIIndicator, PlusDIIndicator if there is no trend**

### Breaking
- **Upgraded to Java 11**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* Part of the Directional Movement System.
*
* @see <a href=
* "https://www.investopedia.com/terms/a/adx.asp">https://www.investopedia.com/terms/a/adx.asp</a>
* "https://www.investopedia.com/articles/trading/07/adx-trend-indicator.asp">https://www.investopedia.com/articles/trading/07/adx-trend-indicator.asp</a>
*/
public class ADXIndicator extends CachedIndicator<Num> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public DXIndicator(BarSeries series, int barCount) {
protected Num calculate(int index) {
Num pdiValue = plusDIIndicator.getValue(index);
Num mdiValue = minusDIIndicator.getValue(index);
if (pdiValue.plus(mdiValue).equals(zero())) {
final var sum = pdiValue.plus(mdiValue);
if (sum.equals(zero())) {
return zero();
}
return pdiValue.minus(mdiValue).abs().dividedBy(pdiValue.plus(mdiValue)).multipliedBy(hundred());
return pdiValue.minus(mdiValue).abs().dividedBy(sum).multipliedBy(hundred());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public MinusDIIndicator(BarSeries series, int barCount) {

@Override
protected Num calculate(int index) {
return avgMinusDMIndicator.getValue(index).dividedBy(atrIndicator.getValue(index)).multipliedBy(hundred());
final var atrIndicatorValue = atrIndicator.getValue(index);
if (atrIndicatorValue.equals(zero())) {
return zero();
}
return avgMinusDMIndicator.getValue(index).dividedBy(atrIndicatorValue).multipliedBy(hundred());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected Num calculate(int index) {
if (downMove.isGreaterThan(upMove) && downMove.isGreaterThan(zero())) {
return downMove;
} else {
return numOf(0);
return zero();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public PlusDIIndicator(BarSeries series, int barCount) {

@Override
protected Num calculate(int index) {
return avgPlusDMIndicator.getValue(index).dividedBy(atrIndicator.getValue(index)).multipliedBy(numOf(100));
final var atrIndicatorValue = atrIndicator.getValue(index);
if (atrIndicatorValue.equals(zero())) {
return zero();
}
return avgPlusDMIndicator.getValue(index).dividedBy(atrIndicatorValue).multipliedBy(numOf(100));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* 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.candles;

import org.ta4j.core.BarSeries;
import org.ta4j.core.indicators.CachedIndicator;
import org.ta4j.core.indicators.trend.DownTrendIndicator;

/**
* Hammer candle indicator.
*
* @see <a href="https://www.investopedia.com/terms/h/hammer.asp">
* https://www.investopedia.com/terms/h/hammer.asp</a>
*/
public class HammerIndicator extends CachedIndicator<Boolean> {

private static final double BODY_LENGTH_TO_BOTTOM_WICK_COEFFICIENT = 2d;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be constructor params rather than hardcoded?

private static final double BODY_LENGTH_TO_UPPER_WICK_COEFFICIENT = 1d;

private final RealBodyIndicator realBodyIndicator;
private final DownTrendIndicator trendIndicator;

/**
* Constructor.
*
* @param series the bar series
*/
public HammerIndicator(final BarSeries series) {
super(series);
this.realBodyIndicator = new RealBodyIndicator(series);
this.trendIndicator = new DownTrendIndicator(series);
}

@Override
protected Boolean calculate(final int index) {
final var bar = getBarSeries().getBar(index);
final var openPrice = bar.getOpenPrice();
final var closePrice = bar.getClosePrice();
final var lowPrice = bar.getLowPrice();
final var highPrice = bar.getHighPrice();

final var bodyHeight = this.realBodyIndicator.getValue(index).abs();

final var upperBodyBoundary = openPrice.max(closePrice);
final var bottomBodyBoundary = openPrice.min(closePrice);
final var bottomWickHeight = bottomBodyBoundary.minus(lowPrice);
final var upperWickHeight = highPrice.minus(upperBodyBoundary);

return bottomWickHeight.dividedBy(bodyHeight).isGreaterThan(numOf(BODY_LENGTH_TO_BOTTOM_WICK_COEFFICIENT))
&& upperWickHeight.dividedBy(bodyHeight).isLessThanOrEqual(numOf(BODY_LENGTH_TO_UPPER_WICK_COEFFICIENT))
&& this.trendIndicator.getValue(index);
}

@Override
public int getUnstableBars() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* 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.candles;

import org.ta4j.core.BarSeries;
import org.ta4j.core.indicators.CachedIndicator;
import org.ta4j.core.indicators.trend.UpTrendIndicator;

/**
* Hanging man candle indicator.
*
* @see <a href="https://www.investopedia.com/terms/h/hangingman.asp">
* https://www.investopedia.com/terms/h/hangingman.asp</a>
*/
public class HangingManIndicator extends CachedIndicator<Boolean> {

private static final double BODY_LENGTH_TO_BOTTOM_WICK_COEFFICIENT = 2d;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Perhaps these values can be used as defaults in a no-arg constructor.

private static final double BODY_LENGTH_TO_UPPER_WICK_COEFFICIENT = 1d;

private final RealBodyIndicator realBodyIndicator;
private final UpTrendIndicator trendIndicator;

/**
* Constructor.
*
* @param series the bar series
*/
public HangingManIndicator(final BarSeries series) {
super(series);
this.realBodyIndicator = new RealBodyIndicator(series);
this.trendIndicator = new UpTrendIndicator(series);
}

@Override
protected Boolean calculate(final int index) {
final var bar = getBarSeries().getBar(index);
final var openPrice = bar.getOpenPrice();
final var closePrice = bar.getClosePrice();
final var lowPrice = bar.getLowPrice();
final var highPrice = bar.getHighPrice();

final var bodyHeight = this.realBodyIndicator.getValue(index).abs();

final var upperBodyBoundary = openPrice.max(closePrice);
final var bottomBodyBoundary = openPrice.min(closePrice);
final var bottomWickHeight = bottomBodyBoundary.minus(lowPrice);
final var upperWickHeight = highPrice.minus(upperBodyBoundary);

return bottomWickHeight.dividedBy(bodyHeight).isGreaterThan(numOf(BODY_LENGTH_TO_BOTTOM_WICK_COEFFICIENT))
&& upperWickHeight.dividedBy(bodyHeight).isLessThanOrEqual(numOf(BODY_LENGTH_TO_UPPER_WICK_COEFFICIENT))
&& this.trendIndicator.getValue(index);
}

@Override
public int getUnstableBars() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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.candles;

import org.ta4j.core.BarSeries;
import org.ta4j.core.indicators.CachedIndicator;
import org.ta4j.core.indicators.trend.DownTrendIndicator;

/**
* Inverted hammer candle indicator.
*/
public class InvertedHammerIndicator extends CachedIndicator<Boolean> {

private static final double BODY_LENGTH_TO_BOTTOM_WICK_COEFFICIENT = 2d;
private static final double BODY_LENGTH_TO_UPPER_WICK_COEFFICIENT = 1d;

private final RealBodyIndicator realBodyIndicator;
private final DownTrendIndicator trendIndicator;

/**
* Constructor.
*
* @param series the bar series
*/
public InvertedHammerIndicator(final BarSeries series) {
super(series);
this.realBodyIndicator = new RealBodyIndicator(series);
this.trendIndicator = new DownTrendIndicator(series);
}

@Override
protected Boolean calculate(final int index) {
final var bar = getBarSeries().getBar(index);
final var openPrice = bar.getOpenPrice();
final var closePrice = bar.getClosePrice();
final var lowPrice = bar.getLowPrice();
final var highPrice = bar.getHighPrice();

final var bodyHeight = this.realBodyIndicator.getValue(index).abs();

final var upperBodyBoundary = openPrice.max(closePrice);
final var bottomBodyBoundary = openPrice.min(closePrice);
final var bottomWickHeight = bottomBodyBoundary.minus(lowPrice);
final var upperWickHeight = highPrice.minus(upperBodyBoundary);

return upperWickHeight.dividedBy(bodyHeight).isGreaterThan(numOf(BODY_LENGTH_TO_BOTTOM_WICK_COEFFICIENT))
&& bottomWickHeight.dividedBy(bodyHeight)
.isLessThanOrEqual(numOf(BODY_LENGTH_TO_UPPER_WICK_COEFFICIENT))
&& this.trendIndicator.getValue(index);
}

@Override
public int getUnstableBars() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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.trend;

import org.ta4j.core.BarSeries;
import org.ta4j.core.indicators.AbstractIndicator;
import org.ta4j.core.indicators.adx.ADXIndicator;
import org.ta4j.core.indicators.adx.MinusDIIndicator;
import org.ta4j.core.indicators.adx.PlusDIIndicator;

public class DownTrendIndicator extends AbstractIndicator<Boolean> {

private static final int UNSTABLE_BARS = 5;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The choice of 5 here is unnecessarily opinionated no? How about as a constructor param?


private final ADXIndicator directionStrengthIndicator;
private final MinusDIIndicator minusDIIndicator;
private final PlusDIIndicator plusDIIndicator;

public DownTrendIndicator(final BarSeries series) {
super(series);
this.directionStrengthIndicator = new ADXIndicator(series, UNSTABLE_BARS);
this.minusDIIndicator = new MinusDIIndicator(series, UNSTABLE_BARS);
this.plusDIIndicator = new PlusDIIndicator(series, UNSTABLE_BARS);
}

@Override
public Boolean getValue(final int index) {
// calculate trend excluding this bar
final var previousIndex = index - 1;
return this.directionStrengthIndicator.getValue(index).isGreaterThan(numOf(25))
&& this.minusDIIndicator.getValue(previousIndex)
.isGreaterThan(this.plusDIIndicator.getValue(previousIndex));
}

@Override
public int getUnstableBars() {
return UNSTABLE_BARS;
}
}
Loading