Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
0.878%
Drawdown
0.000%
Expectancy
0
Net Profit
0.143%
Sharpe Ratio
6.749
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0.009
Annual Standard Deviation
0.001
Annual Variance
0
Information Ratio
-6.449
Tracking Error
0.141
Treynor Ratio
0.97
Total Fees
$1.00
class ProtectiveCollarAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 4, 1)
        self.SetEndDate(2017, 5, 30)
        self.SetCash(10000000)
        equity = self.AddEquity("GOOG", Resolution.Minute)
        self.underlyingsymbol = equity.Symbol
        self.SetBenchmark(equity.Symbol)

    def OnData(self,slice):

        if not self.Portfolio.Invested and self.Time.hour != 0 and self.Time.minute != 0: 
            self.TradeOptions(slice)
            
        # if the options expire, print out the price and the position information                
        if slice.Delistings.Count > 0:
            if [x.Key in [self.otm_call, self.otm_put] for x in slice.Delistings]:
                self.Log('GOOG position: '+ str(self.Portfolio['GOOG'].Quantity))
                for contract in [self.otm_call, self.otm_put]:
                    self.Log('{0} quantity: {1}'.format(str(contract.Value),self.Portfolio[contract].Quantity))
                self.Log('GOOG Price S(T): '+ str(self.Securities['GOOG'].Price))
                        
    def TradeOptions(self,slice):
        
        contracts = self.OptionChainProvider.GetOptionContractList(self.underlyingsymbol, self.Time.date())
        if len(contracts) == 0 : return
        filtered_contracts = self.InitialFilter(self.underlyingsymbol, contracts, -10, 10, 0, 30)
        # choose the furthest expiration date within 30 days from now on
        expiry = sorted(filtered_contracts, key = lambda x: x.ID.Date)[-1].ID.Date
        # filter the call options contracts
        call = [x for x in filtered_contracts if x.ID.OptionRight == 0 and x.ID.Date == expiry]
        # filter the put options contracts
        put = [x for x in filtered_contracts if x.ID.OptionRight == 1 and x.ID.Date == expiry]
        # sorted the call options by strike price and choose the deep OTM one in the list
        self.otm_call = sorted(call, key = lambda x: x.ID.StrikePrice)[-1]
        self.otm_put = sorted(put, key = lambda x: x.ID.StrikePrice)[0]
        if (self.otm_call is None) or (self.otm_put is None): return
        
        self.AddOptionContract(self.otm_call, Resolution.Minute)
        self.AddOptionContract(self.otm_put, Resolution.Minute)
        
        self.Sell(self.otm_call, 1) # sell the OTM call 
        self.Buy(self.otm_put, 1) # buy the OTM put
        self.Buy("GOOG",100)     # buy 100 shares of the underlying stock

        
        self.Log('share price at time zero S(0)'+str(self.Securities['GOOG'].Price))
            
    def InitialFilter(self, underlyingsymbol, symbol_list, min_strike_rank, max_strike_rank, min_expiry, max_expiry):
        
        ''' This method is an initial filter of option contracts
            based on the range of strike price and the expiration date '''
            
        if len(symbol_list) == 0 : return
        # fitler the contracts based on the expiry range
        contract_list = [i for i in symbol_list 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[underlyingsymbol].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: 
            min_strike = strike_list[atm_strike_rank + min_strike_rank]
            max_strike = strike_list[atm_strike_rank + max_strike_rank]

        except:
            min_strike = strike_list[0]
            max_strike = strike_list[-1]
           
        filtered_contracts = [i for i in contract_list if i.ID.StrikePrice >= min_strike and i.ID.StrikePrice <= max_strike]

        return filtered_contracts