Search in sources :

Example 11 with Bar

use of org.ta4j.core.Bar in project crypto-bot by jnidzwetzki.

the class BarMerger method closeBar.

protected void closeBar() {
    if (prices.isEmpty()) {
        return;
    }
    final double open = prices.get(0);
    final double close = prices.get(prices.size() - 1);
    final double high = prices.stream().mapToDouble(e -> e).max().orElse(-1);
    final double low = prices.stream().mapToDouble(e -> e).min().orElse(-1);
    final Instant i = Instant.ofEpochMilli(timeframeBegin + timeframe.getMilliSeconds() - 1);
    final ZonedDateTime withTimezone = ZonedDateTime.ofInstant(i, Const.BITFINEX_TIMEZONE);
    final Bar bar = new BaseBar(withTimezone, open, high, low, close, totalVolume);
    try {
        tickConsumer.accept(symbol, bar);
    } catch (IllegalArgumentException e) {
    // ignore time shift
    }
    totalVolume = 0;
    prices.clear();
}
Also used : BaseBar(org.ta4j.core.BaseBar) Bar(org.ta4j.core.Bar) ZonedDateTime(java.time.ZonedDateTime) BaseBar(org.ta4j.core.BaseBar) Instant(java.time.Instant)

Example 12 with Bar

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

the class BullishEngulfingIndicator 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.isBearish() && currBar.isBullish()) {
        final Decimal prevOpenPrice = prevBar.getOpenPrice();
        final Decimal prevClosePrice = prevBar.getClosePrice();
        final Decimal currOpenPrice = currBar.getOpenPrice();
        final Decimal currClosePrice = currBar.getClosePrice();
        return currOpenPrice.isLessThan(prevOpenPrice) && currOpenPrice.isLessThan(prevClosePrice) && currClosePrice.isGreaterThan(prevOpenPrice) && currClosePrice.isGreaterThan(prevClosePrice);
    }
    return false;
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Example 13 with Bar

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

the class BullishHaramiIndicator method calculate.

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

Example 14 with Bar

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

the class LowerShadowIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    Bar t = series.getBar(index);
    final Decimal openPrice = t.getOpenPrice();
    final Decimal closePrice = t.getClosePrice();
    if (closePrice.isGreaterThan(openPrice)) {
        // Bullish
        return openPrice.minus(t.getMinPrice());
    } else {
        // Bearish
        return closePrice.minus(t.getMinPrice());
    }
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Example 15 with Bar

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

the class ThreeBlackCrowsIndicator method isDeclining.

/**
 * @param index the current bar/candle index
 * @return true if the current bar/candle is declining, false otherwise
 */
private boolean isDeclining(int index) {
    Bar prevBar = series.getBar(index - 1);
    Bar currBar = series.getBar(index);
    final Decimal prevOpenPrice = prevBar.getOpenPrice();
    final Decimal prevClosePrice = prevBar.getClosePrice();
    final Decimal currOpenPrice = currBar.getOpenPrice();
    final Decimal currClosePrice = currBar.getClosePrice();
    // Opens within the body of the previous candle
    return currOpenPrice.isLessThan(prevOpenPrice) && currOpenPrice.isGreaterThan(prevClosePrice) && // Closes below the previous close price
    currClosePrice.isLessThan(prevClosePrice);
}
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