Does anybody use a BitMEX trading bot

BitMEX Market Maker

This is a sample market making bot for use with BitMEX.

It is free to use and modify for your own strategies. It provides the following:

  • A object wrapping the REST and WebSocket APIs.
    • All data is realtime and efficiently fetched via the WebSocket. This is the fastest way to get market data.
    • Orders may be created, queried, and cancelled via , , and the like.
    • Withdrawals may be requested (but they still must be confirmed via email and 2FA).
    • Connection errors and WebSocket reconnection is handled for you.
    • Permanent API Key support is included.
  • A scaffolding for building your own trading strategies.
    • Out of the box, a simple market making strategy is implemented that blankets the bid and ask.
    • More complicated strategies are up to the user. Try incorporating index data, query other markets to catch moves early, or develop your own completely custom strategy.

Develop on Testnet first! Testnet trading is completely free and is identical to the live market.

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.

Getting Started

  1. Create a Testnet BitMEX Account and deposit some TBTC.
  2. Install: . It is strongly recommeded to use a virtualenv.
  3. Create a marketmaker project: run
    • This will create and in the working directory.
    • Modify to tune parameters.
  4. Edit settings.py to add your BitMEX API Key and Secret and change bot parameters.
    • Note that user/password authentication is not supported.
    • Run with to test cost and spread.
  5. Run it:
  6. Satisfied with your bot's performance? Create a live API Key for your BitMEX account, set the and start trading!

Operation Overview

This market maker works on the following principles:

  • The market maker tracks the last and of the quoted instrument to determine where to start quoting.
  • Based on parameters set by the user, the bot creates a descriptions of orders it would like to place.
    • If is set, the bot will start inside the current spread and work outwards.
    • Otherwise, spread is determined by interval calculations.
  • If the user specifies position limits, these are checked. If the current position is beyond a limit, the bot stops quoting that side of the market.
  • These order descriptors are compared with what the bot has currently placed in the market.
    • If an existing order can be amended to the desired value, it is amended.
    • Otherwise, a new order is created.
    • Extra orders are canceled.
  • The bot then prints details of contracts traded, tickers, and total delta.

Simplified Output

The following is some of what you can expect when running this bot:

Advanced usage

You can implement custom trading strategies using the market maker. controls placing, updating, and monitoring orders on BitMEX. To implement your own custom strategy, subclass and override :

Your strategy should provide a set of orders. An order is a dict containing price, quantity, and whether the order is buy or sell. For example:

Call to submit your orders. will create, amend, and delete orders on BitMEX as necessary to match what you pass in:

To run your strategy, call :

Your custom strategy will run until you terminate the program with CTRL-C. There is an example in .

Notes on Rate Limiting

By default, the BitMEX API rate limit is 300 requests per 5 minute interval (avg 1/second).

This bot uses the WebSocket and bulk order placement/amend to greatly reduce the number of calls sent to the BitMEX API.

Most calls to the API consume one request, except:

  • Bulk order placement/amend: Consumes 0.1 requests, rounded up, per order. For example, placing 16 orders consumes 2 requests.
  • Bulk order cancel: Consumes 1 request no matter the size. Is not blocked by an exceeded ratelimit; cancels will always succeed. This bot will always cancel all orders on an error or interrupt.

If you are quoting multiple contracts and your ratelimit is becoming an obstacle, please email support with details of your quoting. In the vast majority of cases, we are able to raise a user's ratelimit without issue.

Troubleshooting

Common errors we've seen:

    • This is caused by an outdated version of . Run to update.

Compatibility

This module supports Python 3.5 and later.

See also

BitMEX has a Python REST client and websocket client.