@@ -184,6 +184,8 @@ def fetch_quote_snapshots(
184184 symbols : list [str ] | tuple [str , ...] | set [str ],
185185 * ,
186186 wait_seconds : float = 3.0 ,
187+ retry_wait_seconds : float = 1.5 ,
188+ attempts_per_data_type : int = 2 ,
187189 exchange : str = "SMART" ,
188190 currency : str = "USD" ,
189191 stock_factory : Callable [..., Any ] | None = None ,
@@ -199,38 +201,39 @@ def fetch_quote_snapshots(
199201 ib .qualifyContracts (contract )
200202 contracts .append ((symbol , contract ))
201203
202- snapshots = _collect_quote_snapshots (
203- ib ,
204- contracts ,
205- wait_seconds = wait_seconds ,
206- currency = currency ,
207- )
208- missing_contracts = [(symbol , contract ) for symbol , contract in contracts if symbol not in snapshots ]
209- if not missing_contracts :
210- return snapshots
204+ snapshots : dict [str , QuoteSnapshot ] = {}
205+ missing_contracts = list (contracts )
206+ attempts_per_data_type = max (int (attempts_per_data_type or 1 ), 1 )
211207
212208 setter = getattr (ib , "reqMarketDataType" , None )
213- if not callable (setter ):
214- return snapshots
209+ market_data_types = (1 , 2 , 4 ) if callable (setter ) else (1 ,)
215210
216211 try :
217- for market_data_type in (2 , 4 ):
218- _set_market_data_type (ib , market_data_type )
219- recovered = _collect_quote_snapshots (
220- ib ,
221- missing_contracts ,
222- wait_seconds = wait_seconds ,
223- currency = currency ,
224- )
225- snapshots .update (recovered )
226- missing_contracts = [
227- (symbol , contract )
228- for symbol , contract in missing_contracts
229- if symbol not in recovered
230- ]
212+ for market_data_type in market_data_types :
213+ if callable (setter ):
214+ _set_market_data_type (ib , market_data_type )
215+
216+ for attempt_index in range (attempts_per_data_type ):
217+ wait_for_attempt = wait_seconds if attempt_index == 0 else retry_wait_seconds
218+ recovered = _collect_quote_snapshots (
219+ ib ,
220+ missing_contracts ,
221+ wait_seconds = wait_for_attempt ,
222+ currency = currency ,
223+ )
224+ snapshots .update (recovered )
225+ missing_contracts = [
226+ (symbol , contract )
227+ for symbol , contract in missing_contracts
228+ if symbol not in snapshots
229+ ]
230+ if not missing_contracts :
231+ break
232+
231233 if not missing_contracts :
232234 break
233235 finally :
234- _set_market_data_type (ib , 1 )
236+ if callable (setter ):
237+ _set_market_data_type (ib , 1 )
235238
236239 return snapshots
0 commit comments