I have one third-party API-kit module (bitmex). I have many calls to this module in my code. The problem is that this API-kit often gives me an error:
HTTPServiceUnavailable
I understand that this error is due to unstable BitMEX servers. But if it occurs during a trading session, it completely destroys my trading.
Here is an example of calling the bitmex module in my code:
balance = bitmex.User.User_getMargin(currency='XBt').result()
Of course, I tried to solve the problem. Here is the function I came up with:
def bitmex_error_protection(request):
retries = 0
while True:
try:
result = eval(request)
except bravado.exception.HTTPServiceUnavailable:
time.sleep(5)
retries += 1
if retries > 5:
raise Exception("The servers are overloaded, the max number of attempts has been reached")
else:
return result
As you understand, the meaning of this function is that I pass it a request in a packed form (in a string object). After that, there will be "While True" until the request is executed without this error.
Here is an example of how I am now making these calls:
balance = bitmex_error_protection("bitmex.User.User_getMargin(currency='XBt').result()")
This solution works, but I understand that it is far from elegant and rests on crutches.
I will be glad if you suggest a couple of ideas or completely alternative methods for solving this problem, where I don't need to use a string object.
Maybe for this, I need to create a class?