Skip to content

Commit

Permalink
IB bug fixes (#1302)
Browse files Browse the repository at this point in the history
- Fix data fetch with `PriceType.LAST` for Crypto
- Add `async-timeout` package
- Make `request_timeout` configurable
  • Loading branch information
rsmb7z authored Oct 24, 2023
1 parent 6924465 commit 035b4fa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
6 changes: 3 additions & 3 deletions nautilus_trader/adapters/interactive_brokers/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ async def get_historical_bars(
endDateTime=end_date_time,
durationStr=duration,
barSizeSetting=bar_size_setting,
whatToShow=what_to_show[bar_type.spec.price_type],
whatToShow=what_to_show(bar_type),
useRTH=use_rth,
formatDate=2,
keepUpToDate=False,
Expand Down Expand Up @@ -1192,7 +1192,7 @@ async def subscribe_historical_bars(
endDateTime="",
durationStr=timedelta_to_duration_str(duration),
barSizeSetting=bar_size_setting,
whatToShow=what_to_show[bar_type.spec.price_type],
whatToShow=what_to_show(bar_type),
useRTH=use_rth,
formatDate=2,
keepUpToDate=True,
Expand Down Expand Up @@ -1395,7 +1395,7 @@ async def subscribe_realtime_bars(
reqId=req_id,
contract=contract,
barSize=bar_type.spec.step,
whatToShow=what_to_show[bar_type.spec.price_type],
whatToShow=what_to_show(bar_type),
useRTH=use_rth,
realTimeBarsOptions=[],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class BarDataDownloaderConfig(ActorConfig):
bar_types: list[str]
handler: Callable
freq: str = "1W"
request_timeout: int = 30


class BarDataDownloader(AsyncActor):
Expand Down Expand Up @@ -67,7 +68,7 @@ async def _on_start(self):
instrument_ids = {bar_type.instrument_id for bar_type in self.bar_types}
for instrument_id in instrument_ids:
request_id = self.request_instrument(instrument_id)
await self.await_request(request_id)
await self.await_request(request_id, timeout=self.config.request_timeout)

request_dates = list(pd.date_range(self.start_time, self.end_time, freq=self.freq))

Expand All @@ -78,7 +79,7 @@ async def _on_start(self):
start=request_date,
end=request_date + pd.Timedelta(self.freq),
)
await self.await_request(request_id)
await self.await_request(request_id, timeout=self.config.request_timeout)

self.stop()

Expand Down
19 changes: 13 additions & 6 deletions nautilus_trader/adapters/interactive_brokers/parsing/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from nautilus_trader.core.datetime import nanos_to_secs
from nautilus_trader.model.data import BarAggregation
from nautilus_trader.model.data import BarSpecification
from nautilus_trader.model.data import BarType
from nautilus_trader.model.enums import BookAction
from nautilus_trader.model.enums import OrderSide
from nautilus_trader.model.enums import PriceType
Expand All @@ -44,12 +45,18 @@
4: "MidPoint",
}

what_to_show = {
PriceType.ASK: "ASK",
PriceType.BID: "BID",
PriceType.LAST: "TRADES",
PriceType.MID: "MIDPOINT",
}

def what_to_show(bar_type: BarType) -> str:
mapping = {
PriceType.ASK: "ASK",
PriceType.BID: "BID",
PriceType.LAST: "TRADES",
PriceType.MID: "MIDPOINT",
}
if str(bar_type.instrument_id.venue) == "PAXOS" and bar_type.spec.price_type == PriceType.LAST:
return "AGGTRADES"
else:
return mapping[bar_type.spec.price_type]


def generate_trade_id(ts_event: int, price: float, size: Decimal) -> TradeId:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ uvloop = {version = "^0.19.0", markers = "sys_platform != 'win32'"}
hiredis = {version = "^2.2.3", optional = true}
redis = {version = "^5.0.1", optional = true}
docker = {version = "^6.1.3", optional = true}
async-timeout = "^4.0.3"
nautilus_ibapi = {version = "==1019.1", optional = true} # Pinned for stability
betfair_parser = {version = "==0.7.1", optional = true} # Pinned for stability

[tool.poetry.extras]
betfair = ["betfair_parser"]
docker = ["docker"]
ib = ["nautilus_ibapi"]
ib = ["nautilus_ibapi", "async-timeout"]
redis = ["hiredis", "redis"]

[tool.poetry.group.dev]
Expand Down

0 comments on commit 035b4fa

Please sign in to comment.