Overall Statistics
# https://quantpedia.com/strategies/low-volatility-factor-effect-in-stocks-long-only-version/
#
# The investment universe consists of global large-cap stocks (or US large-cap stocks). At the end of each month, the investor constructs 
# equally weighted decile portfolios by ranking the stocks on the past three-year volatility of weekly returns. The investor goes long 
# stocks in the top decile (stocks with the lowest volatility).
#
# QC implementation changes:
#   - Instead of all listed stock, we select 500 most liquid US stocks.

import numpy as np

class LowVolatilityFactorEffectStocksLongOnlyVersion(QCAlgorithm):

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

        self.symbol = self.AddEquity('SPY', Resolution.Daily).Symbol
        
        self.period = 3 * 12 * 21
        
        self.coarse_count = 500
        self.last_coarse = []
        self.data = {}
        
        self.selection_flag = False
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction)
        self.Schedule.On(self.DateRules.MonthEnd(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol), self.Selection)

    def OnSecuritiesChanged(self, changes):
        for security in changes.AddedSecurities:
            security.SetFeeModel(CustomFeeModel(self))
            security.SetLeverage(10)
            
    def CoarseSelectionFunction(self, coarse):
        # Update the rolling window every day.
        for stock in coarse:
            symbol = stock.Symbol

            # Store monthly price.
            if symbol in self.data:
                self.data[symbol].update(stock.AdjustedPrice)

        if not self.selection_flag:
            return Universe.Unchanged

        # selected = [x.Symbol for x in coarse if x.HasFundamentalData and x.Market == 'usa']
        selected = [x.Symbol
            for x in sorted([x for x in coarse if x.HasFundamentalData and x.Market == 'usa'],
                key = lambda x: x.DollarVolume, reverse = True)[:self.coarse_count]]
        
        # Warmup price rolling windows.
        for symbol in selected:
            if symbol in self.data:
                continue
            
            self.data[symbol] = SymbolData(symbol, self.period)
            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)
        
        self.last_coarse = [x for x in selected if self.data[x].is_ready()]
        return self.last_coarse
    
    def OnData(self, data):
        if not self.selection_flag:
            return
        self.selection_flag = False
        
        weekly_vol = {x : self.data[x].volatility() for x in self.last_coarse}

        # Volatility sorting.
        sorted_by_vol = sorted(weekly_vol.items(), key = lambda x: x[1], reverse = True)
        decile = int(len(sorted_by_vol) / 10)
        long = [x[0] for x in sorted_by_vol[-decile:]]
        
        # Trade execution.
        stocks_invested = [x.Key for x in self.Portfolio if x.Value.Invested]
        for symbol in stocks_invested:
            if symbol not in long:
                self.Liquidate(symbol)

        for symbol in long:
            if self.Securities[symbol].Price != 0 and self.Securities[symbol].IsTradable:
                self.SetHoldings(symbol, 1 / len(long))

    def Selection(self):
        self.selection_flag = True

class SymbolData():
    def __init__(self, symbol, period):
        self.Symbol = symbol
        self.Price = RollingWindow[float](period)
    
    def update(self, value):
        self.Price.Add(value)
    
    def is_ready(self) -> bool:
        return self.Price.IsReady
        
    def volatility(self) -> float:
        closes = [x for x in self.Price]
        
        # Weekly volatility calc.
        separete_weeks = [closes[x:x+5] for x in range(0, len(closes), 5)]
        weekly_returns = [x[0] / x[-1] - 1 for x in separete_weeks]
        return np.std(weekly_returns)   

# Custom fee model.
class CustomFeeModel(FeeModel):
    def GetOrderFee(self, parameters):
        fee = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.00005
        return OrderFee(CashAmount(fee, "USD"))