Search in sources :

Example 1 with Strategy

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

the class EMABot method createStrategies.

/**
 * Init the trading stretegies
 */
private void createStrategies() {
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        TradeStrategyFactory strategyFactory = new EMAStrategy03(5, 12, 40, timeSeries.get(currency));
        final Strategy strategy = strategyFactory.getStrategy();
        strategies.put(currency, strategy);
    }
}
Also used : EMAStrategy03(com.github.jnidzwetzki.cryptobot.strategy.EMAStrategy03) Strategy(org.ta4j.core.Strategy) TradeStrategyFactory(com.github.jnidzwetzki.cryptobot.strategy.TradeStrategyFactory) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)

Example 2 with Strategy

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

the class StrategyAnalysis method main.

public static void main(String[] args) {
    // Getting the time series
    TimeSeries series = CsvTradesLoader.loadBitstampSeries();
    // Building the trading strategy
    Strategy strategy = MovingMomentumStrategy.buildStrategy(series);
    // Running the strategy
    TimeSeriesManager seriesManager = new TimeSeriesManager(series);
    TradingRecord tradingRecord = seriesManager.run(strategy);
    /*
          Analysis criteria
         */
    // Total profit
    TotalProfitCriterion totalProfit = new TotalProfitCriterion();
    System.out.println("Total profit: " + totalProfit.calculate(series, tradingRecord));
    // Number of bars
    System.out.println("Number of bars: " + new NumberOfBarsCriterion().calculate(series, tradingRecord));
    // Average profit (per bar)
    System.out.println("Average profit (per bar): " + new AverageProfitCriterion().calculate(series, tradingRecord));
    // Number of trades
    System.out.println("Number of trades: " + new NumberOfTradesCriterion().calculate(series, tradingRecord));
    // Profitable trades ratio
    System.out.println("Profitable trades ratio: " + new AverageProfitableTradesCriterion().calculate(series, tradingRecord));
    // Maximum drawdown
    System.out.println("Maximum drawdown: " + new MaximumDrawdownCriterion().calculate(series, tradingRecord));
    // Reward-risk ratio
    System.out.println("Reward-risk ratio: " + new RewardRiskRatioCriterion().calculate(series, tradingRecord));
    // Total transaction cost
    System.out.println("Total transaction cost (from $1000): " + new LinearTransactionCostCriterion(1000, 0.005).calculate(series, tradingRecord));
    // Buy-and-hold
    System.out.println("Buy-and-hold: " + new BuyAndHoldCriterion().calculate(series, tradingRecord));
    // Total profit vs buy-and-hold
    System.out.println("Custom strategy profit vs buy-and-hold strategy profit: " + new VersusBuyAndHoldCriterion(totalProfit).calculate(series, tradingRecord));
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) TradingRecord(org.ta4j.core.TradingRecord) TimeSeriesManager(org.ta4j.core.TimeSeriesManager) MovingMomentumStrategy(ta4jexamples.strategies.MovingMomentumStrategy) Strategy(org.ta4j.core.Strategy)

Example 3 with Strategy

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

the class StrategyExecutionLogging method main.

public static void main(String[] args) {
    // Loading the Logback configuration
    loadLoggerConfiguration();
    // Getting the time series
    TimeSeries series = CsvTradesLoader.loadBitstampSeries();
    // Building the trading strategy
    Strategy strategy = CCICorrectionStrategy.buildStrategy(series);
    // Running the strategy
    TimeSeriesManager seriesManager = new TimeSeriesManager(series);
    seriesManager.run(strategy);
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) TimeSeriesManager(org.ta4j.core.TimeSeriesManager) Strategy(org.ta4j.core.Strategy) CCICorrectionStrategy(ta4jexamples.strategies.CCICorrectionStrategy)

Example 4 with Strategy

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

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

Strategy (org.ta4j.core.Strategy)7 BaseStrategy (org.ta4j.core.BaseStrategy)4 TimeSeriesManager (org.ta4j.core.TimeSeriesManager)4 Test (org.junit.Test)3 TimeSeries (org.ta4j.core.TimeSeries)3 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)3 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)1 EMAStrategy03 (com.github.jnidzwetzki.cryptobot.strategy.EMAStrategy03)1 TradeStrategyFactory (com.github.jnidzwetzki.cryptobot.strategy.TradeStrategyFactory)1 Before (org.junit.Before)1 TradingRecord (org.ta4j.core.TradingRecord)1 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)1 FixedRule (org.ta4j.core.trading.rules.FixedRule)1 OverIndicatorRule (org.ta4j.core.trading.rules.OverIndicatorRule)1 UnderIndicatorRule (org.ta4j.core.trading.rules.UnderIndicatorRule)1 CCICorrectionStrategy (ta4jexamples.strategies.CCICorrectionStrategy)1 MovingMomentumStrategy (ta4jexamples.strategies.MovingMomentumStrategy)1