示例#1
0
def main():
    bf = membf.BarFeed(bar.Frequency.DAY)
    bars = [
        bar.BasicBar(datetime.datetime(2000, 1, 1), 10, 10, 10, 10, 10, 10,
                     bar.Frequency.DAY),
    ]
    bf.addBarsFromSequence("orcl", bars)

    strat = Strategy(bf, 1000)
    strat.run()
示例#2
0
def main():
    bf = membf.BarFeed(bar.Frequency.DAY)
    bars = [
        bar.BasicBar(datetime.datetime(2000, 1, 1), 10, 10, 10, 10, 10, 10,
                     bar.Frequency.DAY),
        bar.BasicBar(datetime.datetime(2000, 1, 2), 10, 10, 10, 10, 10, 10,
                     bar.Frequency.DAY),
    ]
    bf.addBarsFromSequence("orcl", bars)

    logger.getLogger().setLevel(logging.DEBUG)
    strat = BacktestingStrategy(bf, 1)
    strat.run()
示例#3
0
def load_pyalgotrade_daily_bars(instrument, barType, fromDateTime, toDateTime):
    assert (barType == persistence.Bar.Type.DAILY)
    # Load pyalgotrade.bar.Bar objects from the db.
    dbBars = persistence.Bar.getBars(instrument, barType, fromDateTime,
                                     toDateTime)
    bars = [ds_bar_to_pyalgotrade_bar(dbBar) for dbBar in dbBars]

    # Use a feed to build pyalgotrade.bar.Bars objects.
    feed = membf.BarFeed(bar.Frequency.DAY)
    feed.addBarsFromSequence(instrument, bars)
    ret = []
    for dateTime, bars in feed:
        ret.append(bars)
    return ret
示例#4
0
    def __testManualImpl(self, closingPrices, cash):
        barFeed = membf.BarFeed(barfeed.Frequency.DAY)
        bars = build_bars_from_closing_prices(closingPrices)
        barFeed.addBarsFromSequence("orcl", bars)

        strat = strategy_test.TestStrategy(barFeed, cash)
        stratAnalyzer = drawdown.DrawDown()
        strat.attachAnalyzer(stratAnalyzer)

        # Manually place the order to get it filled on the first bar.
        order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY,
                                                    "orcl", 1, True)
        order.setGoodTillCanceled(True)
        strat.getBroker().placeOrder(order)

        strat.run()
        return stratAnalyzer
示例#5
0
    def testPartialFillGTCOpenAndClose2(self):
        instrument = "orcl"
        bf = membf.BarFeed(bar.Frequency.DAY)
        bars = [
            bar.BasicBar(datetime.datetime(2000, 1, 1), 10, 10, 10, 10, 10, 10,
                         bar.Frequency.DAY),
            bar.BasicBar(datetime.datetime(2000, 1, 2), 11, 11, 10, 10, 10, 10,
                         bar.Frequency.DAY),
            bar.BasicBar(datetime.datetime(2000, 1, 3), 12, 12, 10, 10, 10, 10,
                         bar.Frequency.DAY),
            bar.BasicBar(datetime.datetime(2000, 1, 4), 13, 13, 10, 10, 10, 10,
                         bar.Frequency.DAY),
            bar.BasicBar(datetime.datetime(2000, 1, 5), 14, 14, 10, 10, 10, 10,
                         bar.Frequency.DAY),
        ]
        bf.addBarsFromSequence(instrument, bars)
        strat = TestStrategy(bf, instrument, 1000)
        strat.addPosEntry(datetime.datetime(2000, 1, 1), strat.enterLong,
                          instrument, 4, True)
        # Exit the position before the entry order gets completely filled.
        strat.addPosExit(datetime.datetime(2000, 1, 2))
        strat.run()

        self.assertEqual(strat.positions[0].isOpen(), False)
        self.assertEqual(strat.enterOkCalls, 1)
        self.assertEqual(strat.enterCanceledCalls, 0)
        self.assertEqual(strat.exitOkCalls, 1)
        self.assertEqual(strat.exitCanceledCalls, 0)

        self.assertEqual(len(strat.posExecutionInfo), 2)
        self.assertTrue(strat.positions[0].getEntryOrder().isCanceled())
        self.assertTrue(strat.positions[0].getExitOrder().isFilled())
        self.assertEqual(strat.positions[0].getShares(), 0)

        self.assertEqual(strat.posExecutionInfo[0].getPrice(), 11)
        self.assertEqual(strat.posExecutionInfo[0].getQuantity(), 2)
        self.assertEqual(strat.posExecutionInfo[0].getCommission(), 0)
        self.assertEqual(strat.posExecutionInfo[0].getDateTime(),
                         datetime.datetime(2000, 1, 2))

        self.assertEqual(strat.posExecutionInfo[1].getPrice(), 12)
        self.assertEqual(strat.posExecutionInfo[1].getQuantity(), 2)
        self.assertEqual(strat.posExecutionInfo[1].getCommission(), 0)
        self.assertEqual(strat.posExecutionInfo[1].getDateTime(),
                         datetime.datetime(2000, 1, 3))