Add contract days_to_expiry/dte and interpret Python date/datetime type errors - #9665
Closed
jhonabreul wants to merge 1 commit into
Closed
Add contract days_to_expiry/dte and interpret Python date/datetime type errors#9665jhonabreul wants to merge 1 commit into
jhonabreul wants to merge 1 commit into
Conversation
… errors - BaseContract.DaysToExpiry(reference)/DTE on option and future contracts; from Python the reference accepts both datetime and date instances - UnsupportedOperandPythonExceptionInterpreter also interprets the datetime-vs-date comparison TypeError and appends a hint pointing at .date() alignment and days_to_expiry/dte for the datetime-date shapes - New TzInfoPythonExceptionInterpreter for TimeZones values passed as Python tzinfo, pointing at zoneinfo; AlgorithmImports imports ZoneInfo - New GenericTypeParameterPythonExceptionInterpreter for pythonnet's 'type(s) expected' when parameterizing generics with Python types - ContractDaysToExpiryRegressionAlgorithm (C# + Python) and unit tests Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Collaborator
Author
|
Superseded: this surface is already addressed by pythonnet PR #143 (datetime/date coercion at the interop layer). Closing per review. |
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
Python algorithms mix .NET-converted
datetimevalues (self.time, expiries) with pure Pythondateobjects and die on DTE math:The changes:
contract.days_to_expiry(reference)/contract.dteon option and future contracts: whole days to expiry, date parts only. The reference acceptsdatetimeordatefrom Python and defaults to the contract's current time..date()alignment anddays_to_expiry/dte.datetime.now(TimeZones.NEW_YORK)now explains Lean time zones are nottzinfoand points atZoneInfo("America/New_York").AlgorithmImportsnow importsZoneInfo, so the hinted fix works without an extra import.type(s) expectederror (e.g.RollingWindow[datetime](30)) now explains that Python types cannot parameterize .NET generics and names the alternatives.Related work: QuantConnect/pythonnet#143 adds datetime/date coercion at the interop layer; the hints here still cover pure-Python mixes.
Deferred (needs interop-layer changes): making
RollingWindow[datetime]work, and making Lean time zones directly usable astzinfo.Related Issue
N/A
Motivation and Context
DTE filtering is line one of nearly every options/futures selector, and these exact errors are easy to hit and hard to decode.
Requires Documentation Change
Yes: document
days_to_expiry/dteand theZoneInfoavailability.How Has This Been Tested?
ContractDaysToExpiryRegressionAlgorithm(C# + Python): validatesdays_to_expiry/dteagainst manual expiry math; the Python twin passesdatetime,dateand keyword references.OptionContractTests/FuturesContractTests: default and explicit references, plus Python binding tests.Tests.Common.Exceptions+Tests.Common.Data.Marketnamespaces: 211 passed, 0 failed.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>