Example #1
0
    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())
Example #2
0
    def testBuyAndSellWithPartialFill1(self):

        class Strategy(TestStrategy):
            def __init__(self, cli, feed, brk):
                TestStrategy.__init__(self, cli, 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.exit(limitPrice=101)

        cli = MockClient()
        cli.addTrade(datetime.datetime(2000, 1, 1), 1, 100, 0.1)
        cli.addTrade(datetime.datetime(2000, 1, 2), 1, 100, 0.1)
        cli.addTrade(datetime.datetime(2000, 1, 2), 1, 101, 10)
        cli.addTrade(datetime.datetime(2000, 1, 3), 1, 100, 0.2)
        cli.addTrade(datetime.datetime(2000, 1, 4), 1, 100, 0.2)
        cli.addTrade(datetime.datetime(2000, 1, 5), 1, 101, 0.2)

        barFeed = barfeed.LiveTradeFeed(cli)
        brk = broker.PaperTradingBroker(1000, barFeed)
        strat = Strategy(cli, barFeed, brk)

        strat.getDispatcher().addSubject(cli)
        strat.run()

        self.assertTrue(strat.pos.isOpen())
        self.assertEqual(round(strat.pos.getShares(), 3), 0.1)
        self.assertEqual(len(strat.posExecutionInfo), 1)
        self.assertEqual(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
        self.assertEqual(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
Example #3
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(TestStrategy):
            def __init__(self, cli, feed, brk):
                TestStrategy.__init__(self, cli, feed, brk)
                self.pos = None

            def onBars(self, bars):
                if self.pos is None:
                    self.pos = self.enterLongLimit("BTC", 100, 0.01, True)
                elif self.pos.entryFilled() and not self.pos.getExitOrder():
                    self.pos.exitLimit(100, True)

        cli = MockClient()
        cli.addTrade(datetime.datetime(2000, 1, 1), 1, 100, 1)
        cli.addTrade(datetime.datetime(2000, 1, 2), 1, 100, 0.01)
        cli.addTrade(datetime.datetime(2000, 1, 3), 1, 100, 0.00441376)
        cli.addTrade(datetime.datetime(2000, 1, 4), 1, 100, 0.00445547)
        cli.addTrade(datetime.datetime(2000, 1, 5), 1, 100, 0.00113077)

        barFeed = barfeed.LiveTradeFeed(cli)
        brk = broker.PaperTradingBroker(1000, barFeed)
        strat = Strategy(cli, barFeed, brk)

        strat.getDispatcher().addSubject(cli)
        strat.run()

        self.assertEqual(brk.getShares("BTC"), 0)
        self.assertEqual(strat.pos.getEntryOrder().getAvgFillPrice(), 100)
        self.assertEqual(strat.pos.getExitOrder().getAvgFillPrice(), 100)
        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)
Example #4
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(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)
Example #5
0
 def getCurrentDateTime(self):
     return wsclient.get_current_datetime()
Example #6
0
 def getCurrentDateTime(self):
     return wsclient.get_current_datetime()