Ejemplo n.º 1
0
    def getStrategyTrades(self, date=None):
        if date is None:
            date = self.end

        print "Get Strategy Trades for date {0}".format(date.date())
        x = self.getNextTradingDay(date)

        filtered  = [rate for rate in self.rateData if rate.date.date() == date.date()]
        dailyReturns = []

        for item in filtered:
            if hasattr(item, 'dailyReturn'):
                dailyReturns.append(item)

        dailyReturns.sort(key=lambda r: r.dailyReturn, reverse=False)

        # Get 5 worst returns
        worstReturns = dailyReturns[:5]
        for item in worstReturns:
            trade = Trade("long", item.symbol, x)
            database.saveTrade(trade)
Ejemplo n.º 2
0
    def testEquityLine(self, initialInvestment):
        print "testing strategy with investment {0} and percent invested {1}".format(investment, percentInv)

        totalReturns = []

        x = self.start
        while x.date() < self.end.date():
            dailyRates = [rate for rate in self.rateData if rate.date() == x.date()]
            if len(dailyRates) == 0:
                x = x + timedelta(days=1)
                continue
            else:
                dailyReturns = []
                for item in dailyRates:
                    if hasattr(item, 'dailyReturn'):
                        dailyReturns.append(item)

                if len(dailyReturns) > 0:
                    dailyReturns.sort(key=lambda r: r.dailyReturn, reverse=False)
                    worstReturns = dailyReturns[:5]
                    # Worst Returns contains the rates that we are going to buy. We need a brute force algorithm to distribute

        filtered = [rate for rate in self.rateData if rate.date.date() != self.start.date()]
        dailyReturns = []

        for item in filtered:
            if hasattr(item, 'dailyReturn'):
                dailyReturns.append(item)

        dailyReturns.sort(key=lambda r: r.dailyReturn, reverse=False)

        # Get 5 worst returns
        worstReturns = dailyReturns[:5]
        for item in worstReturns:
            trade = Trade("long", item.symbol, x)
            database.saveTrade(trade)

        dailyReturns.sort(key=lambda r: r.dailyReturn, reverse=False)

        for trade in trades:
            openDate = trade["open_date"]
            symbol = trade["stock_symbol"]
            prevDate = self.getPreviousTradingDay(openDate)
            x = None
            y = None
            for rate in self.rateData:
                if rate['date'].date() == openDate.date() and rate['symbol'] == symbol:
                    x = rate
                if rate['date'].date() == prevDate.date() and rate['symbol'] == symbol:
                    y = rate
                if x is not None and y is not None:
                    break

            if x is None:
                print "NOT FOUND"
                continue
            if y is None:
                print "NOT FOUND"
                continue

            dailyReturn = (x['adjClose'] - y['adjClose']) / y['adjClose']
            returns.append(dailyReturn)
        print "Finished with returns"
        return returns