Voyz / ibind

IBind is a REST and WebSocket client library for Interactive Brokers Client Portal Web API.
Apache License 2.0
61 stars 5 forks source link

Auto pacing on 429 error #15

Open exallon72 opened 4 days ago

exallon72 commented 4 days ago

It would be great if you could make the API auto pace itself on 429 errors.

Voyz commented 4 days ago

hey @exallon72 thanks for the suggestion 👍

Could you point out how this could be implemented? Would you see IBind keep an internal request counter and throttle requests upon seeing 429? How would you imagine such an interaction would look like from your app's perspective?

exallon72 commented 14 hours ago

It's a good question. First it should probably not allow any more requests to be done within the same session to that endpoint.

Then I got a strange error. Tried with a couple of BTC. That I apparently do not have access to. It retiried 3 times. But then I got a 429. Indicating to me that it seems to loop a lot more than that since IB is usually not that picky. Unfortunately I don't have the log for the failed attempts left, but should be easy to replicate by using an asset you don't have market data access.

Traceback (most recent call last): File "/home/jocke/PycharmProjects/test/.venv/lib/python3.12/site-packages/ibind/base/rest_client.py", line 163, in request return self._process_response(response, result) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/jocke/PycharmProjects/test/.venv/lib/python3.12/site-packages/ibind/base/rest_client.py", line 188, in _process_response raise ExternalBrokerError(f'{self}: response error {result} :: {response.status_code} :: {response.reason} :: {response.text}', status_code=response.status_code) from e ibind.support.errors.ExternalBrokerError: IbkrClient: response error Result(data=None, request={'url': 'https://192.168.0.13:5000/v1/api/iserver/marketdata/history', 'params': {'conid': '475655817', 'bar': '1min', 'exchange': 'SMART', 'period': '1d', 'outsideRth': False, 'startTime': '20190101-10:00:00'}}) :: 429 :: Too Many Requests :: {"error":"Too many history chart requests, please try again later."} 2024-07-06 08:29:03,492 - ERROR - IbkrClient: response error Result(data=None, request={'url': 'https://192.168.0.13:5000/v1/api/iserver/marketdata/history', 'params': {'conid': '475655817', 'bar': '1min', 'exchange': 'SMART', 'period': '1d', 'outsideRth': False, 'startTime': '20190101-10:00:00'}}) :: 429 :: Too Many Requests :: {"error":"Too many history chart requests, please try again later."} Traceback (most recent call last):

exallon72 commented 14 hours ago

One small improvement could be also for the 429 not just send ExternalBrokerError since you then have to investigate the error to see if it's a 429.

exallon72 commented 14 hours ago

Adding to that. If you don't have market data access. It should not do any retries. This was using: marketdata_history_by_conid