Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-6.787
Tracking Error
0.053
Treynor Ratio
0
Total Fees
$0.00
import pandas as pd

class ParticleCalibratedCoil(QCAlgorithm):

    def Initialize(self):
        
        '''
            Parameters for adjusting
        '''
        self.numberOfLiquidStocks = 5 # Controls the number of stocks in play
        
        '''
            Backtesting variables
        '''
        self.SetWarmUp(1)
        self.SetStartDate(2019, 11, 18)
        self.SetEndDate(2019, 12, 18)
        self.SetCash(1000000)
        
        '''
            Algorithm variables
        '''
        self.UniverseSettings.Resolution = Resolution.Minute
        self.AddUniverse(self.CoarseSelectionFilter)
        self.SetSecurityInitializer(lambda x: x.SetDataNormalizationMode(DataNormalizationMode.Raw))
        
        self.dictOfUnderlyingIndicators = {}
        
        
    def CoarseSelectionFilter(self, coarse):
        '''
            1. Sorts each element of the coarse object by dollar volume
            2. Returns a list of coarse object, limited to top 100 highest volume
            3. Returns a list of symbols we want to initialise into the algorithm
        '''
        
        self.sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
        self.topHundredMostLiquid = self.sortedByDollarVolume[:self.numberOfLiquidStocks]
            
        return [stock.Symbol for stock in self.topHundredMostLiquid]
        
    def OnSecuritiesChanged (self, changes):
        '''
            For any new securities added into the universe
            If the security is an underlying
            Subscribe to the option chains
            
            For any securities we want removed from the universe
            Remove the underlying and then remove the options
            
            For each new secury added into the universe
            If there is not yet one
            Create a standard deviation indicator
        '''
        for underlying in changes.AddedSecurities:
            if underlying.Symbol.SecurityType != SecurityType.Equity: continue
            option = self.AddOption(underlying.Symbol.Value, Resolution.Minute)
            option.SetFilter(-5, +2, timedelta(30), timedelta(60))
            
            if not underlying.Symbol in self.dictOfUnderlyingIndicators:
                self.dictOfUnderlyingIndicators[underlying.Symbol] = \
                {"Volatility": self.RSI(underlying.Symbol, 1, Resolution.Minute)}
    
        for underlying in changes.RemovedSecurities:
            self.RemoveSecurity(underlying.Symbol)
            for symbol in self.Securities.Keys:
                if symbol.SecurityType == SecurityType.Option and symbol.Underlying == underlying.Symbol:
                    self.RemoveSecurity(symbol)
        
        
    def OnData(self, slice):
        
        '''
            For each OptionChain, the key is the underlying symbol object, while the
            value is the option chain.
            For each chain in OptionChains, each chain represents the entire chain of option contracts
            for the underlying security.
        '''

        for chain in slice.OptionChains.Values:
            
            # Filter for the first ATM contract
            atmContract = sorted(chain, key = lambda x: abs(x.UnderlyingLastPrice - x.Strike))[0]
            
            impliedVolatility = atmContract.ImpliedVolatility
            underlyingSymbol = atmContract.UnderlyingSymbol
            
            if underlyingSymbol in self.dictOfUnderlyingIndicators and \
               "Volatility" in self.dictOfUnderlyingIndicators[underlyingSymbol]:
                self.dictOfUnderlyingIndicators[underlyingSymbol] = {'NormalizedIV': 
                                                                    impliedVolatility / \
                                                                    self.dictOfUnderlyingIndicators[underlyingSymbol]["Volatility"].Current.Value
                }
            
            self.Debug(f"Implied volatility of {underlyingSymbol} is {impliedVolatility}")
            self.Debug(f"The normalized implied volatility is {self.dictOfUnderlyingIndicators[underlyingSymbol]['NormalizedIV']}")