QuantConnect / Lean.Brokerages.InteractiveBrokers

InteractiveBrokers Brokerage Plugin
Apache License 2.0
31 stars 20 forks source link

Improve Speed of Security Addition and Warmup #98

Open AlexCatarino opened 7 months ago

AlexCatarino commented 7 months ago

Expected Behavior

Be able to subscribe and warm up more than 300 options.

Actual Behavior

It takes 2.2 seconds to add security, and make a historical request to warm it up:

2024-02-28T23:46:14.7609004Z TRACE:: InfluxDbHistoryProvider.GetHistory(): SPX   240315P05025000 IndexOption 2024-02-28T21:10:00.0000000Z to 2024-02-28T23:46:00.8007143Z. QuoteBar. Quote. Minute
2024-02-28T23:46:14.7609481Z TRACE:: InfluxDbHistoryProvider.GetHistory(): SPX   240315P05025000 IndexOption 2024-02-28T21:10:00.0000000Z to 2024-02-28T23:46:00.8007143Z. TradeBar. Trade. Minute
2024-02-28T23:46:14.7611697Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): SPX   240315P05025000 (OPT SPX USD Smart)
2024-02-28T23:46:15.3380276Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): clientOnContractDetails event: OPT SPX USD SMART 20240314 5025 P SPX
2024-02-28T23:46:15.3390762Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): contracts found: 1
2024-02-28T23:46:15.3391535Z TRACE:: InteractiveBrokersBrokerage::GetHistory(): Submitting request: SPX   240315P05025000 (OPT SPX USD Smart  20240314 5025 P): Minute/Quote 2/28/2024 9:10:00 PM UTC -> 2/28/2024 11:46:00 PM UTC
2024-02-28T23:46:16.0407240Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): SPX   240315P05025000 (OPT SPX USD Smart)
2024-02-28T23:46:16.2256337Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): clientOnContractDetails event: OPT SPX USD SMART 20240314 5025 P SPX
2024-02-28T23:46:16.2680131Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): contracts found: 1
2024-02-28T23:46:16.2681073Z TRACE:: InteractiveBrokersBrokerage::GetHistory(): Submitting request: SPX   240315P05025000 (OPT SPX USD Smart  20240314 5025 P): Minute/Trade 2/28/2024 9:10:00 PM UTC -> 2/28/2024 11:46:00 PM UTC
2024-02-28T23:46:16.7629965Z TRACE:: InteractiveBrokersBrokerage::GetHistory(): Download completed: SPX   240315P05025000 (OPT SPX USD Smart  20240314 5025 P)
2024-02-28T23:46:16.7643986Z TRACE:: EventBasedDataQueueHandlerSubscriptionManager.Subscribe(): SPX   240315P05025000
2024-02-28T23:46:16.7644386Z TRACE:: InteractiveBrokersBrokerage.Subscribe(): Subscribe Request: SPX   240315P05025000
2024-02-28T23:46:16.7645160Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): SPX   240315P05025000 (OPT SPX USD Smart)
2024-02-28T23:46:16.9488177Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): clientOnContractDetails event: OPT SPX USD SMART 20240314 5025 P SPX
2024-02-28T23:46:16.9922404Z TRACE:: InteractiveBrokersBrokerage.GetContractDetails(): contracts found: 1
2024-02-28T23:46:16.9924096Z TRACE:: InteractiveBrokersBrokerage.Subscribe(): Subscribe Processed: SPX   240315P05025000 (OPT SPX USD Smart  20240314 5025 P) # 22. SubscribedSymbols.Count: 5
2024-02-28T23:46:16.9925928Z TRACE:: DataManager.AddSubscription(): Added SPX   240315P05025000,#0,SPX,Minute,QuoteBar,Quote,Raw,OpenInterest. Start: 2/28/2024 11:46:00 PM. End: 12/31/2050 5:00:00 AM
2024-02-28T23:46:16.9928868Z TRACE:: DataManager.AddSubscription(): Added SPX   240315P05025000,#0,SPX,Minute,TradeBar,Trade,Raw,OpenInterest. Start: 2/28/2024 11:46:00 PM. End: 12/31/2050 5:00:00 AM
2024-02-28T23:46:16.9932410Z TRACE:: DataManager.AddSubscription(): Added SPX   240315P05025000,#0,SPX,Minute,OpenInterest,OpenInterest,Raw,OpenInterest,Internal. Start: 2/28/2024 6:00:00 AM. End: 12/31/2050 5:00:00 AM

It leads to a 10-minute time out with less than 300 contracts: 10 min x 60 = 600 s / 2 s = 300 contracts

Potential Solution

See https://github.com/QuantConnect/Lean.Brokerages.InteractiveBrokers/issues/48

Reproducing the Problem

class IndexOptionsAlgorithm(QCAlgorithm):

    def Initialize(self):
        seeder = FuncSecuritySeeder(self.GetLastKnownPrices)
        self.SetSecurityInitializer(lambda security: seeder.SeedSecurity(security))
        index_symbol = self.AddIndex("SPX").Symbol
        option = self.AddIndexOption(index_symbol)
        option.SetFilter(lambda u: u.Strikes(-20, 1).Expiration(0, 100).PutsOnly().StandardsOnly())

Checklist