Overall Statistics
Total Trades
6380
Average Win
0.88%
Average Loss
-0.74%
Compounding Annual Return
95.446%
Drawdown
32.200%
Expectancy
0.158
Net Profit
3474.176%
Sharpe Ratio
2.047
Probabilistic Sharpe Ratio
88.628%
Loss Rate
47%
Win Rate
53%
Profit-Loss Ratio
1.19
Alpha
0.922
Beta
-0.09
Annual Standard Deviation
0.447
Annual Variance
0.199
Information Ratio
1.727
Tracking Error
0.482
Treynor Ratio
-10.146
Total Fees
$119928.83
Estimated Strategy Capacity
$40000.00
from QuantConnect.Indicators import *
class ShortTermReversalVictor(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2015, 1, 1)
        self.SetEndDate(2020, 5, 1)  
        self.SetCash(100000)

        self.symbol = self.AddEquity('SPY', Resolution.Daily).Symbol
        
        self.coarse_count = 500
        self.stock_selection = 10 # 5
        self.top_by_market_cap_count = 100
        
        self.period = 4
        self.SetWarmUp(timedelta(self.period))
        
        self.long = []
        self.short = []
        
        # symbolData at each moment
        self.symbolDataDict = {}
        self.symbolToMarketCap = {}
        self.SetBenchmark("SPY")
        
        self.day = 1
        self.selection_flag = False
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY"), self.Selection)

    def OnSecuritiesChanged(self, changes):
        #self.Debug("change")
        for removed in changes.RemovedSecurities:
            self.symbolDataDict.pop(removed.Symbol, None)
        for security in changes.AddedSecurities:
            security.SetLeverage(3)
            symbol = security.Symbol
            daily_history = self.History(symbol, self.period+1, Resolution.Daily)
            if daily_history.empty:
                self.Log(f"Not enough data for {symbol} yet")
                continue
            if symbol not in self.symbolDataDict.keys():
                symbolData = SymbolData(symbol, self.period, self)
                self.symbolDataDict[symbol] = symbolData
                symbolData.warmup(daily_history)
       
    def CoarseSelectionFunction(self, coarse):
        #self.Debug("coarse")
        if not self.selection_flag: # self.selection_flag is only true when day is 5 or it is a Friday.
            return Universe.Unchanged

        selected = sorted([x for x in coarse if x.HasFundamentalData and x.Market == 'usa' and x.Price > 1],
            key=lambda x: x.DollarVolume, reverse=True)
        selected = [x.Symbol for x in selected][:self.coarse_count]

        return selected
        
    def FineSelectionFunction(self, fine): # the long and short lists are updated daily
        #self.Debug("Fine")
        if not self.selection_flag: # self.selection_flag is only true when day is 5 or it is a Friday.
            return Universe.Unchanged
        fine = [x for x in fine if x.MarketCap != 0]
        
        sorted_by_market_cap = sorted(fine, key = lambda x:x.MarketCap, reverse = True)
        top_by_market_cap = {
            "top":[x.Symbol for x in sorted_by_market_cap[:self.top_by_market_cap_count//2]],
            "bottom":[x.Symbol for x in sorted_by_market_cap[-self.top_by_market_cap_count//2:]]
        }
        
        # get the top market cap and bottom market cap (50-50)
        filtered = top_by_market_cap["top"]+top_by_market_cap["bottom"]
        
        filteredFine = sorted_by_market_cap[:self.top_by_market_cap_count//2]+sorted_by_market_cap[-self.top_by_market_cap_count//2:]
        for f in filteredFine:
            self.symbolToMarketCap[f.Symbol] = f.MarketCap
            
        return filtered
    
    def OnData(self, data):
        #self.Debug("data")
        # onData gets called after onSecuritiesChanged here because we filtered the entire data first
        for symbolData in self.symbolDataDict.values():
            symbol = symbolData.symbol
            symbolData.marketCap = self.symbolToMarketCap.get(symbol,-1)
            # you need to update this in case some of the stocks persisted into next selection period, which won't be warmed up in onSecuritiesChanged
            security = self.Securities[symbol]
            self.symbolDataDict[symbol].update_closes(security.Close)
            self.symbolDataDict[symbol].update_highs(security.High)
            self.symbolDataDict[symbol].update_lows(security.Low)
        if not self.selection_flag:
            return
        self.selection_flag = False
    
        symbolDataList = list(self.symbolDataDict.values())
        # sort by market cap
        sorted_by_market_cap = [s.symbol for s in sorted(symbolDataList, key=lambda s:s.marketCap, reverse=True)]
        top = sorted_by_market_cap[:self.top_by_market_cap_count//2]
        bottom = sorted_by_market_cap[-self.top_by_market_cap_count//2:]
        # sort performance (from worst) for top market cap and performance (from best) for bottom market cap
        top_performances = {symbol : self.symbolDataDict[symbol].period_return() for symbol in top if self.symbolDataDict[symbol].is_ready()}    
        bottom_performances = {symbol : self.symbolDataDict[symbol].period_return() for symbol in bottom if self.symbolDataDict[symbol].is_ready()}
        sorted_top_performances = [x[0] for x in sorted(top_performances.items(), key=lambda item: item[1])]
        sorted_bottom_performances = [x[0] for x in sorted(bottom_performances.items(), key=lambda item: item[1], reverse=True)]
        
        # select securities to short and long (50-50)
        self.long = sorted_top_performances[:self.stock_selection//2]
        self.short = sorted_bottom_performances[:self.stock_selection//2]
            
        invested = [x.Key for x in self.Portfolio if x.Value.Invested]
        for symbol in invested: # if they are not to be selected again, then they are liquidated
            if symbol not in self.long + self.short:
                self.Liquidate(symbol)
        
        for symbol in self.long:
            if self.Securities[symbol].Price != 0 and self.Securities[symbol].IsTradable:
                self.SetHoldings(symbol, 0.9 / len(self.long)) # split portfolio evenly
            
        for symbol in self.short:
            if self.Securities[symbol].Price != 0 and self.Securities[symbol].IsTradable:
                self.SetHoldings(symbol, -0.9 / len(self.short)) # split portfolio evenly
                
        self.long.clear()
        self.short.clear()

    def Selection(self):
        if self.day == self.period:
            self.selection_flag = True
        
        self.day += 1
        if self.day > self.period:
            self.day = 1
    

class SymbolData():
    def __init__(self, symbol, period, algo):
        self.symbol = symbol
        self.marketCap = None
        self.algo = algo
        self.closes = RollingWindow[float](period+1)
        self.highs = RollingWindow[float](period+1)
        self.lows = RollingWindow[float](period+1)
        self.period = period
        
    def update_closes(self, close):
        self.closes.Add(close)
    
    def update_highs(self, high):
        self.highs.Add(high)
    
    def update_lows(self, low):
        self.lows.Add(low)
        
    def period_return(self):
        if self.closes[self.period] == 0:
            self.algo.Debug(self.symbol)
            return None
        return self.closes[0] / self.closes[self.period] - 1
        
    def is_ready(self) -> bool:
        return self.closes.IsReady and self.highs.IsReady and self.lows.IsReady and self.closes[self.period] != 0
        
    def warmup(self, daily_history):
        if daily_history.empty:
            return
        closes = daily_history.loc[self.symbol].close
        for time, c in closes.iteritems():
            self.update_closes(c)
        highs = daily_history.loc[self.symbol].high
        for time, h in highs.iteritems():
            self.update_highs(h)
        lows = daily_history.loc[self.symbol].low
        for time, l in lows.iteritems():
            self.update_lows(l)
class ShortTermReversalExperimental(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2000, 1, 1)  
        self.SetEndDate(2021, 1, 1)  
        self.SetCash(100000)

        self.symbol = self.AddEquity('SPY', Resolution.Daily).Symbol
        
        self.coarse_count = 500
        self.stock_selection = 5
        self.top_by_market_cap_count = 100
        
        self.period = 21
        
        self.long = []
        self.short = []
        
        # Daily close data
        self.data = {}
        
        self.day = 1
        self.selection_flag = False
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        self.Schedule.On(self.DateRules.EveryDay(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol), self.Selection)

    def OnSecuritiesChanged(self, changes):
        for security in changes.AddedSecurities:
            security.SetLeverage(5)
        
    def CoarseSelectionFunction(self, coarse):
        # Update the rolling window every day, with latest prices.
        for stock in coarse:
            symbol = stock.Symbol
            if symbol not in self.Portfolio:
                self.AddEquity(symbol, Resolution.Daily)

            # Store monthly price.
            if symbol in self.data:
                self.data[symbol].update(stock.AdjustedPrice)
            
        if not self.selection_flag: # self.selection_flag is only true when day is 5 or it is a Friday.
            return Universe.Unchanged

        selected = sorted([x for x in coarse if x.HasFundamentalData and x.Market == 'usa' and x.Price > 1],
            key=lambda x: x.DollarVolume, reverse=True)
        selected = [x.Symbol for x in selected][:self.coarse_count]

        # Warmup price rolling windows.
        for symbol in selected:
            if symbol in self.data: # if data is already stored, skip
                continue

            self.data[symbol] = SymbolData(self.period) # Creates a new SymbolData object (defined below) with a period and RollingWindow of closing prices
            history = self.History(symbol, self.period, Resolution.Daily)
            if history.empty:
                self.Log(f"Not enough data for {symbol} yet")
                continue
            closes = history.loc[symbol].close
            for time, close in closes.iteritems():
                self.data[symbol].update(close)
    
        return [x for x in selected if self.data[x].is_ready()]
        
    def FineSelectionFunction(self, fine): # the long and short lists are updated daily
        fine = [x for x in fine if x.MarketCap != 0]
        
        sorted_by_market_cap = sorted(fine, key = lambda x:x.MarketCap, reverse = True)
        top_by_market_cap = [x.Symbol for x in sorted_by_market_cap[:self.top_by_market_cap_count]] # top_by_market_cap_count is 100
        
        month_performances = {symbol : self.data[symbol].monthly_return() for symbol in top_by_market_cap}
        week_performances = {symbol : self.data[symbol].weekly_return() for symbol in top_by_market_cap}
            
        sorted_by_month_perf = [x[0] for x in sorted(month_performances.items(), key=lambda item: item[1], reverse=True)]
        sorted_by_week_perf = [x[0] for x in sorted(week_performances.items(), key=lambda item: item[1])]
        
        sorted_by_week_perf = [symbol for symbol in sorted_by_week_perf if self.data[symbol].macd_diff < .01]
        # self.long = sorted_by_week_perf[:self.stock_selection] # only self.stock_selection # of these
        
        # MACD assessment component
        macd_perf = []
        for symbol in sorted_by_week_perf:
            macd = self.MACD(symbol, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily)
            if (macd.Current.Value - macd.Signal.Current.Value) / macd.Fast.Current.Value > .01:
                macd_perf.append(symbol)
        self.long = macd_perf[:self.stock_selection]
        
        for symbol in sorted_by_month_perf:
            if symbol not in self.long:
                self.short.append(symbol)
            
            if len(self.short) == self.stock_selection: # only need self.stock_selection # in short list
                break
        
        return self.long + self.short
    
    def OnData(self, data): # equities are kept for at least a week, at the end of week update portfolio
        if not self.selection_flag:
            return
        self.selection_flag = False
        
        invested = [x.Key for x in self.Portfolio if x.Value.Invested]
        for symbol in invested: # if they are not to be selected again, then they are liquidated
            if symbol not in self.long + self.short:
                self.Liquidate(symbol)
        
        for symbol in self.long:
            if self.Securities[symbol].Price != 0 and self.Securities[symbol].IsTradable:
                self.SetHoldings(symbol, 1 / len(self.long)) # split portfolio evenly

        for symbol in self.short:
            if self.Securities[symbol].Price != 0 and self.Securities[symbol].IsTradable:
                self.SetHoldings(symbol, -1 / len(self.short)) # split portfolio evenly
                
        self.long.clear()
        self.short.clear()

    def Selection(self):
        if self.day == 5:
            self.selection_flag = True
        
        self.day += 1
        if self.day > 5:
            self.day = 1