Search in sources :

Example 96 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.

the class RewardRiskRatioCriterionTest method withOneTrade.

@Test
public void withOneTrade() {
    MockTimeSeries series = new MockTimeSeries(100, 95, 95, 100, 90, 95, 80, 120);
    Trade trade = new Trade(Order.buyAt(0, series), Order.sellAt(1, series));
    RewardRiskRatioCriterion ratioCriterion = new RewardRiskRatioCriterion();
    assertEquals((95d / 100) / ((1d - 0.95d)), TATestsUtils.TA_OFFSET, ratioCriterion.calculate(series, trade));
}
Also used : MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 97 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.

the class TotalProfitCriterionTest method calculateWithOpenedTradeShouldReturn1.

@Test
public void calculateWithOpenedTradeShouldReturn1() {
    MockTimeSeries series = new MockTimeSeries(100, 95, 100, 80, 85, 70);
    AnalysisCriterion profit = new TotalProfitCriterion();
    Trade trade = new Trade();
    assertEquals(1d, profit.calculate(series, trade), TATestsUtils.TA_OFFSET);
    trade.operate(0);
    assertEquals(1d, profit.calculate(series, trade), TATestsUtils.TA_OFFSET);
}
Also used : MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 98 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.

the class TotalProfitCriterionTest method calculateOnlyWithGainTrades.

@Test
public void calculateOnlyWithGainTrades() {
    MockTimeSeries series = new MockTimeSeries(100, 105, 110, 100, 95, 105);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(2, series), Order.buyAt(3, series), Order.sellAt(5, series));
    AnalysisCriterion profit = new TotalProfitCriterion();
    assertEquals(1.10 * 1.05, profit.calculate(series, tradingRecord), TATestsUtils.TA_OFFSET);
}
Also used : MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 99 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.

the class VersusBuyAndHoldCriterionTest method calculateWithAverageProfit.

@Test
public void calculateWithAverageProfit() {
    MockTimeSeries series = new MockTimeSeries(100, 95, 100, 80, 85, 130);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, Decimal.NaN, Decimal.NaN), Order.sellAt(1, Decimal.NaN, Decimal.NaN), Order.buyAt(2, Decimal.NaN, Decimal.NaN), Order.sellAt(5, Decimal.NaN, Decimal.NaN));
    AnalysisCriterion buyAndHold = new VersusBuyAndHoldCriterion(new AverageProfitCriterion());
    assertEquals(Math.pow(95d / 100 * 130d / 100, 1d / 6) / Math.pow(130d / 100, 1d / 6), buyAndHold.calculate(series, tradingRecord), TATestsUtils.TA_OFFSET);
}
Also used : MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 100 with MockTimeSeries

use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.

the class VersusBuyAndHoldCriterionTest method calculateWithOnlyOneTrade.

@Test
public void calculateWithOnlyOneTrade() {
    MockTimeSeries series = new MockTimeSeries(100, 95, 100, 80, 85, 70);
    Trade trade = new Trade(Order.buyAt(0, series), Order.sellAt(1, series));
    AnalysisCriterion buyAndHold = new VersusBuyAndHoldCriterion(new TotalProfitCriterion());
    assertEquals((100d / 70) / (100d / 95), buyAndHold.calculate(series, trade), TATestsUtils.TA_OFFSET);
}
Also used : MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Aggregations

MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)135 Test (org.junit.Test)90 MockBar (org.ta4j.core.mocks.MockBar)46 ArrayList (java.util.ArrayList)45 Before (org.junit.Before)45 Bar (org.ta4j.core.Bar)42 TimeSeries (org.ta4j.core.TimeSeries)29 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)25 BaseTradingRecord (org.ta4j.core.BaseTradingRecord)17 TradingRecord (org.ta4j.core.TradingRecord)12 ZonedDateTime (java.time.ZonedDateTime)5 AnalysisCriterion (org.ta4j.core.AnalysisCriterion)3 BaseStrategy (org.ta4j.core.BaseStrategy)3 ExternalCriterionTest (org.ta4j.core.ExternalCriterionTest)3 Strategy (org.ta4j.core.Strategy)3 Decimal (org.ta4j.core.Decimal)2 TimeSeriesManager (org.ta4j.core.TimeSeriesManager)2 Trade (org.ta4j.core.Trade)2 SMAIndicator (org.ta4j.core.indicators.SMAIndicator)2 FixedRule (org.ta4j.core.trading.rules.FixedRule)2