Overall Statistics
Total Trades
12
Average Win
0.02%
Average Loss
-0.02%
Compounding Annual Return
-0.241%
Drawdown
0.000%
Expectancy
-0.173
Net Profit
-0.026%
Sharpe Ratio
-5.618
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
1.07
Alpha
-0.001
Beta
-0.001
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-6.314
Tracking Error
0.143
Treynor Ratio
2.159
Total Fees
$2.00
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class IronButterflyAlgorithm(QCAlgorithm):

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

    def OnData(self,slice):

        if self.Portfolio[self.underlyingsymbol].Quantity != 0:
            self.Liquidate()
      
        if not self.Portfolio.Invested and self.Time.hour != 0 and self.Time.minute != 0: 
            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)
            # sorted the optionchain by expiration date and choose the furthest date
            expiry = sorted(filtered_contracts, key = lambda x: x.ID.Date)[-1].ID.Date
            # filter the call and put options from the contracts
            call = [i for i in filtered_contracts if i.ID.OptionRight == OptionRight.Call and i.ID.Date == expiry]
            put = [i for i in filtered_contracts if i.ID.OptionRight == OptionRight.Put and i.ID.Date == expiry]
            # sorted the contracts according to their strike prices 
            call_contracts = sorted(call,key = lambda x: x.ID.StrikePrice)    
            put_contracts = sorted(put,key = lambda x: x.ID.StrikePrice)    
            underlyingPrice = self.Securities["GOOG"].Price
            atm_put = sorted(put_contracts,key = lambda x: abs(underlyingPrice - x.ID.StrikePrice))[0]
            atm_call = sorted(call_contracts,key = lambda x: abs(underlyingPrice - x.ID.StrikePrice))[0]
            otm_call = call_contracts[-1]
            otm_put = put_contracts[0]
            
            self.trade_contracts = [atm_put, atm_call, otm_call, otm_put]
            for contract in self.trade_contracts:
                self.AddOptionContract(contract, Resolution.Minute) 
              
            self.Sell(atm_put ,1)
            self.Sell(atm_call ,1) 
            self.Buy(otm_call ,1)
            self.Buy(otm_put ,1)
        
            
    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
    
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))