def testBuyAndSellWithPartialFill2(self): class Strategy(TestStrategy): def __init__(self, feed, brk): TestStrategy.__init__(self, feed, brk) self.pos = None def onBars(self, bars): if self.pos is None: self.pos = self.enterLongLimit("BTC", 100, 1, True) elif bars.getDateTime() == datetime.datetime(2000, 1, 3): self.pos.exitLimit(101) barFeed = TestingLiveTradeFeed() barFeed.addTrade(datetime.datetime(2000, 1, 1), 1, 100, 0.1) barFeed.addTrade(datetime.datetime(2000, 1, 2), 1, 100, 0.1) barFeed.addTrade(datetime.datetime(2000, 1, 2), 1, 101, 10) barFeed.addTrade(datetime.datetime(2000, 1, 3), 1, 100, 0.2) barFeed.addTrade(datetime.datetime(2000, 1, 4), 1, 100, 0.2) barFeed.addTrade(datetime.datetime(2000, 1, 5), 1, 101, 0.2) barFeed.addTrade(datetime.datetime(2000, 1, 6), 1, 102, 5) brk = broker.PaperTradingBroker(1000, barFeed) strat = Strategy(barFeed, brk) strat.run() self.assertFalse(strat.pos.isOpen()) self.assertEquals(strat.pos.getShares(), 0) self.assertEquals(len(strat.posExecutionInfo), 2) self.assertEquals(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date()) self.assertEquals(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
def testRoundingBugWithTrades(self): # Unless proper rounding is in place 0.01 - 0.00441376 - 0.00445547 - 0.00113077 == 6.50521303491e-19 # instead of 0. class Strategy(TestStrategy): def __init__(self, feed, brk): TestStrategy.__init__(self, feed, brk) self.pos = None def onBars(self, bars): if self.pos is None: self.pos = self.enterLongLimit("BTC", 1000, 0.01, True) elif self.pos.entryFilled() and not self.pos.getExitOrder(): self.pos.exitLimit(1000, True) barFeed = TestingLiveTradeFeed() barFeed.addTrade(datetime.datetime(2000, 1, 1), 1, 1000, 1) barFeed.addTrade(datetime.datetime(2000, 1, 2), 1, 1000, 0.01) barFeed.addTrade(datetime.datetime(2000, 1, 3), 1, 1000, 0.00441376) barFeed.addTrade(datetime.datetime(2000, 1, 4), 1, 1000, 0.00445547) barFeed.addTrade(datetime.datetime(2000, 1, 5), 1, 1000, 0.00113077) brk = broker.PaperTradingBroker(1000, barFeed) strat = Strategy(barFeed, brk) strat.run() self.assertEquals(brk.getShares("BTC"), 0) self.assertEquals(strat.pos.getEntryOrder().getAvgFillPrice(), 1000) self.assertEquals(strat.pos.getExitOrder().getAvgFillPrice(), 1000) self.assertEquals(strat.pos.getEntryOrder().getFilled(), 0.01) self.assertEquals(strat.pos.getExitOrder().getFilled(), 0.01) self.assertEquals(strat.pos.getEntryOrder().getRemaining(), 0) self.assertEquals(strat.pos.getExitOrder().getRemaining(), 0) self.assertEquals(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date()) self.assertEquals(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date()) self.assertFalse(strat.pos.isOpen()) self.assertEquals(len(strat.posExecutionInfo), 2) self.assertEquals(strat.pos.getShares(), 0.0)
def getCurrentDateTime(self): return wsclient.get_current_datetime()