Search in sources :

Example 1 with BaseStrategy

use of org.ta4j.core.BaseStrategy in project crypto-bot by jnidzwetzki.

the class BBreakoutStrategy method getStrategy.

public Strategy getStrategy() {
    final ClosePriceIndicator closePrice = new ClosePriceIndicator(timeSeries);
    final SMAIndicator sma = new SMAIndicator(closePrice, bbPeriod);
    final BollingerBandsMiddleIndicator bbmiddle = new BollingerBandsMiddleIndicator(sma);
    final StandardDeviationIndicator sd = new StandardDeviationIndicator(closePrice, bbPeriod);
    final BollingerBandsUpperIndicator bbup = new BollingerBandsUpperIndicator(bbmiddle, sd, Decimal.valueOf(deviationUp));
    final BollingerBandsUpperIndicator bbdown = new BollingerBandsUpperIndicator(bbmiddle, sd, Decimal.valueOf(deviationDown));
    final Rule buyingRule = new UnderIndicatorRule(closePrice, bbdown);
    final Rule sellingRule = new OverIndicatorRule(closePrice, bbup).or(new StopLossRule(closePrice, Decimal.valueOf(2)));
    final BaseStrategy strategy = new BaseStrategy(buyingRule, sellingRule);
    return strategy;
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) BollingerBandsMiddleIndicator(org.ta4j.core.indicators.bollinger.BollingerBandsMiddleIndicator) BaseStrategy(org.ta4j.core.BaseStrategy) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) StandardDeviationIndicator(org.ta4j.core.indicators.statistics.StandardDeviationIndicator) BollingerBandsUpperIndicator(org.ta4j.core.indicators.bollinger.BollingerBandsUpperIndicator) Rule(org.ta4j.core.Rule) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator)

Example 2 with BaseStrategy

use of org.ta4j.core.BaseStrategy in project crypto-bot by jnidzwetzki.

the class DonchianChannelStrategy method getStrategy.

@Override
public Strategy getStrategy() {
    final MACDIndicator macd = new MACDIndicator(closePriceIndicator, 9, 26);
    final EMAIndicator emaMacd = new EMAIndicator(macd, 9);
    final Rule buyingRule = new AndRule(new IsRisingRule(donchianChannelUpper, 1), new OverIndicatorRule(macd, Decimal.valueOf(0)));
    final // new OrRule(
    Rule sellingRule = new IsFallingRule(donchianChannelLower, 1);
    // new UnderIndicatorRule(macd, emaMacd));
    final BaseStrategy strategy = new BaseStrategy(buyingRule, sellingRule);
    return strategy;
}
Also used : AndRule(org.ta4j.core.trading.rules.AndRule) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) IsFallingRule(org.ta4j.core.trading.rules.IsFallingRule) EMAIndicator(org.ta4j.core.indicators.EMAIndicator) IsRisingRule(org.ta4j.core.trading.rules.IsRisingRule) MACDIndicator(org.ta4j.core.indicators.MACDIndicator) BaseStrategy(org.ta4j.core.BaseStrategy) Rule(org.ta4j.core.Rule) IsFallingRule(org.ta4j.core.trading.rules.IsFallingRule) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) AndRule(org.ta4j.core.trading.rules.AndRule) IsRisingRule(org.ta4j.core.trading.rules.IsRisingRule)

Example 3 with BaseStrategy

use of org.ta4j.core.BaseStrategy in project crypto-bot by jnidzwetzki.

the class EMAStrategy03 method getStrategy.

public Strategy getStrategy() {
    ClosePriceIndicator closePrice = new ClosePriceIndicator(timeSeries);
    EMAIndicator sma1 = new EMAIndicator(closePrice, sma1Value);
    EMAIndicator sma2 = new EMAIndicator(closePrice, sma2Value);
    EMAIndicator sma3 = new EMAIndicator(closePrice, sma3Value);
    RSIIndicator rsi = new RSIIndicator(closePrice, 14);
    Rule buyingRule = new OverIndicatorRule(sma1, sma2).and(new OverIndicatorRule(sma2, sma3)).and(new OverIndicatorRule(rsi, Decimal.valueOf(50)));
    Rule sellingRule = new CrossedDownIndicatorRule(sma1, sma3).or(new CrossedDownIndicatorRule(sma2, sma3)).or(new StopLossRule(closePrice, Decimal.valueOf("3")));
    final BaseStrategy strategy = new BaseStrategy(buyingRule, sellingRule);
    return strategy;
}
Also used : OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) EMAIndicator(org.ta4j.core.indicators.EMAIndicator) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) RSIIndicator(org.ta4j.core.indicators.RSIIndicator) BaseStrategy(org.ta4j.core.BaseStrategy) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) Rule(org.ta4j.core.Rule) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator)

Example 4 with BaseStrategy

use of org.ta4j.core.BaseStrategy in project ta4j by ta4j.

the class AbstractAnalysisCriterionTest method setUp.

@Before
public void setUp() {
    alwaysStrategy = new BaseStrategy(BooleanRule.TRUE, BooleanRule.TRUE);
    buyAndHoldStrategy = new BaseStrategy(new FixedRule(0), new FixedRule(4));
    strategies = new ArrayList<Strategy>();
    strategies.add(alwaysStrategy);
    strategies.add(buyAndHoldStrategy);
}
Also used : BaseStrategy(org.ta4j.core.BaseStrategy) BaseStrategy(org.ta4j.core.BaseStrategy) Strategy(org.ta4j.core.Strategy) FixedRule(org.ta4j.core.trading.rules.FixedRule) Before(org.junit.Before)

Example 5 with BaseStrategy

use of org.ta4j.core.BaseStrategy 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)

Aggregations

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