I am currently programming a script to use on the Bitmex API. In algo trading a lot can happen to cause an error in the script, so my question to you all was if I missed a safety and what kind of safeties y'all are using?

This is the list of safeties and optimizations that I am using:

  1. Use mark price for stoploss when calculating profit.
  2. Complex calculations out of the main loop.
  3. Use bucketed orders.
  4. Order handling is priority over getting data (stoplosses first, then take profit order, then entry order, then price data).
  5. Bitmex offline server safety, make sure there are no open trades when the server is offline.
  6. Use Websocket API.
    1. If request failed, retry request 1/2x (if there is no overload error) otherwise shut down.

    2. If request failed for entry order with overload error, if the running price is x% close to the entry price dont execute the order

    3. If request failed for stoploss / exit order with overload error, keep trying till it goes through (check if its possible to execute stoploss if running price already passed the appointed stoploss price)

  7. If base order filled and straight below stoploss price, stoploss placement will get an error, if below cancel stoploss placement and exit at market.
  8. 2th check from BitMEX side, check if base order has stoploss and takeprofit order. I already check on my side if after I submitted an order if BitMEX recieved it.
  9. Max 10 stop orders (BitMEX limit).
  10. Use 1 order quantity for the whole trade (same amount for entering an exiting a trade).

I was wondering if I missed some? I am also curious what kind of safeties and optimizations y'all are using and willing to share? Thanks in advance!


1 Answer 1


The biggest risk as I've seen is from falling to loops. Make sure to check the quantities of orders, cancels, replaces. Have hard-breaks on any exceptions to those quantities.

For your risk can you have a separate process that listens to fills? It's good practice to have a separate risk engine that can kill the trader if limits are breached.

  • $\begingroup$ We are checking internal if an order has been placed on Bitmex after we have send the request. And we are checking all orders on Bitmex if they exist in our internal system. Is this enough or do we actually need a separate program that checks this? $\endgroup$
    – Bob hhhuh
    Oct 15, 2020 at 18:02
  • 1
    $\begingroup$ From my experience yes. You can have logic issues in your main program that can become very expensive. A separate monitor can just kill your trader when things go bad. $\endgroup$
    – JoshK
    Oct 15, 2020 at 18:06
  • $\begingroup$ Also, BitMex has derrivs that require calculation. You definitely need to double check your greeks. $\endgroup$
    – JoshK
    Oct 15, 2020 at 18:07
  • $\begingroup$ Thanks for your input! $\endgroup$
    – Bob hhhuh
    Oct 15, 2020 at 18:17
  • 1
    $\begingroup$ Falling into a loop: Your intention for your program: buy 1 share of XYZ. What actually happens: your program keeps buying 1 share over and over and over $\endgroup$
    – JoshK
    Oct 16, 2020 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.