Overall Statistics
Total Trades
10
Average Win
0.24%
Average Loss
-0.01%
Compounding Annual Return
-4.078%
Drawdown
2.000%
Expectancy
24.087
Net Profit
-0.667%
Sharpe Ratio
-1.42
Probabilistic Sharpe Ratio
15.563%
Loss Rate
20%
Win Rate
80%
Profit-Loss Ratio
30.36
Alpha
-0.033
Beta
0.077
Annual Standard Deviation
0.027
Annual Variance
0.001
Information Ratio
0.125
Tracking Error
0.269
Treynor Ratio
-0.504
Total Fees
$7.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 CoveredCallAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2020, 3, 1)
        self.SetCash(100000)
        equity = self.AddEquity("IBM", Resolution.Minute)
        option = self.AddOption("IBM", Resolution.Minute)
        self.symbol = option.Symbol

        # set strike/expiry filter for this option chain
        option.SetFilter(-3, +3, timedelta(0), timedelta(30))
        # use the underlying equity as the benchmark
        self.SetBenchmark(equity.Symbol)
 
        
    def OnData(self,slice):
        if not self.Portfolio["IBM"].Invested:
            self.MarketOrder("IBM",100)     # buy 100 shares of underlying stocks
            self.Log(str(self.Time) + " bought IBM " + "@" + str(self.Securities["IBM"].Price) 
                    + " Cash balance: " + str(self.Portfolio.Cash)
                    + " Equity: " + str(self.Portfolio.HoldStock))
        
        option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
        if len(option_invested) < 1:
            self.TradeOptions(slice) 
 
    def TradeOptions(self,slice):
        for i in slice.OptionChains:
            if i.Key != self.symbol: continue
            chain = i.Value
            # filter the call options contracts
            call = [x for x in chain if x.Right == OptionRight.Call] 
            # sorted the contracts according to their expiration dates and choose the ATM options
            contracts = sorted(sorted(call, key = lambda x: abs(chain.Underlying.Price - x.Strike)), 
                                            key = lambda x: x.Expiry, reverse=True)
            if len(contracts) == 0: return    
            self.call = contracts[0].Symbol
            # short the call options
            self.MarketOrder(self.call, -1)     
    
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))
        self.Log("Cash balance: " + str(self.Portfolio.Cash))