Overall Statistics Total Trades2Average Win0%Average Loss0%Compounding Annual Return0.747%Drawdown0.000%Expectancy0Net Profit0.014%Sharpe Ratio6.242Probabilistic Sharpe Ratio93.349%Loss Rate0%Win Rate0%Profit-Loss Ratio0Alpha0.006Beta-0.003Annual Standard Deviation0.001Annual Variance0Information Ratio0.159Tracking Error0.323Treynor Ratio-2.204Total Fees$2.00Estimated Strategy Capacity$52000000.00Lowest Capacity AssetAAPL XYME7HUNIMKM|AAPL R735QTJ8XC9X
from AlgorithmImports import *

def Initialize(self):
self.SetStartDate(2022, 4, 27)
self.SetEndDate(2022, 5, 3)
self.SetCash(500000)

self.symbol = option.Symbol
option.SetFilter(self.UniverseFunc)

def UniverseFunc(self, universe):
return universe.IncludeWeeklys().Strikes(-1, 1).Expiration(timedelta(4),timedelta(50))

def OnData(self, data):
# avoid extra orders
if self.Portfolio.Invested: return

# Get the OptionChain of the self.symbol
chain = data.OptionChains.get(self.symbol, None)
if not chain: return

# get at-the-money strike
atm_strike = sorted(chain, key=lambda x: abs(x.Strike - chain.Underlying.Price))[0].Strike

# filter the call options from the contracts which is ATM in the option chain.
calls = [i for i in chain if i.Strike == atm_strike and i.Right == OptionRight.Call]
if len(calls) == 0: return

# sorted the optionchain by expiration date
expiries = sorted([x.Expiry for x in calls], key = lambda x: x)

# select the farest expiry as far-leg expiry, and the nearest expiry as near-leg expiry
near_expiry = expiries[0]
far_expiry = expiries[-1]

option_strategy = OptionStrategies.CallCalendarSpread(self.symbol, atm_strike, near_expiry, far_expiry)
# We open a position with 1 unit of the option strategy
# self.Sell(option_strategy, 1) if short call calendar spread