Search in sources :

Example 66 with Bar

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

the class ParabolicSarIndicatorTest method startUpAndDownTrendTest.

@Test
public void startUpAndDownTrendTest() {
    List<Bar> bars = new ArrayList<Bar>();
    bars.add(new MockBar(0, 75.1, 74.06, 75.11));
    bars.add(new MockBar(0, 75.9, 76.030000, 74.640000));
    bars.add(new MockBar(0, 75.24, 76.269900, 75.060000));
    bars.add(new MockBar(0, 75.17, 75.280000, 74.500000));
    bars.add(new MockBar(0, 74.6, 75.310000, 74.540000));
    bars.add(new MockBar(0, 74.1, 75.467000, 74.010000));
    bars.add(new MockBar(0, 73.740000, 74.700000, 73.546000));
    bars.add(new MockBar(0, 73.390000, 73.830000, 72.720000));
    bars.add(new MockBar(0, 73.25, 73.890000, 72.86));
    bars.add(new MockBar(0, 74.36, 74.410000, 73, 26));
    bars.add(new MockBar(0, 76.510000, 76.830000, 74.820000));
    bars.add(new MockBar(0, 75.590000, 76.850000, 74.540000));
    bars.add(new MockBar(0, 75.910000, 76.960000, 75.510000));
    bars.add(new MockBar(0, 74.610000, 77.070000, 74.560000));
    bars.add(new MockBar(0, 75.330000, 75.530000, 74.010000));
    bars.add(new MockBar(0, 75.010000, 75.500000, 74.510000));
    bars.add(new MockBar(0, 75.620000, 76.210000, 75.250000));
    bars.add(new MockBar(0, 76.040000, 76.460000, 75.092800));
    bars.add(new MockBar(0, 76.450000, 76.450000, 75.435000));
    bars.add(new MockBar(0, 76.260000, 76.470000, 75.840000));
    bars.add(new MockBar(0, 76.850000, 77.000000, 76.190000));
    ParabolicSarIndicator sar = new ParabolicSarIndicator(new MockTimeSeries(bars));
    assertEquals(sar.getValue(0).toString(), "NaN");
    assertDecimalEquals(sar.getValue(1), 74.640000000000000568434188608080);
    // start with up trend
    assertDecimalEquals(sar.getValue(2), 74.640000000000000568434188608080);
    // switch to downtrend
    assertDecimalEquals(sar.getValue(3), 76.269900000000006912159733474255);
    // hold trend...
    assertDecimalEquals(sar.getValue(4), 76.234502000000006773916538804770);
    assertDecimalEquals(sar.getValue(5), 76.200611960000006763493729522452);
    assertDecimalEquals(sar.getValue(6), 76.112987481600006697590288240463);
    assertDecimalEquals(sar.getValue(7), 75.958968232704006684543855953962);
    assertDecimalEquals(sar.getValue(8), 75.699850774087686058830877300352);
    // switch to up trend
    assertDecimalEquals(sar.getValue(9), 75.461462712160671083174936939031);
    // hold trend
    assertDecimalEquals(sar.getValue(10), 72.719999999999998863131622783840);
    assertDecimalEquals(sar.getValue(11), 72.802199999999998851762939011678);
    assertDecimalEquals(sar.getValue(12), 72.964111999999998670318746007979);
    assertDecimalEquals(sar.getValue(13), 73.203865279999998374933056766167);
    assertDecimalEquals(sar.getValue(14), 73.513156057599997959241591161117);
    assertDecimalEquals(sar.getValue(15), 73.797703572991997576805442804471);
    assertDecimalEquals(sar.getValue(16), 74.059487287152637224964186316356);
    assertDecimalEquals(sar.getValue(17), 74.300328304180425701270230347291);
    assertDecimalEquals(sar.getValue(18), 74.521902039845991099471790855751);
    assertDecimalEquals(sar.getValue(19), 74.725749876658311265817226523534);
    assertDecimalEquals(sar.getValue(20), 74.913289886525645818855027337894);
}
Also used : MockBar(org.ta4j.core.mocks.MockBar) Bar(org.ta4j.core.Bar) MockBar(org.ta4j.core.mocks.MockBar) ArrayList(java.util.ArrayList) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 67 with Bar

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

the class FibonacciReversalIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    List<Integer> barsOfPreviousPeriod = pivotPointIndicator.getBarsOfPreviousPeriod(index);
    if (barsOfPreviousPeriod.isEmpty())
        return Decimal.NaN;
    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);
    }
    if (fibReversalTyp == FibReversalTyp.RESISTANCE) {
        return pivotPointIndicator.getValue(index).plus(fibonacciFactor.multipliedBy(high.minus(low)));
    }
    return pivotPointIndicator.getValue(index).minus(fibonacciFactor.multipliedBy(high.minus(low)));
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Example 68 with Bar

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

the class StandardReversalIndicator method calculateR2.

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

Example 69 with Bar

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

the class StandardReversalIndicator method calculateS2.

private Decimal calculateS2(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 pivotPointIndicator.getValue(index).minus((high.minus(low)));
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Example 70 with Bar

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

the class ThreeBlackCrowsIndicator method isBlackCrow.

/**
 * @param index the current bar/candle index
 * @return true if the current bar/candle is a black crow, false otherwise
 */
private boolean isBlackCrow(int index) {
    Bar prevBar = series.getBar(index - 1);
    Bar currBar = series.getBar(index);
    if (currBar.isBearish()) {
        if (prevBar.isBullish()) {
            // First crow case
            return hasVeryShortLowerShadow(index) && currBar.getOpenPrice().isLessThan(prevBar.getMaxPrice());
        } else {
            return hasVeryShortLowerShadow(index) && isDeclining(index);
        }
    }
    return false;
}
Also used : Bar(org.ta4j.core.Bar)

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