Add option chain selection helpers - #9667
Draft
jhonabreul wants to merge 2 commits into
Draft
Conversation
Adds composable contract selection helpers to OptionChain so the usual hand-rolled nearest-expiry/nearest-strike/ATM scans become a single call: Select(), ClosestExpiry(), At(expiry), AtTheMoney(right), Calls/Puts and Strikes (with ClosestTo/FirstAbove/FirstBelow). All helpers are null-safe, count Saturday-convention expirations at their Friday last trading date, and support DTE windows to guard against selecting already-subscribed contracts outside the requested expiration range. Also fixes the OptionChain universe-contracts constructor never assigning the chain-level Underlying (the base constructor initializes it to an empty QuoteBar, so the null-coalescing assignment never fired).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Options algorithms keep re-deriving the same contract selection: nearest expiry to a target DTE, same-expiry filter, nearest-strike/ATM scan. The hand-rolled version crashes on empty
min(), misses Saturday-convention expiries, and picks 0-DTE contracts.New helpers on
OptionChain(both slice chains andalgorithm.option_chain()chains):Select(right, targetDte, minDte, maxDte, moneyness, targetDelta)— best-match single contract in one call.ClosestExpiry(targetDte, minDte, maxDte)— nearest expiration; ties prefer the earlier one.At(expiry)— single-expiration chain that composes with the other helpers.Calls/Puts— sorted by expiration then strike.AtTheMoney(right)— strike closest to the underlying price.Strikes(newStrikeListtype) — distinct sorted strikes withClosestTo,FirstAbove,FirstBelow.Design points:
null/Noneinstead of throwing, so callers guard with one null check.At()matches by that date.OptionChains reported a zero underlying price (Underlying ??=never assigned).Deferred:
contract.days_to_expiry/dteships separately; pruning out-of-window subscribed contracts from slice chains is out of scope.Related Issue
N/A
Motivation and Context
This exact selection is hand-built over and over (~10–15 lines per entry routine) and recurrently hits empty-sequence crashes, Saturday-expiry zero matches, and 0-DTE picks.
Requires Documentation Change
Yes: the new
OptionChainmembers andStrikeList(XML docs included).How Has This Been Tested?
OptionChainTestsfixture: 38 tests covering ceremony equivalence, moneyness/delta targeting, DTE windows, tie-breaking, Saturday expiries, composition throughAt(), the underlying-price fix, and null-safety on empty chains.OptionChainSelectionHelpersRegressionAlgorithm(C# + Python): asserts each helper against the hand-rolled equivalent and trades the selected contract; both languages pass with matching statistics.[PandasIgnore]d, so data frames are unchanged.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>