-
Notifications
You must be signed in to change notification settings - Fork 708
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
Candle indicators #1146
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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?