Search in sources :

Example 1 with TradingRecord

use of org.ta4j.core.TradingRecord 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 2 with TradingRecord

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

the class CashFlowTest method cashFlowValueWithOnlyOneTradeAndAGapBefore.

@Test
public void cashFlowValueWithOnlyOneTradeAndAGapBefore() {
    TimeSeries sampleTimeSeries = new MockTimeSeries(1d, 1d, 2d);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(1, sampleTimeSeries), Order.sellAt(2, sampleTimeSeries));
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertDecimalEquals(cashFlow.getValue(0), 1);
    assertDecimalEquals(cashFlow.getValue(1), 1);
    assertDecimalEquals(cashFlow.getValue(2), 2);
}
Also used : BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TradingRecord(org.ta4j.core.TradingRecord) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 3 with TradingRecord

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

the class CashFlowTest method cashFlowBuyWithOnlyOneTrade.

@Test
public void cashFlowBuyWithOnlyOneTrade() {
    TimeSeries sampleTimeSeries = new MockTimeSeries(1d, 2d);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, sampleTimeSeries), Order.sellAt(1, sampleTimeSeries));
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertDecimalEquals(cashFlow.getValue(0), 1);
    assertDecimalEquals(cashFlow.getValue(1), 2);
}
Also used : BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TradingRecord(org.ta4j.core.TradingRecord) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 4 with TradingRecord

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

the class CashFlowTest method cashFlowShortSell.

@Test
public void cashFlowShortSell() {
    TimeSeries sampleTimeSeries = new MockTimeSeries(1, 2, 4, 8, 16, 32);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, sampleTimeSeries), Order.sellAt(2, sampleTimeSeries), Order.sellAt(2, sampleTimeSeries), Order.buyAt(4, sampleTimeSeries), Order.buyAt(4, sampleTimeSeries), Order.sellAt(5, sampleTimeSeries));
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertDecimalEquals(cashFlow.getValue(0), 1);
    assertDecimalEquals(cashFlow.getValue(1), 2);
    assertDecimalEquals(cashFlow.getValue(2), 4);
    assertDecimalEquals(cashFlow.getValue(3), 2);
    assertDecimalEquals(cashFlow.getValue(4), 1);
    assertDecimalEquals(cashFlow.getValue(5), 2);
}
Also used : BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TradingRecord(org.ta4j.core.TradingRecord) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 5 with TradingRecord

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

the class CashFlowTest method cashFlowValue.

@Test
public void cashFlowValue() {
    // First sample series
    TimeSeries sampleTimeSeries = new MockTimeSeries(3d, 2d, 5d, 1000d, 5000d, 0.0001d, 4d, 7d, 6d, 7d, 8d, 5d, 6d);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, sampleTimeSeries), Order.sellAt(2, sampleTimeSeries), Order.buyAt(6, sampleTimeSeries), Order.sellAt(8, sampleTimeSeries), Order.buyAt(9, sampleTimeSeries), Order.sellAt(11, sampleTimeSeries));
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertDecimalEquals(cashFlow.getValue(0), 1);
    assertDecimalEquals(cashFlow.getValue(1), 2d / 3);
    assertDecimalEquals(cashFlow.getValue(2), 5d / 3);
    assertDecimalEquals(cashFlow.getValue(3), 5d / 3);
    assertDecimalEquals(cashFlow.getValue(4), 5d / 3);
    assertDecimalEquals(cashFlow.getValue(5), 5d / 3);
    assertDecimalEquals(cashFlow.getValue(6), 5d / 3);
    assertDecimalEquals(cashFlow.getValue(7), 5d / 3 * 7d / 4);
    assertDecimalEquals(cashFlow.getValue(8), 5d / 3 * 6d / 4);
    assertDecimalEquals(cashFlow.getValue(9), 5d / 3 * 6d / 4);
    assertDecimalEquals(cashFlow.getValue(10), 5d / 3 * 6d / 4 * 8d / 7);
    assertDecimalEquals(cashFlow.getValue(11), 5d / 3 * 6d / 4 * 5d / 7);
    assertDecimalEquals(cashFlow.getValue(12), 5d / 3 * 6d / 4 * 5d / 7);
    // Second sample series
    sampleTimeSeries = new MockTimeSeries(5d, 6d, 3d, 7d, 8d, 6d, 10d, 15d, 6d);
    tradingRecord = new BaseTradingRecord(Order.buyAt(4, sampleTimeSeries), Order.sellAt(5, sampleTimeSeries), Order.buyAt(6, sampleTimeSeries), Order.sellAt(8, sampleTimeSeries));
    CashFlow flow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertDecimalEquals(flow.getValue(0), 1);
    assertDecimalEquals(flow.getValue(1), 1);
    assertDecimalEquals(flow.getValue(2), 1);
    assertDecimalEquals(flow.getValue(3), 1);
    assertDecimalEquals(flow.getValue(4), 1);
    assertDecimalEquals(flow.getValue(5), "0.75");
    assertDecimalEquals(flow.getValue(6), "0.75");
    assertDecimalEquals(flow.getValue(7), "1.125");
    assertDecimalEquals(flow.getValue(8), "0.45");
}
Also used : BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TimeSeries(org.ta4j.core.TimeSeries) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseTradingRecord(org.ta4j.core.BaseTradingRecord) TradingRecord(org.ta4j.core.TradingRecord) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Aggregations

TradingRecord (org.ta4j.core.TradingRecord)14 Test (org.junit.Test)13 BaseTradingRecord (org.ta4j.core.BaseTradingRecord)13 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)13 TimeSeries (org.ta4j.core.TimeSeries)11 ExternalCriterionTest (org.ta4j.core.ExternalCriterionTest)3 AnalysisCriterion (org.ta4j.core.AnalysisCriterion)1 Strategy (org.ta4j.core.Strategy)1 TimeSeriesManager (org.ta4j.core.TimeSeriesManager)1 MockBar (org.ta4j.core.mocks.MockBar)1 MovingMomentumStrategy (ta4jexamples.strategies.MovingMomentumStrategy)1