Overall Statistics
Total Trades
271
Average Win
0.15%
Average Loss
-0.37%
Compounding Annual Return
0.035%
Drawdown
4.400%
Expectancy
0.011
Net Profit
0.071%
Sharpe Ratio
0.024
Probabilistic Sharpe Ratio
6.540%
Loss Rate
27%
Win Rate
73%
Profit-Loss Ratio
0.39
Alpha
-0.008
Beta
0.059
Annual Standard Deviation
0.024
Annual Variance
0.001
Information Ratio
-0.547
Tracking Error
0.277
Treynor Ratio
0.01
Total Fees
$167.00
from datetime import timedelta

class CoveredCallOptionsAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 7, 1)
        self.SetEndDate(2020, 6, 30)
        self.SetCash(1000000)
        equity = self.AddEquity("GOOG", Resolution.Minute)
        equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.SetSecurityInitializer(lambda x: x.SetMarketPrice(self.GetLastKnownPrice(x)))
        
        self.underlying = equity.Symbol
        # use the underlying equity as the benchmark
        self.SetBenchmark(self.underlying)
        #self.SetWarmUp(50)
        
        # Initialize the call contract
        self.call = str()
        
    def OnData(self,slice):
        if not self.Portfolio[self.underlying].Invested:
            self.MarketOrder(self.underlying, 100)  # long the underlying stock
        
        if self.call == str() or not self.Portfolio[self.call].Invested:
            self.call = self.AddContract() # Add the call option contract (subscribe the contract data)

        if self.Securities.ContainsKey(self.call) and not self.Portfolio[self.call].Invested:
            self.Sell(self.call, 1) # short the call option

    def AddContract(self):
        filtered_contracts = self.InitialFilter(-3, 3, 0, 7)
        if len(filtered_contracts) == 0: return str()
        else:
            call = [x for x in filtered_contracts if x.ID.OptionRight == OptionRight.Call] 
            # sorted the contracts according to their expiration dates and choose the ATM options
            contracts = sorted(sorted(call, key = lambda x: abs(self.Securities[self.underlying].Price- x.ID.StrikePrice)), 
                                            key = lambda x: x.ID.Date, reverse=True)
            if len(contracts) > 0:
                self.AddOptionContract(contracts[0], Resolution.Minute)
                return contracts[0]
            else:
                return str()
 
    def InitialFilter(self, min_strike_rank, max_strike_rank, min_expiry, max_expiry):
        
        ''' This method is an initial filter of option contracts 
            according to the range of strike price and the expiration date '''
            
        contracts = self.OptionChainProvider.GetOptionContractList(self.underlying, self.Time.date())
        if len(contracts) == 0 : return []
        # fitler the contracts based on the expiry range
        contract_list = [i for i in contracts if min_expiry < (i.ID.Date.date() - self.Time.date()).days <= max_expiry]
        # find the strike price of ATM option
        atm_strike = sorted(contract_list,
                            key = lambda x: abs(x.ID.StrikePrice - self.Securities[self.underlying].Price))[0].ID.StrikePrice
        strike_list = sorted(set([i.ID.StrikePrice for i in contract_list]))
        # find the index of ATM strike in the sorted strike list
        atm_strike_rank = strike_list.index(atm_strike)
        try: 
            strikes = strike_list[(atm_strike_rank + min_strike_rank):(atm_strike_rank + max_strike_rank)]
        except:
            strikes = strike_list
        filtered_contracts = [i for i in contract_list if i.ID.StrikePrice in strikes]

        return filtered_contracts 
    
    def OnOrderEvent(self, orderEvent):
        if orderEvent.Status == OrderStatus.Canceled:
            self.call = self.AddContract() # Add the call option contract (subscribe the contract data)