Overall Statistics
Total Trades
9
Average Win
4.79%
Average Loss
-10.07%
Compounding Annual Return
-9.532%
Drawdown
1.600%
Expectancy
-0.016
Net Profit
-1.629%
Sharpe Ratio
-4.06
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
0.48
Alpha
-0.097
Beta
-0.001
Annual Standard Deviation
0.024
Annual Variance
0.001
Information Ratio
-7.03
Tracking Error
0.145
Treynor Ratio
69.44
Total Fees
$3.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.
from datetime import timedelta

class ButterflySpreadAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 4, 1)
        self.SetEndDate(2017, 5, 30)
        self.SetCash(150000)
        equity = self.AddEquity("GOOG", Resolution.Minute)
        option = self.AddOption("GOOG", Resolution.Minute)
        self.symbol = option.Symbol
        # set our strike/expiry filter for this option chain
        option.SetFilter(-9, 9, timedelta(30), timedelta(60))
        # use the underlying equity GOOG as the benchmark
        self.SetBenchmark(equity.Symbol)
        
    def OnData(self,slice):

         # if there is no securities in portfolio, trade the options 
        if not self.Portfolio.Invested and self.Time.hour != 0 and self.Time.minute != 0: 
            for i in slice.OptionChains:
                if i.Key != self.symbol: continue
                chain = i.Value
                # sorted the optionchain by expiration date and choose the furthest date
                expiry = sorted(chain,key = lambda x: x.Expiry, reverse=True)[0].Expiry
                # filter the call options from the contracts expires on that date
                call = [i for i in chain if i.Expiry == expiry and i.Right == 0]
                # sorted the contracts according to their strike prices 
                call_contracts = sorted(call,key = lambda x: x.Strike)    
                if len(call_contracts) == 0: continue
                # choose OTM call 
                self.otm_call = call_contracts[-1]
                # choose ITM call 
                self.itm_call = call_contracts[0]
                # choose ATM call
                self.atm_call = sorted(call_contracts,key = lambda x: abs(chain.Underlying.Price - x.Strike))[0]
    
                self.Sell(self.atm_call.Symbol ,2)
                self.Buy(self.itm_call.Symbol ,1)
                self.Buy(self.otm_call.Symbol ,1)
                self.expirydate = self.otm_call.Expiry
        if self.Portfolio.Invested:
            self.CheckForExpiry()
            
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))
        
    def CheckForExpiry(self):
        if (self.expirydate - self.Time) < timedelta(7):
            self.Log("less than 7 days before expiry")
            self.Liquidate()