Overall Statistics
Total Trades
1741
Average Win
0.60%
Average Loss
-0.64%
Compounding Annual Return
6.927%
Drawdown
59.800%
Expectancy
0.140
Net Profit
103.269%
Sharpe Ratio
0.364
Loss Rate
41%
Win Rate
59%
Profit-Loss Ratio
0.94
Alpha
0.122
Beta
-0.607
Annual Standard Deviation
0.301
Annual Variance
0.091
Information Ratio
0.298
Tracking Error
0.301
Treynor Ratio
-0.18
Total Fees
$3372.30
 
 
class JanuaryEffectInStocksAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2008, 1, 1)  
        self.SetEndDate(2018, 8, 1)  
        self.SetCash(100000) 
        self.AddEquity("SPY", Resolution.Daily)
        self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY"), self.Rebalance)
        self.monthly_rebalance = False 
        self.coarse = False
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        
    def CoarseSelectionFunction(self, coarse):
        if self.monthly_rebalance:
            self.coarse = True
            coarse = [x for x in coarse if (x.AdjustedPrice > 10)]
            topDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)[:1000]
            return [i.Symbol for i in topDollarVolume]
        else:
            return []
    
    def FineSelectionFunction(self, fine):
        if self.monthly_rebalance:
            fine =[i for i in fine if i.EarningReports.BasicAverageShares.ThreeMonths!=0
                                  and i.EarningReports.BasicEPS.TwelveMonths!=0
                                  and i.ValuationRatios.PERatio!=0]
            for i in fine:
                i.MarketCap = float(i.EarningReports.BasicAverageShares.ThreeMonths * (i.EarningReports.BasicEPS.TwelveMonths*i.ValuationRatios.PERatio))
            sorted_market_cap = sorted(fine, key = lambda x:x.MarketCap, reverse=True)
            symbols = [i.Symbol for i in sorted_market_cap]
            self.top_market_cap = symbols[:10]
            self.bottom_market_cap = symbols[-10:]
            return self.top_market_cap + self.bottom_market_cap
        else:
            return []
    
    def Rebalance(self):
        self.monthly_rebalance = True
            
    
    def OnData(self, data):
        if not (self.monthly_rebalance and self.coarse): return
        self.coarse = False
        self.monthly_rebalance = False
        stocks_invested = [x.Key for x in self.Portfolio if x.Value.Invested]
        # invest in small cap stocks at the beginning of each January
        if self.Time.month == 1:
          # liquidate stocks not in the small-cap group
          for i in stocks_invested:
              if i not in self.bottom_market_cap:
                  self.Liquidate(i)
          weight = 1/len(self.bottom_market_cap)
          for i in self.bottom_market_cap:
              self.SetHoldings(i, weight)
        # invest in large cap stocks for rest of the year
        else:
          # liquidate stocks not in the large-cap group
          for i in stocks_invested:
              if i not in self.top_market_cap:
                  self.Liquidate(i)
        
          weight = 1/len(self.top_market_cap)
          for i in self.top_market_cap:
              self.SetHoldings(i, weight)