Search in sources :

Example 1 with FixedRule

use of org.ta4j.core.trading.rules.FixedRule in project ta4j by ta4j.

the class AbstractAnalysisCriterionTest method setUp.

@Before
public void setUp() {
    alwaysStrategy = new BaseStrategy(BooleanRule.TRUE, BooleanRule.TRUE);
    buyAndHoldStrategy = new BaseStrategy(new FixedRule(0), new FixedRule(4));
    strategies = new ArrayList<Strategy>();
    strategies.add(alwaysStrategy);
    strategies.add(buyAndHoldStrategy);
}
Also used : BaseStrategy(org.ta4j.core.BaseStrategy) BaseStrategy(org.ta4j.core.BaseStrategy) Strategy(org.ta4j.core.Strategy) FixedRule(org.ta4j.core.trading.rules.FixedRule) Before(org.junit.Before)

Example 2 with FixedRule

use of org.ta4j.core.trading.rules.FixedRule in project ta4j by ta4j.

the class TimeSeriesManagerTest method runWithOpenEntryBuyLeft.

@Test
public void runWithOpenEntryBuyLeft() {
    Strategy aStrategy = new BaseStrategy(new FixedRule(1), new FixedRule(3));
    List<Trade> trades = manager.run(aStrategy, 0, 3).getTrades();
    assertEquals(1, trades.size());
    assertEquals(Order.buyAt(1, seriesForRun.getBar(1).getClosePrice(), Decimal.NaN), trades.get(0).getEntry());
    assertEquals(Order.sellAt(3, seriesForRun.getBar(3).getClosePrice(), Decimal.NaN), trades.get(0).getExit());
}
Also used : FixedRule(org.ta4j.core.trading.rules.FixedRule) Test(org.junit.Test)

Example 3 with FixedRule

use of org.ta4j.core.trading.rules.FixedRule in project ta4j by ta4j.

the class TimeSeriesManagerTest method setUp.

@Before
public void setUp() {
    final DateTimeFormatter dtf = DateTimeFormatter.ISO_ZONED_DATE_TIME;
    seriesForRun = new MockTimeSeries(new double[] { 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d }, new ZonedDateTime[] { ZonedDateTime.parse("2013-01-01T00:00:00-05:00", dtf), ZonedDateTime.parse("2013-08-01T00:00:00-05:00", dtf), ZonedDateTime.parse("2013-10-01T00:00:00-05:00", dtf), ZonedDateTime.parse("2013-12-01T00:00:00-05:00", dtf), ZonedDateTime.parse("2014-02-01T00:00:00-05:00", dtf), ZonedDateTime.parse("2015-01-01T00:00:00-05:00", dtf), ZonedDateTime.parse("2015-08-01T00:00:00-05:00", dtf), ZonedDateTime.parse("2015-10-01T00:00:00-05:00", dtf), ZonedDateTime.parse("2015-12-01T00:00:00-05:00", dtf) });
    manager = new TimeSeriesManager(seriesForRun);
    strategy = new BaseStrategy(new FixedRule(0, 2, 3, 6), new FixedRule(1, 4, 7, 8));
    // Strategy would need a real test class
    strategy.setUnstablePeriod(2);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) DateTimeFormatter(java.time.format.DateTimeFormatter) FixedRule(org.ta4j.core.trading.rules.FixedRule) Before(org.junit.Before)

Example 4 with FixedRule

use of org.ta4j.core.trading.rules.FixedRule in project ta4j by ta4j.

the class TimeSeriesManagerTest method runOnSeriesSlices.

@Test
public void runOnSeriesSlices() {
    ZonedDateTime dateTime = ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
    TimeSeries series = new MockTimeSeries(new double[] { 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d, 10d }, new ZonedDateTime[] { dateTime.withYear(2000), dateTime.withYear(2000), dateTime.withYear(2001), dateTime.withYear(2001), dateTime.withYear(2002), dateTime.withYear(2002), dateTime.withYear(2002), dateTime.withYear(2003), dateTime.withYear(2004), dateTime.withYear(2005) });
    manager.setTimeSeries(series);
    Strategy aStrategy = new BaseStrategy(new FixedRule(0, 3, 5, 7), new FixedRule(2, 4, 6, 9));
    List<Trade> trades = manager.run(aStrategy, 0, 1).getTrades();
    assertEquals(1, trades.size());
    assertEquals(Order.buyAt(0, series.getBar(0).getClosePrice(), Decimal.NaN), trades.get(0).getEntry());
    assertEquals(Order.sellAt(2, series.getBar(2).getClosePrice(), Decimal.NaN), trades.get(0).getExit());
    trades = manager.run(aStrategy, 2, 3).getTrades();
    assertEquals(1, trades.size());
    assertEquals(Order.buyAt(3, series.getBar(3).getClosePrice(), Decimal.NaN), trades.get(0).getEntry());
    assertEquals(Order.sellAt(4, series.getBar(4).getClosePrice(), Decimal.NaN), trades.get(0).getExit());
    trades = manager.run(aStrategy, 4, 6).getTrades();
    assertEquals(1, trades.size());
    assertEquals(Order.buyAt(5, series.getBar(5).getClosePrice(), Decimal.NaN), trades.get(0).getEntry());
    assertEquals(Order.sellAt(6, series.getBar(6).getClosePrice(), Decimal.NaN), trades.get(0).getExit());
    trades = manager.run(aStrategy, 7, 7).getTrades();
    assertEquals(1, trades.size());
    assertEquals(Order.buyAt(7, series.getBar(7).getClosePrice(), Decimal.NaN), trades.get(0).getEntry());
    assertEquals(Order.sellAt(9, series.getBar(9).getClosePrice(), Decimal.NaN), trades.get(0).getExit());
    trades = manager.run(aStrategy, 8, 8).getTrades();
    assertTrue(trades.isEmpty());
    trades = manager.run(aStrategy, 9, 9).getTrades();
    assertTrue(trades.isEmpty());
}
Also used : MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ZonedDateTime(java.time.ZonedDateTime) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) FixedRule(org.ta4j.core.trading.rules.FixedRule) Test(org.junit.Test)

Example 5 with FixedRule

use of org.ta4j.core.trading.rules.FixedRule in project ta4j by ta4j.

the class TimeSeriesManagerTest method runWithOpenEntrySellLeft.

@Test
public void runWithOpenEntrySellLeft() {
    Strategy aStrategy = new BaseStrategy(new FixedRule(1), new FixedRule(3));
    List<Trade> trades = manager.run(aStrategy, OrderType.SELL, 0, 3).getTrades();
    assertEquals(1, trades.size());
    assertEquals(Order.sellAt(1, seriesForRun.getBar(1).getClosePrice(), Decimal.NaN), trades.get(0).getEntry());
    assertEquals(Order.buyAt(3, seriesForRun.getBar(3).getClosePrice(), Decimal.NaN), trades.get(0).getExit());
}
Also used : FixedRule(org.ta4j.core.trading.rules.FixedRule) Test(org.junit.Test)

Aggregations

FixedRule (org.ta4j.core.trading.rules.FixedRule)6 Before (org.junit.Before)3 Test (org.junit.Test)3 ZonedDateTime (java.time.ZonedDateTime)2 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)2 DateTimeFormatter (java.time.format.DateTimeFormatter)1 BaseStrategy (org.ta4j.core.BaseStrategy)1 Strategy (org.ta4j.core.Strategy)1 MockBar (org.ta4j.core.mocks.MockBar)1