Search in sources :

Example 31 with TimeSeries

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

the class RecursiveCachedIndicator method getValue.

@Override
public T getValue(int index) {
    TimeSeries series = getTimeSeries();
    if (series != null) {
        final int seriesEndIndex = series.getEndIndex();
        if (index <= seriesEndIndex) {
            // We are not after the end of the series
            final int removedBarsCount = series.getRemovedBarsCount();
            int startIndex = Math.max(removedBarsCount, highestResultIndex);
            if (index - startIndex > RECURSION_THRESHOLD) {
                // Calculating the previous values iteratively
                for (int prevIdx = startIndex; prevIdx < index; prevIdx++) {
                    super.getValue(prevIdx);
                }
            }
        }
    }
    return super.getValue(index);
}
Also used : TimeSeries(org.ta4j.core.TimeSeries)

Example 32 with TimeSeries

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

the class CashFlowTest method reallyLongCashFlow.

@Test
public void reallyLongCashFlow() {
    int size = 1000000;
    TimeSeries sampleTimeSeries = new MockTimeSeries(Collections.nCopies(size, new MockBar(10)));
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, sampleTimeSeries), Order.sellAt(size - 1, sampleTimeSeries));
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertDecimalEquals(cashFlow.getValue(size - 1), 1);
}
Also used : BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TradingRecord(org.ta4j.core.TradingRecord) MockBar(org.ta4j.core.mocks.MockBar) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 33 with TimeSeries

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

the class CashFlowTest method cashFlowSize.

@Test
public void cashFlowSize() {
    TimeSeries sampleTimeSeries = new MockTimeSeries(1d, 2d, 3d, 4d, 5d);
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, new BaseTradingRecord());
    assertEquals(5, cashFlow.getSize());
}
Also used : BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 34 with TimeSeries

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

the class CashFlowTest method cashFlowValueWithOnlyOneTradeAndAGapAfter.

@Test
public void cashFlowValueWithOnlyOneTradeAndAGapAfter() {
    TimeSeries sampleTimeSeries = new MockTimeSeries(1d, 2d, 2d);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, sampleTimeSeries), Order.sellAt(1, sampleTimeSeries));
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertEquals(3, cashFlow.getSize());
    assertDecimalEquals(cashFlow.getValue(0), 1);
    assertDecimalEquals(cashFlow.getValue(1), 2);
    assertDecimalEquals(cashFlow.getValue(2), 2);
}
Also used : BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TradingRecord(org.ta4j.core.TradingRecord) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 35 with TimeSeries

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

the class CashFlowTest method cashFlowSell.

@Test
public void cashFlowSell() {
    TimeSeries sampleTimeSeries = new MockTimeSeries(1, 2, 4, 8, 16, 32);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.sellAt(2, sampleTimeSeries), Order.buyAt(3, sampleTimeSeries));
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertDecimalEquals(cashFlow.getValue(0), 1);
    assertDecimalEquals(cashFlow.getValue(1), 1);
    assertDecimalEquals(cashFlow.getValue(2), 1);
    assertDecimalEquals(cashFlow.getValue(3), "0.5");
    assertDecimalEquals(cashFlow.getValue(4), "0.5");
    assertDecimalEquals(cashFlow.getValue(5), "0.5");
}
Also used : BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TradingRecord(org.ta4j.core.TradingRecord) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

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