BitMEX / sample-market-maker

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

websocket closed sporadically #43

Closed davidjcooper closed 6 years ago

davidjcooper commented 6 years ago

Been running script for 7 days. It has disconnected randomly.

websocket disconnected.

Any chance to get an update that checks for the connection and reconnects if it is dropped?

ryanfox commented 6 years ago

Is there any more output? Does the program terminate?

You could use a script to restart the program automatically. Something like

 while true; ./marketmaker; done
karanratti commented 6 years ago

Hi Ryan, Here is the output from my run. The bot shutdown automatically.

2018-02-26 21:14:00,971 - INFO - market_maker - Amending Buy: 100 @ 10312.5 to 100 @ 10323.0 (+10.5) 2018-02-26 21:14:00,971 - INFO - bitmex - sending req to https://www.bitmex.com/api/v1/order/bulk: {"orders": [{"orderID": "0b63a8bb-9d20-c582-443a-a8dc35d6ac4a", "orderQty": 100, "price": 10323.0, "side": "Buy"}]} 2018-02-26 21:14:01,011 - WARNING - bitmex - Unable to contact the BitMEX API (503), retrying. Request: https://www.bitmex.com/api/v1/order/bulk {"orders": [{"orderID": "0b63a8bb-9d20-c582-443a-a8dc35d6ac4a", "orderQty": 100, "price": 10323.0, "side": "Buy"}]} Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/bitmex.py", line 263, in _curl_bitmex response.raise_for_status() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/models.py", line 935, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 503 Server Error: Service Unavailable for url: https://www.bitmex.com/api/v1/order/bulk

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/bin/marketmaker", line 11, in load_entry_point('bitmex-market-maker==1.3', 'console_scripts', 'marketmaker')() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/init.py", line 22, in run market_maker.run() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/market_maker.py", line 551, in run om.run_loop() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/market_maker.py", line 519, in run_loop self.place_orders() # Creates desired orders and converges to existing orders File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/market_maker.py", line 329, in place_orders return self.converge_orders(buy_orders, sell_orders) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/market_maker.py", line 400, in converge_orders self.exchange.amend_bulk_orders(to_amend) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/market_maker.py", line 188, in amend_bulk_orders return self.bitmex.amend_bulk_orders(orders) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/bitmex.py", line 104, in wrapped return fn(self, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/bitmex.py", line 168, in amend_bulk_orders return self._curl_bitmex(path='order/bulk', postdict={'orders': orders}, verb='PUT', rethrow_errors=True) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/bitmex.py", line 313, in _curl_bitmex return retry() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/market_maker/bitmex.py", line 252, in retry raise Exception("Max retries on %s (%s) hit, raising." % (path, json.dumps(postdict or ''))) Exception: Max retries on order/bulk ({"orders": [{"orderID": "0b63a8bb-9d20-c582-443a-a8dc35d6ac4a", "orderQty": 100, "price": 10323.0, "side": "Buy"}]}) hit, raising. 2018-02-26 21:14:04,395 - INFO - market_maker - Shutting down. All open orders will be cancelled. 2018-02-26 21:14:04,395 - INFO - market_maker - Resetting current position. Canceling all existing orders. 2018-02-26 21:14:04,395 - INFO - bitmex - sending req to https://www.bitmex.com/api/v1/order: {"filter": "{\"ordStatus.isTerminated\": false, \"symbol\": \"XBTUSD\"}", "count": 500} 2018-02-26 21:14:04,432 - INFO - market_maker - Canceling: Buy 100 @ 10312.5 2018-02-26 21:14:04,432 - INFO - bitmex - sending req to https://www.bitmex.com/api/v1/order: {"orderID": ["0b63a8bb-9d20-c582-443a-a8dc35d6ac4a"]} 2018-02-26 21:14:10,988 - ERROR - _logging - close status: 31522 2018-02-26 21:14:11,017 - INFO - ws_thread - Websocket Closed

Where do i add the script that you mentioned in your last post? right at the end?

Thanks

ryanfox commented 6 years ago

The script at the end is something you might run in e.g. bash to restart the market maker.

Have you modified the market maker code? Is the error reproducible?

karanratti commented 6 years ago

okay. Is there a way to automatically restart market maker, in the event of such an error. The only changes I have made are to the values of "order pair", "order start size", "Interval", "Min Spread" and "Relist Interval". I have also enabled "Post only" and set very conservative position limits. The error is sporadic. Looks like when the market moves aggressively in one direction, the bot stops. So in that sense I'm not sure if it is reproducible on demand.

Unfortunately, this leaves me with an unwanted open position (either long or short). Had the bot been running, it would've closed my position, albeit at a small loss. If I am not around to restart it immediately, I usually end up with a heavy loss.

ryanfox commented 6 years ago

If you want to automatically restart the market maker, you might run it via while true; marketmaker; done instead of marketmaker.

This library is intended as a reference for programmers. Some familiarity with Python is required to utilize it. Also keep in mind the README:

BitMEX is not responsible for any losses incurred when using this code. This code is intended for sample purposes ONLY - do not use this code for real trades unless you fully understand what it does and what its caveats are. This is not a sophisticated market making program. It is intended to show the basics of market making while abstracting some of the rote work of interacting with the BitMEX API. It does not make smart decisions and will likely lose money.

andy1245 commented 6 years ago

If you want to automatically restart the market maker, you might run it via while true; marketmaker; done instead of marketmaker.

I'm running the bot on Windows and I start it by opening Windows cmd and typing "marketmaker". However when I try to run it with "while true; marketmaker; done" it doesn't start. Sorry I'm a complete noob in python. Anyone can help me?

ryanfox commented 6 years ago

Windows CMD doesn't support this syntax: while true; marketmaker; done.

Also keep in mind the previous message:

This library is intended as a reference for programmers. Some familiarity with Python is required to utilize it. Also keep in mind the README:

BitMEX is not responsible for any losses incurred when using this code. This code is intended for sample purposes ONLY - do not use this code for real trades unless you fully understand what it does and what its caveats are. This is not a sophisticated market making program. It is intended to show the basics of market making while abstracting some of the rote work of interacting with the BitMEX API. It does not make smart decisions and will likely lose money.