Search in sources :

Example 1 with SMAIndicator

use of org.ta4j.core.indicators.SMAIndicator 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 SMAIndicator

use of org.ta4j.core.indicators.SMAIndicator 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 3 with SMAIndicator

use of org.ta4j.core.indicators.SMAIndicator in project ta4j by ta4j.

the class BollingerBandsMiddleIndicatorTest method bollingerBandsMiddleUsingSMA.

@Test
public void bollingerBandsMiddleUsingSMA() {
    SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(data), 3);
    BollingerBandsMiddleIndicator bbmSMA = new BollingerBandsMiddleIndicator(sma);
    for (int i = 0; i < data.getBarCount(); i++) {
        assertEquals(sma.getValue(i), bbmSMA.getValue(i));
    }
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 4 with SMAIndicator

use of org.ta4j.core.indicators.SMAIndicator in project ta4j by ta4j.

the class BollingerBandsUpperIndicatorTest method setUp.

@Before
public void setUp() {
    TimeSeries data = new MockTimeSeries(1, 2, 3, 4, 3, 4, 5, 4, 3, 3, 4, 3, 2);
    timeFrame = 3;
    closePrice = new ClosePriceIndicator(data);
    sma = new SMAIndicator(closePrice, timeFrame);
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Before(org.junit.Before)

Example 5 with SMAIndicator

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

the class EMAStrategy01 method getStrategy.

public Strategy getStrategy() {
    SMAIndicator shortSma = new SMAIndicator(closePriceIndicator, 5);
    SMAIndicator longSma = new SMAIndicator(closePriceIndicator, 30);
    Rule buyingRule = new CrossedUpIndicatorRule(shortSma, longSma);
    Rule sellingRule = new CrossedDownIndicatorRule(shortSma, longSma).or(new StopLossRule(closePriceIndicator, Decimal.valueOf("3"))).or(new StopGainRule(closePriceIndicator, Decimal.valueOf("2")));
    final BaseStrategy strategy = new BaseStrategy(buyingRule, sellingRule);
    return strategy;
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) BaseStrategy(org.ta4j.core.BaseStrategy) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) StopGainRule(org.ta4j.core.trading.rules.StopGainRule) Rule(org.ta4j.core.Rule) StopGainRule(org.ta4j.core.trading.rules.StopGainRule) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule)

Aggregations

SMAIndicator (org.ta4j.core.indicators.SMAIndicator)9 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)7 CrossedDownIndicatorRule (org.ta4j.core.trading.rules.CrossedDownIndicatorRule)3 CrossedUpIndicatorRule (org.ta4j.core.trading.rules.CrossedUpIndicatorRule)3 OverIndicatorRule (org.ta4j.core.trading.rules.OverIndicatorRule)3 StopLossRule (org.ta4j.core.trading.rules.StopLossRule)3 UnderIndicatorRule (org.ta4j.core.trading.rules.UnderIndicatorRule)3 Before (org.junit.Before)2 Test (org.junit.Test)2 BaseStrategy (org.ta4j.core.BaseStrategy)2 Rule (org.ta4j.core.Rule)2 TimeSeries (org.ta4j.core.TimeSeries)2 StandardDeviationIndicator (org.ta4j.core.indicators.statistics.StandardDeviationIndicator)2 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)2 StopGainRule (org.ta4j.core.trading.rules.StopGainRule)2 CashFlow (org.ta4j.core.analysis.CashFlow)1 AverageProfitableTradesCriterion (org.ta4j.core.analysis.criteria.AverageProfitableTradesCriterion)1 RewardRiskRatioCriterion (org.ta4j.core.analysis.criteria.RewardRiskRatioCriterion)1 TotalProfitCriterion (org.ta4j.core.analysis.criteria.TotalProfitCriterion)1 VersusBuyAndHoldCriterion (org.ta4j.core.analysis.criteria.VersusBuyAndHoldCriterion)1