Search in sources :

Example 16 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class ThreeWhiteSoldiersIndicator method isWhiteSoldier.

/**
 * @param index the current bar/candle index
 * @return true if the current bar/candle is a white soldier, false otherwise
 */
private boolean isWhiteSoldier(int index) {
    Bar prevBar = series.getBar(index - 1);
    Bar currBar = series.getBar(index);
    if (currBar.isBullish()) {
        if (prevBar.isBearish()) {
            // First soldier case
            return hasVeryShortUpperShadow(index) && currBar.getOpenPrice().isGreaterThan(prevBar.getMinPrice());
        } else {
            return hasVeryShortUpperShadow(index) && isGrowing(index);
        }
    }
    return false;
}
Also used : Bar(org.ta4j.core.Bar)

Example 17 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class BearishEngulfingIndicator method calculate.

@Override
protected Boolean calculate(int index) {
    if (index < 1) {
        // Engulfing is a 2-candle pattern
        return false;
    }
    Bar prevBar = series.getBar(index - 1);
    Bar currBar = series.getBar(index);
    if (prevBar.isBullish() && currBar.isBearish()) {
        final Decimal prevOpenPrice = prevBar.getOpenPrice();
        final Decimal prevClosePrice = prevBar.getClosePrice();
        final Decimal currOpenPrice = currBar.getOpenPrice();
        final Decimal currClosePrice = currBar.getClosePrice();
        return currOpenPrice.isGreaterThan(prevOpenPrice) && currOpenPrice.isGreaterThan(prevClosePrice) && currClosePrice.isLessThan(prevOpenPrice) && currClosePrice.isLessThan(prevClosePrice);
    }
    return false;
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Example 18 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class PivotPointIndicator method calcPivotPoint.

private Decimal calcPivotPoint(List<Integer> barsOfPreviousPeriod) {
    if (barsOfPreviousPeriod.isEmpty())
        return Decimal.NaN;
    Bar bar = getTimeSeries().getBar(barsOfPreviousPeriod.get(0));
    Decimal close = bar.getClosePrice();
    Decimal high = bar.getMaxPrice();
    Decimal low = bar.getMinPrice();
    for (int i : barsOfPreviousPeriod) {
        high = (getTimeSeries().getBar(i).getMaxPrice()).max(high);
        low = (getTimeSeries().getBar(i).getMinPrice()).min(low);
    }
    return (high.plus(low).plus(close)).dividedBy(Decimal.THREE);
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Example 19 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class PivotPointIndicator method getBarsOfPreviousPeriod.

/**
 * Calculates the indices of the bars of the previous period
 * @param index index of the current bar
 * @return list of indices of the bars of the previous period
 */
public List<Integer> getBarsOfPreviousPeriod(int index) {
    List<Integer> previousBars = new ArrayList<>();
    if (timeLevel == TimeLevel.BARBASED) {
        previousBars.add(Math.max(0, index - 1));
        return previousBars;
    }
    if (index == 0) {
        return previousBars;
    }
    final Bar currentBar = getTimeSeries().getBar(index);
    // step back while bar-1 in same period (day, week, etc):
    while (index - 1 >= getTimeSeries().getBeginIndex() && getPeriod(getTimeSeries().getBar(index - 1)) == getPeriod(currentBar)) {
        index--;
    }
    // index = last bar in same period, index-1 = first bar in previous period
    long previousPeriod = getPreviousPeriod(currentBar, index - 1);
    while (index - 1 >= getTimeSeries().getBeginIndex() && getPeriod(getTimeSeries().getBar(index - 1)) == previousPeriod) {
        // while bar-n in previous period
        index--;
        previousBars.add(index);
    }
    return previousBars;
}
Also used : Bar(org.ta4j.core.Bar) ArrayList(java.util.ArrayList)

Example 20 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class StandardReversalIndicator method calculateS3.

private Decimal calculateS3(List<Integer> barsOfPreviousPeriod, int index) {
    Bar bar = getTimeSeries().getBar(barsOfPreviousPeriod.get(0));
    Decimal high = bar.getMaxPrice();
    Decimal low = bar.getMinPrice();
    for (int i : barsOfPreviousPeriod) {
        high = (getTimeSeries().getBar(i).getMaxPrice()).max(high);
        low = (getTimeSeries().getBar(i).getMinPrice()).min(low);
    }
    return low.minus(Decimal.TWO.multipliedBy((high.minus(pivotPointIndicator.getValue(index)))));
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Aggregations

Bar (org.ta4j.core.Bar)104 ArrayList (java.util.ArrayList)60 MockBar (org.ta4j.core.mocks.MockBar)48 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)42 Before (org.junit.Before)37 Test (org.junit.Test)33 Decimal (org.ta4j.core.Decimal)20 IOException (java.io.IOException)16 BaseTimeSeries (org.ta4j.core.BaseTimeSeries)16 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)15 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)15 ZonedDateTime (java.time.ZonedDateTime)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 BiConsumer (java.util.function.BiConsumer)15 BarMerger (com.github.jnidzwetzki.cryptobot.util.BarMerger)14 ParseException (java.text.ParseException)14 SimpleDateFormat (java.text.SimpleDateFormat)14 Assert (org.junit.Assert)14 BaseBar (org.ta4j.core.BaseBar)12 TimeSeries (org.ta4j.core.TimeSeries)9