Search in sources :

Example 21 with TimeSeries

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

the class LinearTransactionCostCriterionTest method externalData.

@Test
public void externalData() throws Exception {
    TimeSeries xlsSeries = xls.getSeries();
    TradingRecord xlsTradingRecord = xls.getTradingRecord();
    double value;
    value = getCriterion(1000d, 0.005, 0.2).calculate(xlsSeries, xlsTradingRecord);
    assertEquals(xls.getFinalCriterionValue(1000d, 0.005, 0.2).doubleValue(), value, TATestsUtils.TA_OFFSET);
    assertEquals(843.5492, value, TATestsUtils.TA_OFFSET);
    value = getCriterion(1000d, 0.1, 1.0).calculate(xlsSeries, xlsTradingRecord);
    assertEquals(xls.getFinalCriterionValue(1000d, 0.1, 1.0).doubleValue(), value, TATestsUtils.TA_OFFSET);
    assertEquals(1122.4410, value, TATestsUtils.TA_OFFSET);
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TradingRecord(org.ta4j.core.TradingRecord) ExternalCriterionTest(org.ta4j.core.ExternalCriterionTest) Test(org.junit.Test)

Example 22 with TimeSeries

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

the class CachedIndicatorTest method getValueOnResultsCalculatedFromRemovedBarsShouldReturnFirstRemainingResult.

@Test
public void getValueOnResultsCalculatedFromRemovedBarsShouldReturnFirstRemainingResult() {
    TimeSeries timeSeries = new MockTimeSeries(1, 1, 1, 1, 1);
    timeSeries.setMaximumBarCount(3);
    assertEquals(2, timeSeries.getRemovedBarsCount());
    SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(timeSeries), 2);
    for (int i = 0; i < 5; i++) {
        assertDecimalEquals(sma.getValue(i), 1);
    }
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 23 with TimeSeries

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

the class CachedIndicatorTest method getValueWithOldResultsRemoval.

@Test
public void getValueWithOldResultsRemoval() {
    double[] data = new double[20];
    Arrays.fill(data, 1);
    TimeSeries timeSeries = new MockTimeSeries(data);
    SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(timeSeries), 10);
    assertDecimalEquals(sma.getValue(5), 1);
    assertDecimalEquals(sma.getValue(10), 1);
    timeSeries.setMaximumBarCount(12);
    assertDecimalEquals(sma.getValue(19), 1);
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 24 with TimeSeries

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

the class CachedIndicatorTest method strategyExecutionOnCachedIndicatorAndLimitedTimeSeries.

@Test
public void strategyExecutionOnCachedIndicatorAndLimitedTimeSeries() {
    TimeSeries timeSeries = new MockTimeSeries(0, 1, 2, 3, 4, 5, 6, 7);
    SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(timeSeries), 2);
    // Theoretical values for SMA(2) cache: 0, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5
    timeSeries.setMaximumBarCount(6);
    // Theoretical values for SMA(2) cache: null, null, 2, 2.5, 3.5, 4.5, 5.5, 6.5
    Strategy strategy = new BaseStrategy(new OverIndicatorRule(sma, Decimal.THREE), new UnderIndicatorRule(sma, Decimal.THREE));
    // Theoretical shouldEnter results: false, false, false, false, true, true, true, true
    // Theoretical shouldExit results: false, false, true, true, false, false, false, false
    // As we return the first bar/result found for the removed bars:
    // -> Approximated values for ClosePrice cache: 2, 2, 2, 3, 4, 5, 6, 7
    // -> Approximated values for SMA(2) cache: 2, 2, 2, 2.5, 3.5, 4.5, 5.5, 6.5
    // Then enters/exits are also approximated:
    // -> shouldEnter results: false, false, false, false, true, true, true, true
    // -> shouldExit results: true, true, true, true, false, false, false, false
    assertFalse(strategy.shouldEnter(0));
    assertTrue(strategy.shouldExit(0));
    assertFalse(strategy.shouldEnter(1));
    assertTrue(strategy.shouldExit(1));
    assertFalse(strategy.shouldEnter(2));
    assertTrue(strategy.shouldExit(2));
    assertFalse(strategy.shouldEnter(3));
    assertTrue(strategy.shouldExit(3));
    assertTrue(strategy.shouldEnter(4));
    assertFalse(strategy.shouldExit(4));
    assertTrue(strategy.shouldEnter(5));
    assertFalse(strategy.shouldExit(5));
    assertTrue(strategy.shouldEnter(6));
    assertFalse(strategy.shouldExit(6));
    assertTrue(strategy.shouldEnter(7));
    assertFalse(strategy.shouldExit(7));
}
Also used : OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseStrategy(org.ta4j.core.BaseStrategy) BaseStrategy(org.ta4j.core.BaseStrategy) Strategy(org.ta4j.core.Strategy) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 25 with TimeSeries

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

the class BollingerBandWidthIndicatorTest method setUp.

@Before
public void setUp() {
    TimeSeries data = new MockTimeSeries(10, 12, 15, 14, 17, 20, 21, 20, 20, 19, 20, 17, 12, 12, 9, 8, 9, 10, 9, 10);
    closePrice = new ClosePriceIndicator(data);
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Before(org.junit.Before)

Aggregations

TimeSeries (org.ta4j.core.TimeSeries)48 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)30 Test (org.junit.Test)24 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)16 BaseTradingRecord (org.ta4j.core.BaseTradingRecord)12 TradingRecord (org.ta4j.core.TradingRecord)11 Bar (org.ta4j.core.Bar)9 Before (org.junit.Before)7 ArrayList (java.util.ArrayList)5 MockBar (org.ta4j.core.mocks.MockBar)5 BaseTimeSeries (org.ta4j.core.BaseTimeSeries)4 ZonedDateTime (java.time.ZonedDateTime)3 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)3 Strategy (org.ta4j.core.Strategy)3 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)2 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JFreeChart (org.jfree.chart.JFreeChart)2 XYPlot (org.jfree.chart.plot.XYPlot)2 TimeSeriesManager (org.ta4j.core.TimeSeriesManager)2