Search in sources :

Example 66 with MockTimeSeries

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

the class CashFlowTest method cashFlowWithSellAndBuyOrders.

@Test
public void cashFlowWithSellAndBuyOrders() {
    TimeSeries sampleTimeSeries = new MockTimeSeries(2, 1, 3, 5, 6, 3, 20);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, sampleTimeSeries), Order.sellAt(1, sampleTimeSeries), Order.buyAt(3, sampleTimeSeries), Order.sellAt(4, sampleTimeSeries), Order.sellAt(5, sampleTimeSeries), Order.buyAt(6, sampleTimeSeries));
    CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);
    assertDecimalEquals(cashFlow.getValue(0), 1);
    assertDecimalEquals(cashFlow.getValue(1), "0.5");
    assertDecimalEquals(cashFlow.getValue(2), "0.5");
    assertDecimalEquals(cashFlow.getValue(3), "0.5");
    assertDecimalEquals(cashFlow.getValue(4), "0.6");
    assertDecimalEquals(cashFlow.getValue(5), "0.6");
    assertDecimalEquals(cashFlow.getValue(6), "0.09");
}
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 67 with MockTimeSeries

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

the class AbstractAnalysisCriterionTest method bestShouldBeBuyAndHoldOnLoss.

@Test
public void bestShouldBeBuyAndHoldOnLoss() {
    MockTimeSeries series = new MockTimeSeries(6.0, 3.0, 6.0, 6.0);
    TimeSeriesManager manager = new TimeSeriesManager(series);
    Strategy bestStrategy = new TotalProfitCriterion().chooseBest(manager, strategies);
    assertEquals(buyAndHoldStrategy, bestStrategy);
}
Also used : TimeSeriesManager(org.ta4j.core.TimeSeriesManager) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseStrategy(org.ta4j.core.BaseStrategy) Strategy(org.ta4j.core.Strategy) Test(org.junit.Test)

Example 68 with MockTimeSeries

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

the class AbstractAnalysisCriterionTest method bestShouldBeAlwaysOperateOnProfit.

@Test
public void bestShouldBeAlwaysOperateOnProfit() {
    MockTimeSeries series = new MockTimeSeries(6.0, 9.0, 6.0, 6.0);
    TimeSeriesManager manager = new TimeSeriesManager(series);
    Strategy bestStrategy = new TotalProfitCriterion().chooseBest(manager, strategies);
    assertEquals(alwaysStrategy, bestStrategy);
}
Also used : TimeSeriesManager(org.ta4j.core.TimeSeriesManager) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) BaseStrategy(org.ta4j.core.BaseStrategy) Strategy(org.ta4j.core.Strategy) Test(org.junit.Test)

Example 69 with MockTimeSeries

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

the class AverageProfitCriterionTest method calculateOnlyWithGainTrades.

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

Example 70 with MockTimeSeries

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

the class AverageProfitCriterionTest method calculateWithASimpleTrade.

@Test
public void calculateWithASimpleTrade() {
    series = new MockTimeSeries(100d, 105d, 110d, 100d, 95d, 105d);
    TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(2, series));
    AnalysisCriterion averageProfit = new AverageProfitCriterion();
    assertEquals(Math.pow(110d / 100, 1d / 3), averageProfit.calculate(series, tradingRecord), 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