| Overall Statistics |
|
Total Trades 53 Average Win 1.07% Average Loss -2.66% Compounding Annual Return 14.424% Drawdown 4.000% Expectancy 0.140 Net Profit 14.410% Sharpe Ratio 2.019 Probabilistic Sharpe Ratio 79.713% Loss Rate 19% Win Rate 81% Profit-Loss Ratio 0.40 Alpha -0.013 Beta 0.501 Annual Standard Deviation 0.069 Annual Variance 0.005 Information Ratio -2.392 Tracking Error 0.069 Treynor Ratio 0.279 Total Fees $40.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(2019, 1, 1)
self.SetEndDate(2019, 12, 31)
self.SetCash(25000)
equity = self.AddEquity("SPY", Resolution.Minute)
option = self.AddOption("SPY", Resolution.Minute)
self.symbol = option.Symbol
# set strike/expiry filter for this option chain
option.SetFilter(0, +3, timedelta(0), timedelta(30))
# use the underlying equity as the benchmark
self.SetBenchmark(equity.Symbol)
# self.Schedule.On(DateRules.EveryDay("SPY"), TimeRules.AfterMarketOpen("SPY", 10) , self.EveryDayAfterMarketOpens)
def OnData(self,slice):
if not self.Portfolio["SPY"].Invested:
# self.Log("Cash Balance before SPY purchase :" + str(self.Portfolio.Cash))
self.MarketOrder("SPY",100) # buy 100 shares of underlying stocks
# self.Log("Cash Balance after SPY :" + str(self.Portfolio.Cash))
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 EveryDayAfterMarketOpens(self,slice):
# if not self.Portfolio["SPY"].Invested:
# self.MarketOrder("SPY",100) # buy 100 shares of underlying stocks
# 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
# self.Log(str(i.Value.))
chain = i.Value
# filter the call options contracts
call = [x for x in chain if x.Right == OptionRight.Call]
# self.Log(str(len(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.Log(contracts[0].Symbol)
self.call = contracts[0].Symbol
# short the call options
self.DefaultOrderProperties.TimeInForce = TimeInForce.Day
orderTicket = self.MarketOrder(self.call, -1, False)
# self.Log("Cash Balance after Selling Call option for SPY :" + str(self.Portfolio.Cash))
# Buy Back the call option if falls in value
self.DefaultOrderProperties.TimeInForce = TimeInForce.GoodTilCanceled
self.LimitOrder(self.call, 1, orderTicket.AverageFillPrice * .1)
def OnOrderEvent(self, orderEvent):
self.Log(str(orderEvent))
self.Log("Cash Balance :" + str(self.Portfolio.Cash))