Search in sources :

Example 26 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator 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 27 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class EMAIndicatorTest method stackOverflowError.

@Test
public void stackOverflowError() throws Exception {
    List<Bar> bigListOfBars = new ArrayList<Bar>();
    for (int i = 0; i < 10000; i++) {
        bigListOfBars.add(new MockBar(i));
    }
    MockTimeSeries bigSeries = new MockTimeSeries(bigListOfBars);
    Indicator<Decimal> indicator = getIndicator(new ClosePriceIndicator(bigSeries), 10);
    // if a StackOverflowError is thrown here, then the RecursiveCachedIndicator does not work as intended.
    assertDecimalEquals(indicator.getValue(9999), 9994.5);
}
Also used : MockBar(org.ta4j.core.mocks.MockBar) MockBar(org.ta4j.core.mocks.MockBar) ArrayList(java.util.ArrayList) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 28 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class EMAIndicatorTest method firstValueShouldBeEqualsToFirstDataValue.

@Test
public void firstValueShouldBeEqualsToFirstDataValue() throws Exception {
    Indicator<Decimal> indicator = getIndicator(new ClosePriceIndicator(data), 1);
    assertDecimalEquals(indicator.getValue(0), 64.75);
}
Also used : ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 29 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class RSIIndicatorTest method usingTimeFrame14UsingClosePrice.

@Test
public void usingTimeFrame14UsingClosePrice() throws Exception {
    Indicator<Decimal> indicator = getIndicator(new ClosePriceIndicator(data), 14);
    assertEquals(68.4746, indicator.getValue(15).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(64.7836, indicator.getValue(16).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(72.0776, indicator.getValue(17).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(60.7800, indicator.getValue(18).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(63.6439, indicator.getValue(19).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(72.3433, indicator.getValue(20).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(67.3822, indicator.getValue(21).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(68.5438, indicator.getValue(22).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(76.2770, indicator.getValue(23).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(77.9908, indicator.getValue(24).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(67.4895, indicator.getValue(25).doubleValue(), TATestsUtils.TA_OFFSET);
}
Also used : ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 30 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class RSIIndicatorTest method zeroIfNoGain.

@Test
public void zeroIfNoGain() throws Exception {
    Indicator<Decimal> indicator = getIndicator(new ClosePriceIndicator(data), 1);
    assertEquals(Decimal.ZERO, indicator.getValue(1));
    assertEquals(Decimal.ZERO, indicator.getValue(2));
}
Also used : ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Aggregations

ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)81 Test (org.junit.Test)55 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)26 TimeSeries (org.ta4j.core.TimeSeries)16 Before (org.junit.Before)14 SMAIndicator (org.ta4j.core.indicators.SMAIndicator)7 OverIndicatorRule (org.ta4j.core.trading.rules.OverIndicatorRule)6 ArrayList (java.util.ArrayList)5 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)5 MockBar (org.ta4j.core.mocks.MockBar)5 UnderIndicatorRule (org.ta4j.core.trading.rules.UnderIndicatorRule)5 SimpleDateFormat (java.text.SimpleDateFormat)4 JFreeChart (org.jfree.chart.JFreeChart)4 DateAxis (org.jfree.chart.axis.DateAxis)4 XYPlot (org.jfree.chart.plot.XYPlot)4 EMAIndicator (org.ta4j.core.indicators.EMAIndicator)4 CrossedDownIndicatorRule (org.ta4j.core.trading.rules.CrossedDownIndicatorRule)4 BaseStrategy (org.ta4j.core.BaseStrategy)3 Decimal (org.ta4j.core.Decimal)3 MaxPriceIndicator (org.ta4j.core.indicators.helpers.MaxPriceIndicator)3