Search in sources :

Example 1 with StopLossRule

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

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

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

Example 4 with StopLossRule

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

the class Quickstart method main.

public static void main(String[] args) {
    // Getting a time series (from any provider: CSV, web service, etc.)
    TimeSeries series = CsvTradesLoader.loadBitstampSeries();
    // Getting the close price of the bars
    Decimal firstClosePrice = series.getBar(0).getClosePrice();
    System.out.println("First close price: " + firstClosePrice.doubleValue());
    // Or within an indicator:
    ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
    // Here is the same close price:
    // equal to firstClosePrice
    System.out.println(firstClosePrice.isEqual(closePrice.getValue(0)));
    // Getting the simple moving average (SMA) of the close price over the last 5 bars
    SMAIndicator shortSma = new SMAIndicator(closePrice, 5);
    // Here is the 5-bars-SMA value at the 42nd index
    System.out.println("5-bars-SMA value at the 42nd index: " + shortSma.getValue(42).doubleValue());
    // Getting a longer SMA (e.g. over the 30 last bars)
    SMAIndicator longSma = new SMAIndicator(closePrice, 30);
    // Ok, now let's building our trading rules!
    // Buying rules
    // We want to buy:
    // - if the 5-bars SMA crosses over 30-bars SMA
    // - or if the price goes below a defined price (e.g $800.00)
    Rule buyingRule = new CrossedUpIndicatorRule(shortSma, longSma).or(new CrossedDownIndicatorRule(closePrice, Decimal.valueOf("800")));
    // Selling rules
    // We want to sell:
    // - if the 5-bars SMA crosses under 30-bars SMA
    // - or if if the price looses more than 3%
    // - or if the price earns more than 2%
    Rule sellingRule = new CrossedDownIndicatorRule(shortSma, longSma).or(new StopLossRule(closePrice, Decimal.valueOf("3"))).or(new StopGainRule(closePrice, Decimal.valueOf("2")));
    // Running our juicy trading strategy...
    TimeSeriesManager seriesManager = new TimeSeriesManager(series);
    TradingRecord tradingRecord = seriesManager.run(new BaseStrategy(buyingRule, sellingRule));
    System.out.println("Number of trades for our strategy: " + tradingRecord.getTradeCount());
    // Analysis
    // Getting the cash flow of the resulting trades
    CashFlow cashFlow = new CashFlow(series, tradingRecord);
    // Getting the profitable trades ratio
    AnalysisCriterion profitTradesRatio = new AverageProfitableTradesCriterion();
    System.out.println("Profitable trades ratio: " + profitTradesRatio.calculate(series, tradingRecord));
    // Getting the reward-risk ratio
    AnalysisCriterion rewardRiskRatio = new RewardRiskRatioCriterion();
    System.out.println("Reward-risk ratio: " + rewardRiskRatio.calculate(series, tradingRecord));
    // Total profit of our strategy
    // vs total profit of a buy-and-hold strategy
    AnalysisCriterion vsBuyAndHold = new VersusBuyAndHoldCriterion(new TotalProfitCriterion());
    System.out.println("Our profit vs buy-and-hold profit: " + vsBuyAndHold.calculate(series, tradingRecord));
// Your turn!
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) CashFlow(org.ta4j.core.analysis.CashFlow) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) TotalProfitCriterion(org.ta4j.core.analysis.criteria.TotalProfitCriterion) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) StopGainRule(org.ta4j.core.trading.rules.StopGainRule) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) RewardRiskRatioCriterion(org.ta4j.core.analysis.criteria.RewardRiskRatioCriterion) VersusBuyAndHoldCriterion(org.ta4j.core.analysis.criteria.VersusBuyAndHoldCriterion) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) StopGainRule(org.ta4j.core.trading.rules.StopGainRule) CrossedDownIndicatorRule(org.ta4j.core.trading.rules.CrossedDownIndicatorRule) CrossedUpIndicatorRule(org.ta4j.core.trading.rules.CrossedUpIndicatorRule) AverageProfitableTradesCriterion(org.ta4j.core.analysis.criteria.AverageProfitableTradesCriterion)

Aggregations

StopLossRule (org.ta4j.core.trading.rules.StopLossRule)4 BaseStrategy (org.ta4j.core.BaseStrategy)3 Rule (org.ta4j.core.Rule)3 SMAIndicator (org.ta4j.core.indicators.SMAIndicator)3 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)3 CrossedDownIndicatorRule (org.ta4j.core.trading.rules.CrossedDownIndicatorRule)3 CrossedUpIndicatorRule (org.ta4j.core.trading.rules.CrossedUpIndicatorRule)2 OverIndicatorRule (org.ta4j.core.trading.rules.OverIndicatorRule)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 EMAIndicator (org.ta4j.core.indicators.EMAIndicator)1 RSIIndicator (org.ta4j.core.indicators.RSIIndicator)1 BollingerBandsMiddleIndicator (org.ta4j.core.indicators.bollinger.BollingerBandsMiddleIndicator)1 BollingerBandsUpperIndicator (org.ta4j.core.indicators.bollinger.BollingerBandsUpperIndicator)1 StandardDeviationIndicator (org.ta4j.core.indicators.statistics.StandardDeviationIndicator)1 UnderIndicatorRule (org.ta4j.core.trading.rules.UnderIndicatorRule)1