Exemplo n.º 1
0
    def testCliDonchianAlgorithmWithSpecificDates(self):
        api = PytradeApi(dbfilepah=self.db)
        api.reinitializeUser(username=self.username, cash=self.initialCash)
        tradingAlgorithmGenerator = lambda feed, broker: DonchianTradingAlgorithm(feed, broker, 9, 26, 0.05)

        utc = pytz.utc
        specificdays = [
            utc.localize(datetime.datetime(2014, 2, 7)),
            utc.localize(datetime.datetime(2014, 2, 11)),
            utc.localize(datetime.datetime(2014, 9, 18)),
            utc.localize(datetime.datetime(2014, 10, 23)),
            utc.localize(datetime.datetime(2014, 10, 28)),
            utc.localize(datetime.datetime(2014, 12, 29))]
        feed = DynamicFeed(self.db, self.codes, maxLen=self.maxLen)
        alldays = feed.getAllDays()
        for day in specificdays:
            cli = PytradeCli(dbfilepah=self.db, date=day, maxlen=self.maxLen, codes=self.codes,
                             tradingAlgorithmGenerator=tradingAlgorithmGenerator)
            orders = cli.executeAnalysis()

            nextDay = alldays[alldays.index(day) + 1]
            for order in orders:
                open = cli.getLastValuesForInstrument(order.getInstrument(), nextDay)[1]

                if not cli.confirmOrder(orderId=order.getId(), quantity=order.getQuantity(), price=open, commission=10,
                                        date=nextDay):
                    cli.cancelOrder(order.getId())
            cli.save()

        maxlen = (datetime.datetime.now() - datetime.datetime(2014, 12, 1)).days
        cli = PytradeCli(dbfilepah=self.db, maxlen=maxlen)
        self.assertEqual(cli.getAccountInfo()[1], 36922.16)
Exemplo n.º 2
0
    def testCliDonchianAlgorithm2014(self):
        api = PytradeApi(dbfilepah=self.db)
        api.reinitializeUser(username=self.username, cash=self.initialCash)
        tradingAlgorithmGenerator = lambda feed, broker: DonchianTradingAlgorithm(feed, broker, 9, 26, 0.05)

        feed = DynamicFeed(self.db, self.codes, maxLen=self.maxLen)
        days = feed.getAllDays()
        for i in range(len(days)):
            day = days[i]
            cli = PytradeCli(dbfilepah=self.db, date=day, maxlen=self.maxLen, codes=self.codes, tradingAlgorithmGenerator=tradingAlgorithmGenerator)
            orders = cli.executeAnalysis()

            if i == (len(days) - 1):
                continue

            nextDay = days[i+1]
            for order in orders:
                open = cli.getLastValuesForInstrument(order.getInstrument(), nextDay)[1]

                if not cli.confirmOrder(orderId=order.getId(), quantity=order.getQuantity(), price=open, commission=10,
                                        date=nextDay):
                    cli.cancelOrder(order.getId())
            cli.save()

        maxlen=(datetime.datetime.now() - datetime.datetime(2014, 12, 1)).days
        cli = PytradeCli(dbfilepah=self.db, maxlen=maxlen)
        self.assertEqual(cli.getAccountInfo()[1], 36922.16)
Exemplo n.º 3
0
    def testApiDonchianAlgorithm2014WithSQLiteDataProvider(self):
        feed = DynamicFeed(self.db, self.codes, maxLen=self.maxLen)
        days = feed.getAllDays()

        api = PytradeApi(dbfilepah=self.db)
        api.reinitializeUser(username=self.username, cash=self.initialCash)
        tradingAlgorithmGenerator = lambda feed, broker: DonchianTradingAlgorithm(feed, broker, self.donchianEntry, self.donchianExit, self.riskFactor)

        for i in range(len(days)):
            day = days[i]
            api = PytradeApi(dbfilepah=self.db, username=self.username, tradingAlgorithmGenerator=tradingAlgorithmGenerator, codes=None, date=day, maxlen=self.maxLen, debugmode=False)
            api.executeAnalysis()
            api.persistData()

            if i == (len(days) - 1):
                continue

            day = days[i + 1]
            api = PytradeApi(dbfilepah=self.db, username=self.username, tradingAlgorithmGenerator=tradingAlgorithmGenerator, codes=None, date=day, maxlen=self.maxLen, debugmode=False)

            for order in api.getActiveMarketOrders() + api.getStopOrdersToConfirm():
                bar = api.getCurrentBarForInstrument(order.getInstrument())
                if bar is None:
                    continue

                if not api.confirmOrder(order, bar.getDateTime(), order.getQuantity(), bar.getOpen(), 10):
                    api.cancelOrder(order)

            api.persistData()

        self.assertEqual(api.getEquity(), 36922.16)
Exemplo n.º 4
0
    def testApiDonchianAlgorithmWithSpecificDatesAndSQLiteDataProvider(self):
        utc = pytz.utc
        specificdays = [
            utc.localize(datetime.datetime(2014, 2, 7)),
            utc.localize(datetime.datetime(2014, 2, 11)),
            utc.localize(datetime.datetime(2014, 9, 18)),
            utc.localize(datetime.datetime(2014, 10, 23)),
            utc.localize(datetime.datetime(2014, 10, 28)),
            utc.localize(datetime.datetime(2014, 12, 29))]

        feed = DynamicFeed(self.db, self.codes, maxLen=self.maxLen)
        alldays = feed.getAllDays()

        api = PytradeApi(dbfilepah=self.db)
        api.reinitializeUser(username=self.username, cash=self.initialCash)
        tradingAlgorithmGenerator = lambda feed, broker: DonchianTradingAlgorithm(feed, broker, self.donchianEntry,
                                                                                  self.donchianExit, self.riskFactor)
        for day in specificdays:
            api = PytradeApi(dbfilepah=self.db, username=self.username, tradingAlgorithmGenerator=tradingAlgorithmGenerator, codes=None, date=day,
                             maxlen=self.maxLen, debugmode=False)
            api.executeAnalysis()
            api.persistData()

            index = alldays.index(day)
            day = alldays[index+1]
            api = PytradeApi(dbfilepah=self.db, username=self.username, tradingAlgorithmGenerator=tradingAlgorithmGenerator, codes=None, date=day,
                             maxlen=self.maxLen, debugmode=False)

            for order in api.getActiveMarketOrders() + api.getStopOrdersToConfirm():
                bar = api.getCurrentBarForInstrument(order.getInstrument())
                if bar is None:
                    continue

                if not api.confirmOrder(order, bar.getDateTime(), order.getQuantity(), bar.getOpen(), 10):
                    api.cancelOrder(order)

            api.persistData()

        self.assertEqual(api.getEquity(), 36922.16)