Repository: nautechsystems/nautilus_trader
File: nautilus_trader/adapters/interactive_brokers/client/market_data.py (Python)

Code before:
        if "first_start_ns" not in params:
            params["first_start_ns"] = start

        subscription = await self._subscribe(
            name,
            self.subscribe_historical_bars,
            self._eclient.cancelHistoricalData,
            bar_type=bar_type,
            contract=contract,
            use_rth=use_rth,
            handle_revised_bars=handle_revised_bars,
            params=params,
        )

        # In order to get missed bars after a disconnection
        if (
            self._last_disconnection_ns is not None
            and self._last_disconnection_ns > params["first_start_ns"]
        ):
            start = self._last_disconnection_ns

        # Store start time separately for bar filtering (not part of resubscription handle)
        self._subscription_start_times[subscription.req_id] = start

        bar_size_setting: str = bar_spec_to_bar_size(bar_type.spec)
        self._eclient.reqHistoricalData(
            reqId=subscription.req_id,
            contract=contract,
            endDateTime="",
            durationStr=duration_str,
            barSizeSetting=bar_size_setting,
            whatToShow=what_to_show(bar_type),
            useRTH=use_rth,
            formatDate=2,
            keepUpToDate=True,
            chartOptions=[],
        )

    async def unsubscribe_historical_bars(self, bar_type: BarType) -> None:
        """
        Unsubscribe from historical bar data for a specified bar type.

        Parameters
        ----------
        bar_type : BarType
            The type of bar to unsubscribe from.

        """
        name = str(bar_type)

        # Clean up stored start time before unsubscribing

Review comment (suggestion):
Was just testing this via manual TWS disconnection for 2 minutes.
Got a traceback on reconnection:

```bash
TypeError('>' not supported between instances of 'Timestamp' and 'int')
Traceback (most recent call last):
  File "/Users/john/Desktop/code/algo_trade2/.venv/lib/python3.11/site-packages/nautilus_trader/adapters/interactive_brokers/client/client.py", line 342, in _resubscribe_all
    await subscription.handle()
  File "/Users/john/Desktop/code/algo_trade2/.venv/lib/python3.11/site-packages/nautilus_trader/adapters/interactive_brokers/client/market_data.py", line 474, in subscribe_historical_bars
    and self._last_disconnection_ns > params["first_start_ns"]
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
Seems like no bars are received afterwards.

Code after:
            start = now
            duration_str = timedelta_to_duration_str(
                pd.Timedelta(bar_type.spec.timedelta.total_seconds() * 300, "sec"),
            )  # Download approx 300 bars

        if "first_start_ns" not in params:
            params["first_start_ns"] = start

        subscription = await self._subscribe(
            name,
            self.subscribe_historical_bars,
            self._eclient.cancelHistoricalData,
            bar_type=bar_type,
            contract=contract,
            use_rth=use_rth,
            handle_revised_bars=handle_revised_bars,
            params=params,
        )

        # In order to get missed bars after a disconnection
        if (
            self._last_disconnection_ns is not None
            and self._last_disconnection_ns > params["first_start_ns"]
        ):
            start = self._last_disconnection_ns

        # Store start time separately for bar filtering (not part of resubscription handle)
        self._subscription_start_times[subscription.req_id] = start

        bar_size_setting: str = bar_spec_to_bar_size(bar_type.spec)
        self._eclient.reqHistoricalData(
            reqId=subscription.req_id,
            contract=contract,
            endDateTime="",
            durationStr=duration_str,
            barSizeSetting=bar_size_setting,
            whatToShow=what_to_show(bar_type),
            useRTH=use_rth,
            formatDate=2,
            keepUpToDate=True,
            chartOptions=[],
        )

    async def unsubscribe_historical_bars(self, bar_type: BarType) -> None:
        """
        Unsubscribe from historical bar data for a specified bar type.

        Parameters
        ----------
        bar_type : BarType
            The type of bar to unsubscribe from.