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));
}
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);
}
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);
}
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);
}
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));
}
Aggregations