From 67fd01f19f4c4cef5fbbef741a29170063405b65 Mon Sep 17 00:00:00 2001 From: Ben Singleton Date: Tue, 13 Feb 2024 21:38:14 -0500 Subject: [PATCH 1/3] Fix historicalDataUpdate bug --- .../interactive_brokers/client/common.py | 3 ++- .../interactive_brokers/client/market_data.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/nautilus_trader/adapters/interactive_brokers/client/common.py b/nautilus_trader/adapters/interactive_brokers/client/common.py index caa22d39f3eb..7b6235fdd514 100644 --- a/nautilus_trader/adapters/interactive_brokers/client/common.py +++ b/nautilus_trader/adapters/interactive_brokers/client/common.py @@ -14,6 +14,7 @@ # ------------------------------------------------------------------------------------------------- import asyncio +import functools from abc import ABC from abc import abstractmethod from collections.abc import Callable @@ -70,7 +71,7 @@ class Subscription(msgspec.Struct, frozen=True): req_id: Annotated[int, msgspec.Meta(gt=0)] name: str | tuple - handle: Callable + handle: functools.partial | Callable cancel: Callable last: Any diff --git a/nautilus_trader/adapters/interactive_brokers/client/market_data.py b/nautilus_trader/adapters/interactive_brokers/client/market_data.py index c27e47d5e2ec..83a25b0c8b78 100644 --- a/nautilus_trader/adapters/interactive_brokers/client/market_data.py +++ b/nautilus_trader/adapters/interactive_brokers/client/market_data.py @@ -78,7 +78,7 @@ async def set_market_data_type(self, market_data_type: MarketDataTypeEnum) -> No async def _subscribe( self, name: str | tuple, - subscription_method: Callable, + subscription_method: Callable | functools.partial, cancellation_method: Callable, *args: Any, **kwargs: Any, @@ -274,10 +274,10 @@ async def subscribe_historical_bars( name, self.subscribe_historical_bars, self._eclient.cancelHistoricalData, - bar_type, - contract, - use_rth, - handle_revised_bars, + bar_type=bar_type, + contract=contract, + use_rth=use_rth, + handle_revised_bars=handle_revised_bars, ) if not subscription: return @@ -815,10 +815,14 @@ def historicalDataUpdate(self, req_id: int, bar: BarData) -> None: self.logAnswer(current_fn_name(), vars()) if not (subscription := self._subscriptions.get(req_id=req_id)): return + if isinstance(subscription.handle, functools.partial): + handle_revised_bars = subscription.handle.keywords.get("handle_revised_bars", False) + else: + handle_revised_bars = False if bar := self._process_bar_data( bar_type_str=str(subscription.name), bar=bar, - handle_revised_bars=subscription.handle().keywords.get("handle_revised_bars", False), + handle_revised_bars=handle_revised_bars, ): if bar.is_single_price() and bar.open.as_double() == 0: self._log.debug(f"Ignoring Zero priced {bar=}") From 064fbaa1a0815b013276af18df1b28f2d613717c Mon Sep 17 00:00:00 2001 From: Ben Singleton Date: Tue, 13 Feb 2024 21:40:34 -0500 Subject: [PATCH 2/3] Fix historicalDataUpdate bug --- .../adapters/interactive_brokers/client/market_data.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/nautilus_trader/adapters/interactive_brokers/client/market_data.py b/nautilus_trader/adapters/interactive_brokers/client/market_data.py index 83a25b0c8b78..747855ff351d 100644 --- a/nautilus_trader/adapters/interactive_brokers/client/market_data.py +++ b/nautilus_trader/adapters/interactive_brokers/client/market_data.py @@ -815,14 +815,12 @@ def historicalDataUpdate(self, req_id: int, bar: BarData) -> None: self.logAnswer(current_fn_name(), vars()) if not (subscription := self._subscriptions.get(req_id=req_id)): return - if isinstance(subscription.handle, functools.partial): - handle_revised_bars = subscription.handle.keywords.get("handle_revised_bars", False) - else: - handle_revised_bars = False + if not isinstance(subscription.handle, functools.partial): + raise ValueError(f"Unexpected {subscription=}") if bar := self._process_bar_data( bar_type_str=str(subscription.name), bar=bar, - handle_revised_bars=handle_revised_bars, + handle_revised_bars=subscription.handle.keywords.get("handle_revised_bars", False), ): if bar.is_single_price() and bar.open.as_double() == 0: self._log.debug(f"Ignoring Zero priced {bar=}") From ff7ed6ee8d28c9986f5d9525c841ffa5502ecf93 Mon Sep 17 00:00:00 2001 From: Ben Singleton Date: Tue, 13 Feb 2024 21:43:46 -0500 Subject: [PATCH 3/3] fix error --- .../adapters/interactive_brokers/client/market_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nautilus_trader/adapters/interactive_brokers/client/market_data.py b/nautilus_trader/adapters/interactive_brokers/client/market_data.py index 747855ff351d..a3ecdb4cbd78 100644 --- a/nautilus_trader/adapters/interactive_brokers/client/market_data.py +++ b/nautilus_trader/adapters/interactive_brokers/client/market_data.py @@ -816,7 +816,7 @@ def historicalDataUpdate(self, req_id: int, bar: BarData) -> None: if not (subscription := self._subscriptions.get(req_id=req_id)): return if not isinstance(subscription.handle, functools.partial): - raise ValueError(f"Unexpected {subscription=}") + raise TypeError(f"Expecting partial type subscription method. {subscription=}") if bar := self._process_bar_data( bar_type_str=str(subscription.name), bar=bar,