Search in sources :

Example 46 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries 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 47 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries 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 48 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.

the class RSIIndicatorTest method onlineExampleTest.

@Test
public void onlineExampleTest() throws Exception {
    // from http://cns.bu.edu/~gsc/CN710/fincast/Technical%20_indicators/Relative%20Strength%20Index%20(RSI).htm
    // which uses a different calculation of RSI than ta4j
    TimeSeries series = new MockTimeSeries(46.1250, 47.1250, 46.4375, 46.9375, 44.9375, 44.2500, 44.6250, 45.7500, 47.8125, 47.5625, 47.0000, 44.5625, 46.3125, 47.6875, 46.6875, 45.6875, 43.0625, 43.5625, 44.8750, 43.6875);
    // ta4j RSI uses MMA for average gain and loss
    // then uses simple division of the two for RS
    Indicator<Decimal> indicator = getIndicator(new ClosePriceIndicator(series), 14);
    Indicator<Decimal> close = new ClosePriceIndicator(series);
    Indicator<Decimal> gain = new GainIndicator(close);
    Indicator<Decimal> loss = new LossIndicator(close);
    // this site uses SMA for average gain and loss
    // then uses ratio of MMAs for RS (except for first calculation)
    Indicator<Decimal> avgGain = new SMAIndicator(gain, 14);
    Indicator<Decimal> avgLoss = new SMAIndicator(loss, 14);
    // first online calculation is simple division
    double onlineRs = avgGain.getValue(14).dividedBy(avgLoss.getValue(14)).doubleValue();
    assertEquals(0.5848, avgGain.getValue(14).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(0.5446, avgLoss.getValue(14).doubleValue(), TATestsUtils.TA_OFFSET);
    assertEquals(1.0738, onlineRs, TATestsUtils.TA_OFFSET);
    double onlineRsi = 100d - (100d / (1d + onlineRs));
    // difference in RSI values:
    assertEquals(51.779, onlineRsi, 0.001);
    assertEquals(52.1304, indicator.getValue(14).doubleValue(), TATestsUtils.TA_OFFSET);
    // strange, online average gain and loss is not a simple moving average!
    // but they only use them for the first RS calculation
    // assertEquals(0.5430, avgGain.getValue(15).doubleValue(), TATestsUtils.TA_OFFSET);
    // assertEquals(0.5772, avgLoss.getValue(15).doubleValue(), TATestsUtils.TA_OFFSET);
    // second online calculation uses MMAs
    // MMA of average gain
    double dividend = avgGain.getValue(14).multipliedBy(13d).plus(gain.getValue(15)).dividedBy(14d).doubleValue();
    // MMA of average loss
    double divisor = avgLoss.getValue(14).multipliedBy(13d).plus(loss.getValue(15)).dividedBy(14d).doubleValue();
    onlineRs = dividend / divisor;
    assertEquals(0.9409, onlineRs, TATestsUtils.TA_OFFSET);
    onlineRsi = 100d - (100d / (1d + onlineRs));
    // difference in RSI values:
    assertEquals(48.477, onlineRsi, 0.001);
    assertEquals(47.3710, indicator.getValue(15).doubleValue(), TATestsUtils.TA_OFFSET);
}
Also used : MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) GainIndicator(org.ta4j.core.indicators.helpers.GainIndicator) LossIndicator(org.ta4j.core.indicators.helpers.LossIndicator) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 49 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.

the class RandomWalkIndexLowIndicatorTest method setUp.

@Before
public void setUp() {
    List<Bar> bars = new ArrayList<Bar>();
    bars.add(new MockBar(44.98, 45.05, 45.17, 44.96));
    bars.add(new MockBar(45.05, 45.10, 45.15, 44.99));
    bars.add(new MockBar(45.11, 45.19, 45.32, 45.11));
    bars.add(new MockBar(45.19, 45.14, 45.25, 45.04));
    bars.add(new MockBar(45.12, 45.15, 45.20, 45.10));
    bars.add(new MockBar(45.15, 45.14, 45.20, 45.10));
    bars.add(new MockBar(45.13, 45.10, 45.16, 45.07));
    bars.add(new MockBar(45.12, 45.15, 45.22, 45.10));
    bars.add(new MockBar(45.15, 45.22, 45.27, 45.14));
    bars.add(new MockBar(45.24, 45.43, 45.45, 45.20));
    bars.add(new MockBar(45.43, 45.44, 45.50, 45.39));
    bars.add(new MockBar(45.43, 45.55, 45.60, 45.35));
    bars.add(new MockBar(45.58, 45.55, 45.61, 45.39));
    bars.add(new MockBar(45.45, 45.01, 45.55, 44.80));
    bars.add(new MockBar(45.03, 44.23, 45.04, 44.17));
    bars.add(new MockBar(44.23, 43.95, 44.29, 43.81));
    bars.add(new MockBar(43.91, 43.08, 43.99, 43.08));
    bars.add(new MockBar(43.07, 43.55, 43.65, 43.06));
    bars.add(new MockBar(43.56, 43.95, 43.99, 43.53));
    bars.add(new MockBar(43.93, 44.47, 44.58, 43.93));
    data = new MockTimeSeries(bars);
}
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) Before(org.junit.Before)

Example 50 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.

the class WMAIndicatorTest method wmaWithTimeFrameGreaterThanSeriesSize.

@Test
public void wmaWithTimeFrameGreaterThanSeriesSize() {
    MockTimeSeries series = new MockTimeSeries(1d, 2d, 3d, 4d, 5d, 6d);
    Indicator<Decimal> close = new ClosePriceIndicator(series);
    Indicator<Decimal> wmaIndicator = new WMAIndicator(close, 55);
    assertDecimalEquals(wmaIndicator.getValue(0), 1);
    assertDecimalEquals(wmaIndicator.getValue(1), 1.6667);
    assertDecimalEquals(wmaIndicator.getValue(2), 2.3333);
    assertDecimalEquals(wmaIndicator.getValue(3), 3);
    assertDecimalEquals(wmaIndicator.getValue(4), 3.6666);
    assertDecimalEquals(wmaIndicator.getValue(5), 4.3333);
}
Also used : Decimal(org.ta4j.core.Decimal) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Aggregations

MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)135 Test (org.junit.Test)90 MockBar (org.ta4j.core.mocks.MockBar)46 ArrayList (java.util.ArrayList)45 Before (org.junit.Before)45 Bar (org.ta4j.core.Bar)42 TimeSeries (org.ta4j.core.TimeSeries)29 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)25 BaseTradingRecord (org.ta4j.core.BaseTradingRecord)17 TradingRecord (org.ta4j.core.TradingRecord)12 ZonedDateTime (java.time.ZonedDateTime)5 AnalysisCriterion (org.ta4j.core.AnalysisCriterion)3 BaseStrategy (org.ta4j.core.BaseStrategy)3 ExternalCriterionTest (org.ta4j.core.ExternalCriterionTest)3 Strategy (org.ta4j.core.Strategy)3 Decimal (org.ta4j.core.Decimal)2 TimeSeriesManager (org.ta4j.core.TimeSeriesManager)2 Trade (org.ta4j.core.Trade)2 SMAIndicator (org.ta4j.core.indicators.SMAIndicator)2 FixedRule (org.ta4j.core.trading.rules.FixedRule)2