diff --git a/03 Writing Algorithms/03 Securities/99 Asset Classes/09 Index Options/01 Requesting Data/01 Universes/12 Examples.php b/03 Writing Algorithms/03 Securities/99 Asset Classes/09 Index Options/01 Requesting Data/01 Universes/12 Examples.php index 7feffe4f60..c5e675fab4 100644 --- a/03 Writing Algorithms/03 Securities/99 Asset Classes/09 Index Options/01 Requesting Data/01 Universes/12 Examples.php +++ b/03 Writing Algorithms/03 Securities/99 Asset Classes/09 Index Options/01 Requesting Data/01 Universes/12 Examples.php @@ -5,134 +5,62 @@
0DTE Options are option contracts that expire on the same day they are traded. To create a universe with 0DTE options, call the SetFilter
or set_filter
method. In this example, we will select 0DTE contracts within 1 strike price level and a Delta between 0.25 and 0.75. Then, we will go long on the highest strike price call contract of the SPX index and hold until expiry.
private Option _option; - -public override void Initialize() +public class BasicIndexOptionAlgorithm : QCAlgorithm { - SetStartDate(2023, 1, 1); - SetEndDate(2024, 1, 1); - SetCash(100000); - - // Subscribe to the option chain. - _option = AddIndexOption("SPX", Resolution.Daily); - - // Filter the option universe to only select 0DTE options. - _option.SetFilter(u => u.IncludeWeeklys().Expiration(0, 0).Strikes(-1, 1)); - - // Filter the option universe by Delta. - _option.SetFilter(optionFilterUniverse => optionFilterUniverse.Delta(0.25m, 0.75m)); -} + private Option _option; -public override void OnData(Slice slice) -{ - if (Portfolio.Invested) + public override void Initialize() { - return; + SetStartDate(2023, 1, 1); + SetEndDate(2024, 1, 1); + SetCash(100000); + // Subscribe to the option chain. + _option = AddIndexOption("SPX", "SPXW"); + // Filter the option universe to only select 0DTE options. + _option.SetFilter(u => u.IncludeWeeklys().Expiration(0, 0).Strikes(-1, 1)); + // Filter the option universe by Delta. The last SetFilter call prevails. + // _option.SetFilter(optionFilterUniverse => optionFilterUniverse.Delta(0.25m, 0.75m)); } - // Get the option chain data. - if (!slice.OptionChains.TryGetValue(_option.Symbol, out var chain)) + public override void OnData(Slice slice) { - return; - } - - // Select the call Option contract. - var calls = chain.Where(contract => contract.Right == OptionRight.Call).ToList(); - if (calls.Count < 1) return; - - // Sorted the contracts according to their strike prices. - calls = calls.OrderBy(x => x.Strike).ToList(); + // Get the option chain data. + if (Portfolio.Invested || !slice.OptionChains.TryGetValue(_option.Symbol, out var chain)) + { + return; + } + // Sorted the call Option contracts according to their strike prices. + var call = chain.Where(contract => contract.Right == OptionRight.Call).OrderBy(x => x.Strike).FirstOrDefault(); + if (call == null) return; - // Buy 1 0DTE call option contract for the SPX index. - Buy(calls[0].Symbol, 1); + // Buy 1 0DTE call option contract for the SPX index. + Buy(call.Symbol, 1); + } }-def initialize(self) -> None: +class BasicIndexOptionAlgorithm(QCAlgorithm): + +def initialize(self) -> None: self.set_start_date(2023,1,1) self.set_end_date(2024,1,1) self.set_cash(100_000) # Subscribe to the option chain. - self._option = self.add_index_option("SPX", Resolution.DAILY) + self._option = self.add_index_option("SPX", "SPXW") # Filter the option universe to only select 0DTE options. self._option.set_filter(lambda u: u.include_weeklys().expiration(0, 0).strikes(-1, 1)) - # Filter the option universe by Delta. - self._option.set_filter(lambda option_filter_universe: option_filter_universe.delta(0.25, 0.75)) + # Filter the option universe by Delta. The last set_filter call prevails. + # self._option.set_filter(lambda option_filter_universe: option_filter_universe.delta(0.25, 0.75)) -def on_data(self, slice: Slice) -> None: +def on_data(self, slice: Slice) -> None: if self.portfolio.invested: return # Get the option chain data. chain = slice.option_chains.get(self._option.symbol) if not chain: return - # Select the call Option contract. - calls = [contract for contract in chain if contract.right == OptionRight.CALL] - if len(calls) < 1: return - # Sorted the contracts according to their strike prices. - calls = sorted(calls, key=lambda x: x.Strike) + # Sorted the call Option contracts according to their strike prices. + calls = sorted([contract for contract in chain if contract.right == OptionRight.CALL], key=lambda x: x.strike) + if not calls: + return # Buy 1 0DTE call option contract for the SPX index. - self.Buy(calls[0].Symbol, 1)-
In this example, we will create a universe of option contracts for the SPX index that expire in 30 to 90 days. From this universe, we allocate 10% of our cash to buy an ATM Call option that expires in 90 days. We hold this until expiration and then roll over to the next 90-day ATM contract using the on security changed event handler. Since index options are European (they will not be exercised before expiration) and cash-settled (even if held until exercised, they only affect the cash book), this approach saves us the extra work of handling option exercise or assignment, as we would with equity options.
- -private Option _indexOption; - -public override void Initialize() -{ - // Subscribe to the index option and filter to get only the ones expiring in 30-90 days. - _indexOption = AddIndexOption("SPX", "SPXW"); - _indexOption.SetFilter((u) => u.IncludeWeeklys().CallsOnly().Expiration(30, 90)); -} - -public override void OnData(Slice slice) -{ - // Get option chain data for the canonical symbol. - if (!Portfolio.Invested && - slice.OptionChains.TryGetValue(_indexOption.Symbol, out var chain)) - { - // Obtain the ATM call that expires furthest (90 days). - var expiry = chain.Max(x => x.Expiry); - var atmCall = chain.Where(x => x.Expiry == expiry) - .OrderBy(x => Math.Abs(x.Strike - x.UnderlyingLastPrice)) - .First(); - // Allocate 10% Capital. - SetHoldings(atmCall.Symbol, 0.1m); - } -} - -public override void OnSecuritiesChanged(SecurityChanges changes) -{ - foreach (var removed in changes.RemovedSecurities) - { - // Liquidate the contracts that exit the universe (due to expiry). - if (Portfolio[removed.Symbol].Invested) - { - Liquidate(removed.Symbol); - } - } -}-
def initialize(self): - # Subscribe to the index option and filter to get only the ones expiring in 30-90 days. - self._index_option = self.add_index_option("SPX", "SPXW") - self._index_option.set_filter(lambda u: u.include_weeklys().calls_only().expiration(30, 90)) - -def on_data(self, slice: Slice): - # Get option chain data for the canonical symbol. - if not self.portfolio.invested: - chain = slice.option_chains.get(self._index_option.symbol) - if chain: - # Obtain the ATM call that expires furthest (90 days). - expiry = max(x.expiry for x in chain) - atm_call = min(chain, key=lambda x: abs(x.strike - x.underlying_last_price)) - # Allocate 10% Capital. - self.set_holdings(atm_call.symbol, 0.1) - -def on_securities_changed(self, changes: SecurityChanges): - for removed in changes.removed_securities: - if self.portfolio[removed.symbol].invested: - # Liquidate the contracts that exit the universe (due to expiry). - self.liquidate(removed.symbol)-