use of org.ta4j.core.mocks.MockTimeSeries 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);
}
use of org.ta4j.core.mocks.MockTimeSeries 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");
}
use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.
the class CashFlowTest method cashFlowValueWithNoTrades.
@Test
public void cashFlowValueWithNoTrades() {
TimeSeries sampleTimeSeries = new MockTimeSeries(3d, 2d, 5d, 4d, 7d, 6d, 7d, 8d, 5d, 6d);
CashFlow cashFlow = new CashFlow(sampleTimeSeries, new BaseTradingRecord());
assertDecimalEquals(cashFlow.getValue(4), 1);
assertDecimalEquals(cashFlow.getValue(7), 1);
assertDecimalEquals(cashFlow.getValue(9), 1);
}
use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.
the class AverageProfitCriterionTest method calculateWithNoBarsShouldReturn1.
@Test
public void calculateWithNoBarsShouldReturn1() {
series = new MockTimeSeries(100, 95, 100, 80, 85, 70);
AnalysisCriterion averageProfit = new AverageProfitCriterion();
assertEquals(1d, averageProfit.calculate(series, new BaseTradingRecord()), TATestsUtils.TA_OFFSET);
}
use of org.ta4j.core.mocks.MockTimeSeries in project ta4j by ta4j.
the class AverageProfitCriterionTest method calculateOnlyWithLossTrades.
@Test
public void calculateOnlyWithLossTrades() {
series = new MockTimeSeries(100, 95, 100, 80, 85, 70);
TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(1, series), Order.buyAt(2, series), Order.sellAt(5, series));
AnalysisCriterion averageProfit = new AverageProfitCriterion();
assertEquals(Math.pow(95d / 100 * 70d / 100, 1d / 6), averageProfit.calculate(series, tradingRecord), TATestsUtils.TA_OFFSET);
}
Aggregations