BitMEX / sample-market-maker

Sample BitMEX Market Making Bot
Apache License 2.0
1.7k stars 758 forks source link

ERROR - _logging - close status: 31522 #62

Closed shahidjalally closed 6 years ago

shahidjalally commented 6 years ago

Hello, I'm using this bot on testnet environment since ~20 days and it is working smooth on it. and not occur any rate limit error in during whole period and also generates 200% profit in ~20 days. and while I put it on real account it starts giving below errors after few hours of it's start. while I understand somehow this error I found that it is about API rate limit issue.

ERROR - _logging - close status: 31522

I have also sent and request email to increase rate limits for my account at support@bitmex.com as per their policy and announcement which they have published on API reference section. but I have not received any reply yet.

Now please guide me here what should I needs to do.

ryanfox commented 6 years ago

Thanks for the info. Can you give more information on what is happening when the error is raised? Is there more to the error message?

shahidjalally commented 6 years ago

hello ryanfox, thank for your response. I'm assured that it is about rate limit issue. Bot is stopped after this error. and It allow to re-login after some minutes. It is just like that some where rate limit is reduced or exceeding and time limit plenty prevent to bot to login. I have tries to run live account twice with 20 hours interval and found same error both time with 5 minutes of start the bot.

further I have attached screen shot.

screenshot_1

ryanfox commented 6 years ago

If you are being rate limited, the best idea is to wait until enough time has passed to clear the limit. You might also consider using the websocket API - it streams updates so you don't hit rate limits as quickly.

There is also a HTTP 503 error in that log - BitMEX returns that error code when the trading engine is overloaded.

drachmabtc commented 6 years ago

Was this issue resolved? I'm getting this error too, it has nothing to do with rate limit. I got the error with plenty left of my rate (298/300)

ryanfox commented 6 years ago

The application got an error response from BitMEX:

Unable to contact the BitMEX API (503)

Once the maximum number of retries was hit without completing the request, the application exited:

Max retries on order/bulk hit, raising
[...]
Shutting down. All open orders will be cancelled.

This can occur when BitMEX is under heavy load. You may want to wait and retry.

drachmabtc commented 6 years ago

Hi Ryanfox,

I'm digging further into this issue. Bitmex support staff doesn't agree that this error was caused by heavy load in this instance. I receive these errors from the Testnet API where there were no heavy loads. However I am narrowing down the area where the error is caused. I have narrowed it down to the ws_thread.py code, and specifically the function "def __on_message(self, ws, message):" the '''Handler for parsing WS messages.'''. There is a looping that eventually ends in these error messages.

drachmabtc commented 6 years ago

Here is what I found that produces the Error 31522: At some point, while my code runs, I start getting more and more messages from the ws_thread. Even if my code does not generate any trades. Each message is then handled by the ws.thread.py code to parse and update my data table. The problem is that eventually I get a message from Websocket every second or less therefore preventing any other action, creating an endless loop and eventually it crashes. I don't think this has anything to do with my code. Rather this feels like a problem with the API.

Why am I getting 'orderBook10' and 'instrument' updates every second or less to update my data table?

Here is an example of the messages I get flooded with: {'table': 'orderBook10', 'action': 'update', 'data': [{'symbol': 'XBTH18', 'bids': [[10575.5, 105753], [10573.5, 211462], [10488, 524384], [10440.5, 1044060], [9000, 1200], [7505.5, 800], [7500, 50000], [6000, 2388045], [5500, 1000], [5289, 67]], 'asks': [[10591, 112058], [10593, 224115], [10644.5, 200], [10658, 100], [10678.5, 560287], [10726, 1120574], [10773.5, 50000], [10818.5, 1], [11410, 20099], [11510, 50000]], 'timestamp': '2018-02-21T16:44:39.441Z'}]}

ryanfox commented 6 years ago

The websocket receives all updates to the order book from BitMEX - without that, the market maker cannot place orders.

Have you modified the code at all? Can you reliably reproduce an error? I haven't seen that error before, having run the market maker for an hour+ at a time.

drachmabtc commented 6 years ago

Hi Ryanfox, I'm still investigating this issue, and no I have not modified my code (I used to run my code on the testnet for days without interruption). At least now I have ruled out the update messages from the websocket being the problem. What is happening is my code sooner or later gets stuck, or appears frozen either in a loop or is waiting for a particular api response. All the while messages from the websocket to update the orderbook10 continues, until finally everything shuts down with error 31522. I will report back when I find this bug.

ryanfox commented 6 years ago

If you can find where the loop is occurring, that would help to diagnose the problem.

drachmabtc commented 6 years ago

Well after putting many timestamps throughout my code to find the cause of the error, I now think it has to do with my upgrading my IDE Pycharm from version 2 to version 3. For some reason error 31522 has occurred for multiple reasons and not a singular reason. Even basic coding errors have caused this 31522 to show up. The only difference from before is my IDE version. After making some corrections, my code now has been running for 24 hours with no further error messages. If anything else comes up I will write again. Thx!

Denebulas commented 6 years ago

drachmabtc What corrections made the error go away ? For me it appear trading XBTUSD and not the futures.

viveshok commented 6 years ago

The close status: 31522 error is a second-order issue that occurs when the websocket client doesn't disconnect cleanly. The source cause is different for different people.

This is the line in the websocket-client library that spits the error

I don't have the time to dig into it but at first glance looks like an endian-ess issue. The expected status codes are unsigned shorts between 1000 and 1015, and 31522 is suspiciously close to 2^15 == 32768

ryanfox commented 6 years ago

Closing since this seems to be outside the scope of the market maker, and no further information is available.