Search in sources :

Example 6 with OverIndicatorRule

use of org.ta4j.core.trading.rules.OverIndicatorRule 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 7 with OverIndicatorRule

use of org.ta4j.core.trading.rules.OverIndicatorRule 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 8 with OverIndicatorRule

use of org.ta4j.core.trading.rules.OverIndicatorRule in project crypto-bot by jnidzwetzki.

the class EMAStrategy02 method getStrategy.

public Strategy getStrategy() {
    EMAIndicator sma1 = new EMAIndicator(closePriceIndicator, sma1Value);
    EMAIndicator sma2 = new EMAIndicator(closePriceIndicator, sma2Value);
    EMAIndicator sma3 = new EMAIndicator(closePriceIndicator, sma3Value);
    Rule buyingRule = new CrossedUpIndicatorRule(sma1, sma2).and(new OverIndicatorRule(sma2, sma3));
    Rule sellingRule = new CrossedDownIndicatorRule(sma1, sma3);
    // .or(new CrossedDownIndicatorRule(sma2, sma3));
    final BaseStrategy strategy = new BaseStrategy(buyingRule, sellingRule);
    return strategy;
}
Also used : OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) EMAIndicator(org.ta4j.core.indicators.EMAIndicator) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) BaseStrategy(org.ta4j.core.BaseStrategy) Rule(org.ta4j.core.Rule) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule)

Example 9 with OverIndicatorRule

use of org.ta4j.core.trading.rules.OverIndicatorRule in project crypto-bot by jnidzwetzki.

the class ForexStrategy01 method getStrategy.

public Strategy getStrategy() {
    EMAIndicator sma1 = new EMAIndicator(closePriceIndicator, 5);
    EMAIndicator sma2 = new EMAIndicator(closePriceIndicator, 10);
    RSIIndicator rsi = new RSIIndicator(closePriceIndicator, 14);
    StochasticOscillatorKIndicator stochK = new StochasticOscillatorKIndicator(timeSeries, 14);
    StochasticOscillatorDIndicator stochD = new StochasticOscillatorDIndicator(stochK);
    Rule buyingRule = new CrossedUpIndicatorRule(sma1, sma2).and(new OverIndicatorRule(rsi, Decimal.valueOf(50))).and(new OverIndicatorRule(stochK, stochD)).and(new UnderIndicatorRule(stochD, Decimal.valueOf(80)));
    Rule sellingRule = new CrossedDownIndicatorRule(sma1, sma2).or(new CrossedDownIndicatorRule(rsi, Decimal.valueOf(50)));
    final BaseStrategy strategy = new BaseStrategy(buyingRule, sellingRule);
    return strategy;
}
Also used : StochasticOscillatorDIndicator(org.ta4j.core.indicators.StochasticOscillatorDIndicator) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) EMAIndicator(org.ta4j.core.indicators.EMAIndicator) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) RSIIndicator(org.ta4j.core.indicators.RSIIndicator) BaseStrategy(org.ta4j.core.BaseStrategy) StochasticOscillatorKIndicator(org.ta4j.core.indicators.StochasticOscillatorKIndicator) Rule(org.ta4j.core.Rule) 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)

Example 10 with OverIndicatorRule

use of org.ta4j.core.trading.rules.OverIndicatorRule in project ta4j by ta4j.

the class TradingBotOnMovingTimeSeries method buildStrategy.

/**
 * @param series a time series
 * @return a dummy strategy
 */
private static Strategy buildStrategy(TimeSeries series) {
    if (series == null) {
        throw new IllegalArgumentException("Series cannot be null");
    }
    ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
    SMAIndicator sma = new SMAIndicator(closePrice, 12);
    // Sell when close price goes over SMA
    return new BaseStrategy(new OverIndicatorRule(sma, closePrice), new UnderIndicatorRule(sma, closePrice));
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator)

Aggregations

OverIndicatorRule (org.ta4j.core.trading.rules.OverIndicatorRule)11 UnderIndicatorRule (org.ta4j.core.trading.rules.UnderIndicatorRule)8 BaseStrategy (org.ta4j.core.BaseStrategy)6 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)6 Rule (org.ta4j.core.Rule)5 EMAIndicator (org.ta4j.core.indicators.EMAIndicator)5 CrossedDownIndicatorRule (org.ta4j.core.trading.rules.CrossedDownIndicatorRule)5 CrossedUpIndicatorRule (org.ta4j.core.trading.rules.CrossedUpIndicatorRule)4 RSIIndicator (org.ta4j.core.indicators.RSIIndicator)3 SMAIndicator (org.ta4j.core.indicators.SMAIndicator)3 MACDIndicator (org.ta4j.core.indicators.MACDIndicator)2 StochasticOscillatorKIndicator (org.ta4j.core.indicators.StochasticOscillatorKIndicator)2 StopLossRule (org.ta4j.core.trading.rules.StopLossRule)2 Test (org.junit.Test)1 Strategy (org.ta4j.core.Strategy)1 TimeSeries (org.ta4j.core.TimeSeries)1 CCIIndicator (org.ta4j.core.indicators.CCIIndicator)1 StochasticOscillatorDIndicator (org.ta4j.core.indicators.StochasticOscillatorDIndicator)1 BollingerBandsMiddleIndicator (org.ta4j.core.indicators.bollinger.BollingerBandsMiddleIndicator)1 BollingerBandsUpperIndicator (org.ta4j.core.indicators.bollinger.BollingerBandsUpperIndicator)1