Search in sources :

Example 1 with BarSeriesManager

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

the class CompareNumTypes method test.

public static Num test(BarSeries series) {
    ClosePriceIndicator closePriceIndicator = new ClosePriceIndicator(series);
    RSIIndicator rsi = new RSIIndicator(closePriceIndicator, 100);
    MACDIndicator macdIndicator = new MACDIndicator(rsi);
    EMAIndicator ema = new EMAIndicator(rsi, 12);
    EMAIndicator emaLong = new EMAIndicator(rsi, 26);
    DifferenceIndicator macdIndicator2 = new DifferenceIndicator(ema, emaLong);
    Rule entry = new IsEqualRule(macdIndicator, macdIndicator2);
    Rule exit = new UnderIndicatorRule(new LowPriceIndicator(series), new HighPriceIndicator(series));
    // enter/exit every tick
    Strategy strategy1 = new BaseStrategy(entry, exit);
    long start = System.currentTimeMillis();
    BarSeriesManager manager = new BarSeriesManager(series);
    TradingRecord record1 = manager.run(strategy1);
    GrossReturnCriterion totalReturn1 = new GrossReturnCriterion();
    Num returnResult1 = totalReturn1.calculate(series, record1);
    long end = System.currentTimeMillis();
    System.out.printf("[%s]\n" + "    -Time:   %s ms.\n" + "    -Profit: %s \n" + "    -Bars:   %s\n \n", series.getName(), (end - start), returnResult1, series.getBarCount());
    return returnResult1;
}
Also used : TradingRecord(org.ta4j.core.TradingRecord) EMAIndicator(org.ta4j.core.indicators.EMAIndicator) UnderIndicatorRule(org.ta4j.core.rules.UnderIndicatorRule) IsEqualRule(org.ta4j.core.rules.IsEqualRule) LowPriceIndicator(org.ta4j.core.indicators.helpers.LowPriceIndicator) RSIIndicator(org.ta4j.core.indicators.RSIIndicator) GrossReturnCriterion(org.ta4j.core.analysis.criteria.pnl.GrossReturnCriterion) DecimalNum(org.ta4j.core.num.DecimalNum) Num(org.ta4j.core.num.Num) DoubleNum(org.ta4j.core.num.DoubleNum) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) HighPriceIndicator(org.ta4j.core.indicators.helpers.HighPriceIndicator) BarSeriesManager(org.ta4j.core.BarSeriesManager) MACDIndicator(org.ta4j.core.indicators.MACDIndicator) BaseStrategy(org.ta4j.core.BaseStrategy) BaseStrategy(org.ta4j.core.BaseStrategy) Strategy(org.ta4j.core.Strategy) Rule(org.ta4j.core.Rule) UnderIndicatorRule(org.ta4j.core.rules.UnderIndicatorRule) IsEqualRule(org.ta4j.core.rules.IsEqualRule) DifferenceIndicator(org.ta4j.core.indicators.helpers.DifferenceIndicator)

Example 2 with BarSeriesManager

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

the class CCICorrectionStrategy method main.

public static void main(String[] args) {
    // Getting the bar series
    BarSeries series = CsvTradesLoader.loadBitstampSeries();
    // Building the trading strategy
    Strategy strategy = buildStrategy(series);
    // Running the strategy
    BarSeriesManager seriesManager = new BarSeriesManager(series);
    TradingRecord tradingRecord = seriesManager.run(strategy);
    System.out.println("Number of positions for the strategy: " + tradingRecord.getPositionCount());
    // Analysis
    System.out.println("Total return for the strategy: " + new GrossReturnCriterion().calculate(series, tradingRecord));
}
Also used : BarSeriesManager(org.ta4j.core.BarSeriesManager) TradingRecord(org.ta4j.core.TradingRecord) GrossReturnCriterion(org.ta4j.core.analysis.criteria.pnl.GrossReturnCriterion) BarSeries(org.ta4j.core.BarSeries) BaseStrategy(org.ta4j.core.BaseStrategy) Strategy(org.ta4j.core.Strategy)

Example 3 with BarSeriesManager

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

the class StrategyExecutionLogging method main.

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

Example 4 with BarSeriesManager

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

the class UnstableIndicatorStrategy method test.

public static void test(String name, Stream<Double> closePrices) {
    // Getting the bar series
    BarSeries series = new BaseBarSeriesBuilder().withBars(closePrices.map(close -> new BaseBar(MINUTE, TIME, 0, 0, 0, close, 0)).collect(Collectors.toList())).build();
    // Building the trading strategy
    Strategy strategy = buildStrategy(series);
    // Running the strategy
    BarSeriesManager seriesManager = new BarSeriesManager(series);
    TradingRecord tradingRecord = seriesManager.run(strategy);
    System.out.println(name + " " + tradingRecord.getPositions());
}
Also used : Rule(org.ta4j.core.Rule) BarSeries(org.ta4j.core.BarSeries) CrossedUpIndicatorRule(org.ta4j.core.rules.CrossedUpIndicatorRule) ZonedDateTime(java.time.ZonedDateTime) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) BarSeriesManager(org.ta4j.core.BarSeriesManager) BaseBarSeriesBuilder(org.ta4j.core.BaseBarSeriesBuilder) Stream(java.util.stream.Stream) BaseStrategy(org.ta4j.core.BaseStrategy) CrossedDownIndicatorRule(org.ta4j.core.rules.CrossedDownIndicatorRule) Duration(java.time.Duration) BaseBar(org.ta4j.core.BaseBar) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) UnstableIndicator(org.ta4j.core.indicators.UnstableIndicator) Num(org.ta4j.core.num.Num) TradingRecord(org.ta4j.core.TradingRecord) Strategy(org.ta4j.core.Strategy) SMAIndicator(org.ta4j.core.indicators.SMAIndicator) Indicator(org.ta4j.core.Indicator) BarSeriesManager(org.ta4j.core.BarSeriesManager) BaseBarSeriesBuilder(org.ta4j.core.BaseBarSeriesBuilder) TradingRecord(org.ta4j.core.TradingRecord) BaseBar(org.ta4j.core.BaseBar) BarSeries(org.ta4j.core.BarSeries) BaseStrategy(org.ta4j.core.BaseStrategy) Strategy(org.ta4j.core.Strategy)

Example 5 with BarSeriesManager

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

the class StrategyAnalysis method main.

public static void main(String[] args) {
    // Getting the bar series
    BarSeries series = CsvTradesLoader.loadBitstampSeries();
    // Building the trading strategy
    Strategy strategy = MovingMomentumStrategy.buildStrategy(series);
    // Running the strategy
    BarSeriesManager seriesManager = new BarSeriesManager(series);
    TradingRecord tradingRecord = seriesManager.run(strategy);
    /*
         * Analysis criteria
         */
    // Total profit
    GrossReturnCriterion totalReturn = new GrossReturnCriterion();
    System.out.println("Total return: " + totalReturn.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 return (per bar): " + new AverageReturnPerBarCriterion().calculate(series, tradingRecord));
    // Number of positions
    System.out.println("Number of positions: " + new NumberOfPositionsCriterion().calculate(series, tradingRecord));
    // Profitable position ratio
    System.out.println("Winning positions ratio: " + new WinningPositionsRatioCriterion().calculate(series, tradingRecord));
    // Maximum drawdown
    System.out.println("Maximum drawdown: " + new MaximumDrawdownCriterion().calculate(series, tradingRecord));
    // Reward-risk ratio
    System.out.println("Return over maximum drawdown: " + new ReturnOverMaxDrawdownCriterion().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 return: " + new BuyAndHoldReturnCriterion().calculate(series, tradingRecord));
    // Total profit vs buy-and-hold
    System.out.println("Custom strategy return vs buy-and-hold strategy return: " + new VersusBuyAndHoldCriterion(totalReturn).calculate(series, tradingRecord));
}
Also used : TradingRecord(org.ta4j.core.TradingRecord) LinearTransactionCostCriterion(org.ta4j.core.analysis.criteria.LinearTransactionCostCriterion) NumberOfBarsCriterion(org.ta4j.core.analysis.criteria.NumberOfBarsCriterion) BuyAndHoldReturnCriterion(org.ta4j.core.analysis.criteria.BuyAndHoldReturnCriterion) GrossReturnCriterion(org.ta4j.core.analysis.criteria.pnl.GrossReturnCriterion) BarSeries(org.ta4j.core.BarSeries) AverageReturnPerBarCriterion(org.ta4j.core.analysis.criteria.AverageReturnPerBarCriterion) BarSeriesManager(org.ta4j.core.BarSeriesManager) VersusBuyAndHoldCriterion(org.ta4j.core.analysis.criteria.VersusBuyAndHoldCriterion) MaximumDrawdownCriterion(org.ta4j.core.analysis.criteria.MaximumDrawdownCriterion) NumberOfPositionsCriterion(org.ta4j.core.analysis.criteria.NumberOfPositionsCriterion) ReturnOverMaxDrawdownCriterion(org.ta4j.core.analysis.criteria.ReturnOverMaxDrawdownCriterion) WinningPositionsRatioCriterion(org.ta4j.core.analysis.criteria.WinningPositionsRatioCriterion) MovingMomentumStrategy(ta4jexamples.strategies.MovingMomentumStrategy) Strategy(org.ta4j.core.Strategy)

Aggregations

BarSeriesManager (org.ta4j.core.BarSeriesManager)18 Strategy (org.ta4j.core.Strategy)15 BarSeries (org.ta4j.core.BarSeries)14 TradingRecord (org.ta4j.core.TradingRecord)13 BaseStrategy (org.ta4j.core.BaseStrategy)12 GrossReturnCriterion (org.ta4j.core.analysis.criteria.pnl.GrossReturnCriterion)10 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)5 Num (org.ta4j.core.num.Num)5 AnalysisCriterion (org.ta4j.core.AnalysisCriterion)3 Rule (org.ta4j.core.Rule)3 MockBarSeries (org.ta4j.core.mocks.MockBarSeries)3 MovingMomentumStrategy (ta4jexamples.strategies.MovingMomentumStrategy)3 Test (org.junit.Test)2 ReturnOverMaxDrawdownCriterion (org.ta4j.core.analysis.criteria.ReturnOverMaxDrawdownCriterion)2 VersusBuyAndHoldCriterion (org.ta4j.core.analysis.criteria.VersusBuyAndHoldCriterion)2 WinningPositionsRatioCriterion (org.ta4j.core.analysis.criteria.WinningPositionsRatioCriterion)2 SMAIndicator (org.ta4j.core.indicators.SMAIndicator)2 DecimalNum (org.ta4j.core.num.DecimalNum)2 DecimalFormat (java.text.DecimalFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1