Search in sources :

Example 31 with ClosePriceIndicator

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

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

Example 33 with ClosePriceIndicator

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

the class WilliamsRIndicatorTest method williamsRUsingTimeFrame5UsingClosePrice.

@Test
public void williamsRUsingTimeFrame5UsingClosePrice() {
    WilliamsRIndicator wr = new WilliamsRIndicator(new ClosePriceIndicator(data), 5, new MaxPriceIndicator(data), new MinPriceIndicator(data));
    assertDecimalEquals(wr.getValue(4), -47.2222);
    assertDecimalEquals(wr.getValue(5), -54.5454);
    assertDecimalEquals(wr.getValue(6), -78.5714);
    assertDecimalEquals(wr.getValue(7), -47.6190);
    assertDecimalEquals(wr.getValue(8), -25d);
    assertDecimalEquals(wr.getValue(9), -5.2632);
    assertDecimalEquals(wr.getValue(10), -13.9535);
}
Also used : MaxPriceIndicator(org.ta4j.core.indicators.helpers.MaxPriceIndicator) MinPriceIndicator(org.ta4j.core.indicators.helpers.MinPriceIndicator) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 34 with ClosePriceIndicator

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

the class ZLEMAIndicatorTest method ZLEMAUsingTimeFrame10UsingClosePrice.

@Test
public void ZLEMAUsingTimeFrame10UsingClosePrice() {
    ZLEMAIndicator zlema = new ZLEMAIndicator(new ClosePriceIndicator(data), 10);
    assertDecimalEquals(zlema.getValue(9), 11.9091);
    assertDecimalEquals(zlema.getValue(10), 8.8347);
    assertDecimalEquals(zlema.getValue(11), 5.7739);
}
Also used : ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 35 with ClosePriceIndicator

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

the class ZLEMAIndicatorTest method smallTimeFrame.

@Test
public void smallTimeFrame() {
    ZLEMAIndicator zlema = new ZLEMAIndicator(new ClosePriceIndicator(data), 1);
    assertDecimalEquals(zlema.getValue(0), "10");
}
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