Exemple #1
0
    def testBuyAndSellWithPartialFill2(self):
        class Strategy(MockStrategy):
            def __init__(self, feed, brk):
                super().__init__(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 = MockLiveTradeFeed()
        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.assertEqual(strat.pos.getShares(), 0)
        self.assertEqual(len(strat.posExecutionInfo), 2)
        self.assertEqual(strat.pos.getEntryOrder().getSubmitDateTime().date(),
                         wsclient.get_current_datetime().date())
        self.assertEqual(strat.pos.getExitOrder().getSubmitDateTime().date(),
                         wsclient.get_current_datetime().date())
Exemple #2
0
    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(MockStrategy):
            def __init__(self, feed, brk):
                super().__init__(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 = MockLiveTradeFeed()
        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.assertEqual(brk.getShares("BTC"), 0)
        self.assertEqual(strat.pos.getEntryOrder().getAvgFillPrice(), 1000)
        self.assertEqual(strat.pos.getExitOrder().getAvgFillPrice(), 1000)
        self.assertEqual(strat.pos.getEntryOrder().getFilled(), 0.01)
        self.assertEqual(strat.pos.getExitOrder().getFilled(), 0.01)

        self.assertEqual(strat.pos.getEntryOrder().getRemaining(), 0)
        self.assertEqual(strat.pos.getExitOrder().getRemaining(), 0)
        self.assertEqual(strat.pos.getEntryOrder().getSubmitDateTime().date(),
                         wsclient.get_current_datetime().date())
        self.assertEqual(strat.pos.getExitOrder().getSubmitDateTime().date(),
                         wsclient.get_current_datetime().date())

        self.assertFalse(strat.pos.isOpen())
        self.assertEqual(len(strat.posExecutionInfo), 2)
        self.assertEqual(strat.pos.getShares(), 0.0)
Exemple #3
0
 def getCurrentDateTime(self):
     return wsclient.get_current_datetime()