Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Ambiguous Contract Description #141

Open
4 tasks done
AlexCatarino opened this issue Jan 8, 2025 · 4 comments
Open
4 tasks done

Handle Ambiguous Contract Description #141

AlexCatarino opened this issue Jan 8, 2025 · 4 comments

Comments

@AlexCatarino
Copy link
Member

Expected Behavior

If we get the "ambiguous contract description" error, we try to choose the correct contract.

Actual Behavior

LEAN exist.

2025-01-08T17:16:52.0276524Z TRACE:: EventBasedDataQueueHandlerSubscriptionManager.Subscribe(): SPY 250108C00591000
2025-01-08T17:16:52.0285534Z TRACE:: InteractiveBrokersBrokerage.Subscribe(): Subscribe Request: SPY 250108C00591000
2025-01-08T17:16:52.0319947Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): SPY 250108C00591000 (OPT SPY USD Smart)
2025-01-08T17:16:52.8429421Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): clientOnContractDetails event: OPT SPY USD SMART 20250108 591 C SPY
2025-01-08T17:16:52.8435285Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): contracts found: 2
2025-01-08T17:16:52.8468728Z TRACE:: InteractiveBrokersBrokerage.Subscribe(): Subscribe Processed: SPY 250108C00591000 (OPT SPY USD Smart 20250108 591 C) # 5. SubscribedSymbols.Count: 1
2025-01-08T17:16:52.8484828Z TRACE:: DataManager.AddSubscription(): Added SPY 250108C00591000,#0,SPY,Minute,QuoteBar,Quote,Raw,OpenInterest. Start: 1/8/2025 5:16:52 PM. End: 12/31/2050 5:00:00 AM
2025-01-08T17:16:52.8541028Z TRACE:: DataManager.AddSubscription(): Added SPY 250108C00591000,#0,SPY,Minute,TradeBar,Trade,Raw,OpenInterest. Start: 1/8/2025 5:16:52 PM. End: 12/31/2050 5:00:00 AM
2025-01-08T17:16:52.8555233Z TRACE:: DataManager.AddSubscription(): Added SPY 250108C00591000,#0,SPY,Minute,OpenInterest,OpenInterest,Raw,OpenInterest,Internal. Start: 1/8/2025 5:00:00 AM. End: 12/31/2050 5:00:00 AM
2025-01-08T17:16:53.2809796Z TRACE:: EventBasedDataQueueHandlerSubscriptionManager.Subscribe(): SPY
2025-01-08T17:16:53.2814848Z TRACE:: InteractiveBrokersBrokerage.Subscribe(): Subscribe Request: SPY
2025-01-08T17:16:53.2823305Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): SPY (STK SPY USD Smart)
2025-01-08T17:16:53.3568480Z TRACE:: InteractiveBrokersBrokerage.HandleError(): RequestId: 5 ErrorCode: 200 - The contract description specified for SPY is ambiguous.. Origin: [Id=5] Subscribe: SPY 250108C00591000 (OPT SPY USD Smart 20250108 591 C)
2025-01-08T17:16:53.3605450Z ERROR:: Brokerage.OnMessage(): Error - Code: 200 - The contract description specified for SPY is ambiguous.. Origin: [Id=5] Subscribe: SPY 250108C00591000 (OPT SPY USD Smart 20250108 591 C)

Potential Solution

N/A. Perhaps we can do something when we find more than one.
2025-01-08T17:16:52.8435285Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): contracts found: 2

Reproducing the Problem

# region imports
from AlgorithmImports import *
# endregion
class AmbiguousContractDescription(QCAlgorithm):
    def initialize(self):
        self.set_start_date(2025,1,8)
        self.add_option_contract(
            Symbol.create_option(
                Symbol.create("SPY", SecurityType.EQUITY, Market.USA), Market.USA,
                OptionStyle.AMERICAN, OptionRight.CALL, 591, self.start_date
            )
        )

Checklist

  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
  • I have provided detailed steps to reproduce the issue
@creatidy
Copy link

Yesterday (January 8, 2025) I observed a similar issue with 0DTE SPX options and investigated the root cause. After analyzing the available information about similar cases in the past, I concluded that this issue arises when two contracts share the same last trading date. Typically, the last trading date of an option should coincide with its expiration date. However, in this case, it seems there is a discrepancy.

Today (January 9, 2025), I noticed a lack of trading activity, which aligns with Biden's recent decision. This unexpected absence of trading reinforces my suspicion that the ambiguous contract description error occurs when there are multiple contracts with the same last trading date but differing expiration or other attributes. This behavior seems to confuse the system when determining the correct contract.

@Martin-Molinero
Copy link
Member

Hi @creatidy!
Thank for you looking into it, you are correct. Because options originally scheduled to expire on January 9 expired on Wednesday, January 8 the contract lookup system failed to resolve it. We are asking IB support to see if there's a way around it so it does not happen again

@Martin-Molinero
Copy link
Member

The issue is no longer reproductible, to get around it though we would need to perform FindContracts->reqContractDetails which would return both contracts and we would need to filter by ContractDetails.RealExpirationDate to finally get the ConId and use that to identify the contract we want when talking with IB

@chadneiro1
Copy link

Thanks for looking into this. It seems that this occurred because of the unexpected death of Jimmy Carter which caused them to change the market schedule. I will start paper trading again next week and see if this pops up again but it would still be nice to know that this won't pop up again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants