Ejemplo n.º 1
0
    def testRanOutOfCoins(self):
        class Strategy(BitstampTestStrategy):
            def __init__(self, feed, brk):
                BitstampTestStrategy.__init__(self, feed, brk)
                self.errors = 0
                self.bought = False

            def onBars(self, bars):
                if not self.bought:
                    self.limitOrder("BTC", 100, 0.1)
                    self.bought = True
                else:
                    try:
                        self.limitOrder("BTC", 100, -0.1)
                    except Exception:
                        self.errors += 1

        barFeed = BitstampTestLiveTradeFeed()
        barFeed.addTrade(datetime.datetime(2000, 1, 1), 1, 100, 10)
        barFeed.addTrade(datetime.datetime(2000, 1, 2), 1, 100, 10)
        barFeed.addTrade(datetime.datetime(2000, 1, 3), 1, 100, 10)

        brk = broker.PaperTradingBroker(10.05, barFeed)
        strat = Strategy(barFeed, brk)
        strat.run()

        self.assertEqual(strat.errors, 1)
        self.assertEqual(brk.getShares("BTC"), 0)
        self.assertEqual(brk.getCash(), 10)
Ejemplo n.º 2
0
    def testBuyWithoutCash(self):
        class Strategy(BitstampTestStrategy):
            def __init__(self, feed, brk):
                BitstampTestStrategy.__init__(self, feed, brk)
                self.errors = 0

            def onBars(self, bars):
                try:
                    self.limitOrder("BTC", 10, 1)
                except Exception:
                    self.errors += 1

        barFeed = BitstampTestLiveTradeFeed()
        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)

        brk = broker.PaperTradingBroker(0, barFeed)
        strat = Strategy(barFeed, brk)
        strat.run()

        self.assertEqual(strat.errors, 4)
        self.assertEqual(brk.getShares("BTC"), 0)
        self.assertEqual(brk.getCash(), 0)
Ejemplo n.º 3
0
    def testBuyAndSellWithPartialFill2(self):
        class Strategy(BitstampTestStrategy):
            def __init__(self, feed, brk):
                BitstampTestStrategy.__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 = BitstampTestLiveTradeFeed()
        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())
Ejemplo n.º 4
0
 def testInvalidOrders(self):
     barFeed = BitstampTestLiveTradeFeed()
     brk = broker.PaperTradingBroker(1000, barFeed)
     with self.assertRaises(Exception):
         brk.createLimitOrder(basebroker.Order.Action.BUY, "none", 1, 1)
     with self.assertRaises(Exception):
         brk.createLimitOrder(basebroker.Order.Action.SELL_SHORT, "none", 1,
                              1)
     with self.assertRaises(Exception):
         brk.createMarketOrder(basebroker.Order.Action.BUY, "none", 1)
     with self.assertRaises(Exception):
         brk.createStopOrder(basebroker.Order.Action.BUY, "none", 1, 1)
     with self.assertRaises(Exception):
         brk.createStopLimitOrder(basebroker.Order.Action.BUY, "none", 1, 1,
                                  1)
Ejemplo n.º 5
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(BitstampTestStrategy):
            def __init__(self, feed, brk):
                BitstampTestStrategy.__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 = BitstampTestLiveTradeFeed()
        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)