Skip to content

Fix IB-TWS connection issue with international languages #2726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ async def _connect(self) -> None:
)
await self._receive_server_info()
self._eclient.setConnState(EClient.CONNECTED)
conn_time_str = self._msgspec_decoding_hook(self._eclient.connTime)
self._log.info(
f"Connected to Interactive Brokers (v{self._eclient.serverVersion_}) "
f"at {self._eclient.connTime.decode()} from {self._host}:{self._port} "
f"at {conn_time_str} from {self._host}:{self._port} "
f"with client id: {self._client_id}",
)
except ConnectionError:
Expand All @@ -81,6 +82,17 @@ async def _connect(self) -> None:
if self._eclient.wrapper:
self._eclient.wrapper.error(NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg())

def _msgspec_decoding_hook(self, byte_data: bytes) -> str:
"""
Decode connection time from the server for possible more languages.
"""
for enc in ["utf-8", "gbk", "latin-1", "cp1252"]:
try:
return byte_data.decode(enc)
except UnicodeDecodeError:
continue
return byte_data.decode("utf-8", errors="replace")

async def _disconnect(self) -> None:
"""
Disconnect from TWS/Gateway and clear the `_is_ib_connected` flag.
Expand Down
Loading