Search in sources :

Example 16 with ClosePriceIndicator

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

the class MovingMomentumStrategy method buildStrategy.

/**
 * @param series a time series
 * @return a moving momentum strategy
 */
public static Strategy buildStrategy(TimeSeries series) {
    if (series == null) {
        throw new IllegalArgumentException("Series cannot be null");
    }
    ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
    // The bias is bullish when the shorter-moving average moves above the longer moving average.
    // The bias is bearish when the shorter-moving average moves below the longer moving average.
    EMAIndicator shortEma = new EMAIndicator(closePrice, 9);
    EMAIndicator longEma = new EMAIndicator(closePrice, 26);
    StochasticOscillatorKIndicator stochasticOscillK = new StochasticOscillatorKIndicator(series, 14);
    MACDIndicator macd = new MACDIndicator(closePrice, 9, 26);
    EMAIndicator emaMacd = new EMAIndicator(macd, 18);
    // Entry rule
    Rule entryRule = // Trend
    new OverIndicatorRule(shortEma, longEma).and(// Signal 1
    new CrossedDownIndicatorRule(stochasticOscillK, Decimal.valueOf(20))).and(// Signal 2
    new OverIndicatorRule(macd, emaMacd));
    // Exit rule
    Rule exitRule = // Trend
    new UnderIndicatorRule(shortEma, longEma).and(// Signal 1
    new CrossedUpIndicatorRule(stochasticOscillK, Decimal.valueOf(80))).and(// Signal 2
    new UnderIndicatorRule(macd, emaMacd));
    return new BaseStrategy(entryRule, exitRule);
}
Also used : OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) EMAIndicator(org.ta4j.core.indicators.EMAIndicator) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) MACDIndicator(org.ta4j.core.indicators.MACDIndicator) StochasticOscillatorKIndicator(org.ta4j.core.indicators.StochasticOscillatorKIndicator) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator)

Example 17 with ClosePriceIndicator

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

the class RSI2Strategy method buildStrategy.

/**
 * @param series a time series
 * @return a 2-period RSI strategy
 */
public static Strategy buildStrategy(TimeSeries series) {
    if (series == null) {
        throw new IllegalArgumentException("Series cannot be null");
    }
    ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
    SMAIndicator shortSma = new SMAIndicator(closePrice, 5);
    SMAIndicator longSma = new SMAIndicator(closePrice, 200);
    // We use a 2-period RSI indicator to identify buying
    // or selling opportunities within the bigger trend.
    RSIIndicator rsi = new RSIIndicator(closePrice, 2);
    // Entry rule
    // The long-term trend is up when a security is above its 200-period SMA.
    Rule entryRule = // Trend
    new OverIndicatorRule(shortSma, longSma).and(// Signal 1
    new CrossedDownIndicatorRule(rsi, Decimal.valueOf(5))).and(// Signal 2
    new OverIndicatorRule(shortSma, closePrice));
    // Exit rule
    // The long-term trend is down when a security is below its 200-period SMA.
    Rule exitRule = // Trend
    new UnderIndicatorRule(shortSma, longSma).and(// Signal 1
    new CrossedUpIndicatorRule(rsi, Decimal.valueOf(95))).and(// Signal 2
    new UnderIndicatorRule(shortSma, closePrice));
    return new BaseStrategy(entryRule, exitRule);
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) RSIIndicator(org.ta4j.core.indicators.RSIIndicator) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator)

Example 18 with ClosePriceIndicator

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

the class VarianceIndicatorTest method varianceUsingTimeFrame4UsingClosePrice.

@Test
public void varianceUsingTimeFrame4UsingClosePrice() {
    VarianceIndicator var = new VarianceIndicator(new ClosePriceIndicator(data), 4);
    assertDecimalEquals(var.getValue(0), 0);
    assertDecimalEquals(var.getValue(1), 0.25);
    assertDecimalEquals(var.getValue(2), 2.0 / 3);
    assertDecimalEquals(var.getValue(3), 1.25);
    assertDecimalEquals(var.getValue(4), 0.5);
    assertDecimalEquals(var.getValue(5), 0.25);
    assertDecimalEquals(var.getValue(6), 0.5);
    assertDecimalEquals(var.getValue(7), 0.5);
    assertDecimalEquals(var.getValue(8), 0.5);
    assertDecimalEquals(var.getValue(9), 3.5);
    assertDecimalEquals(var.getValue(10), 10.5);
}
Also used : ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 19 with ClosePriceIndicator

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

the class VarianceIndicatorTest method firstValueShouldBeZero.

@Test
public void firstValueShouldBeZero() {
    VarianceIndicator var = new VarianceIndicator(new ClosePriceIndicator(data), 4);
    assertDecimalEquals(var.getValue(0), 0);
}
Also used : ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 20 with ClosePriceIndicator

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

the class VarianceIndicatorTest method varianceUsingTimeFrame2UsingClosePrice.

@Test
public void varianceUsingTimeFrame2UsingClosePrice() {
    VarianceIndicator var = new VarianceIndicator(new ClosePriceIndicator(data), 2);
    assertDecimalEquals(var.getValue(0), 0);
    assertDecimalEquals(var.getValue(1), 0.25);
    assertDecimalEquals(var.getValue(2), 0.25);
    assertDecimalEquals(var.getValue(3), 0.25);
    assertDecimalEquals(var.getValue(9), 2.25);
    assertDecimalEquals(var.getValue(10), 20.25);
}
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