Code Monkey home page Code Monkey logo

market-maker's Introduction

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 BitMEX 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 BitMEX.buy(), BitMEX.sell(), BitMEX.open_orders() 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.

Getting Started

  1. Create a Testnet BitMEX Account and deposit some TBTC.
  2. Get dependencies: python setup.py install
  • This will create a settings.py file at the root. Modify this file to tune parameters.
  1. 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 DRY_RUN=True to test cost and spread.
  1. Run it: ./marketmaker [symbol]
  2. Satisfied with your bot's performance? Create a live API Key for your BitMEX account, set the BASE_URL and start trading!

Operation Overview

This market maker works on the following principles:

  • The MM tracks the last bidPrice and askPrice of the quoted instrument to determine where to start quoting.
  • Based on parameters set the user, the bot creates a descriptions of orders it would like to place.
    • If settings.MAINTAIN_SPREADS is set, the bot will start inside the current spread and work outwards.
    • Otherwise, spread is determined by interval calculations.
  • If the user specified 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:

2016-01-28 17:29:31,054 - INFO - market_maker - BitMEX Market Maker Version: 1.0
2016-01-28 17:29:31,074 - INFO - ws_thread - Connecting to wss://testnet.bitmex.com/realtime?subscribe=quote:XBT7D,trade:XBT7D,instrument,order:XBT7D,execution:XBT7D,margin,position
2016-01-28 17:29:31,074 - INFO - ws_thread - Authenticating with API Key.
2016-01-28 17:29:31,075 - INFO - ws_thread - Started thread
2016-01-28 17:29:32,079 - INFO - ws_thread - Connected to WS. Waiting for data images, this may take a moment...
2016-01-28 17:29:32,079 - INFO - ws_thread - Got all market data. Starting.
2016-01-28 17:29:32,079 - INFO - market_maker - Using symbol XBT7D.
2016-01-28 17:29:32,079 - INFO - market_maker - Order Manager initializing, connecting to BitMEX. Live run: executing real trades.
2016-01-28 17:29:32,079 - INFO - market_maker - Resetting current position. Cancelling all existing orders.
2016-01-28 17:29:33,460 - INFO - market_maker - XBT7D Ticker: Buy: 388.61, Sell: 389.89
2016-01-28 17:29:33,461 - INFO - market_maker - Start Positions: Buy: 388.62, Sell: 389.88, Mid: 389.25
2016-01-28 17:29:33,461 - INFO - market_maker - Current XBT Balance: 3.443498
2016-01-28 17:29:33,461 - INFO - market_maker - Current Contract Position: -1
2016-01-28 17:29:33,461 - INFO - market_maker - Avg Cost Price: 389.75
2016-01-28 17:29:33,461 - INFO - market_maker - Avg Entry Price: 389.75
2016-01-28 17:29:33,462 - INFO - market_maker - Contracts Traded This Run: 0
2016-01-28 17:29:33,462 - INFO - market_maker - Total Contract Delta: -17.7510 XBT
2016-01-28 17:29:33,462 - INFO - market_maker - Creating 4 orders:
2016-01-28 17:29:33,462 - INFO - market_maker - Sell 100 @ 389.88
2016-01-28 17:29:33,462 - INFO - market_maker - Sell 200 @ 390.27
2016-01-28 17:29:33,463 - INFO - market_maker -  Buy 100 @ 388.62
2016-01-28 17:29:33,463 - INFO - market_maker -  Buy 200 @ 388.23
-----
2016-01-28 17:29:37,366 - INFO - ws_thread - Execution: Sell 1 Contracts of XBT7D at 389.88
2016-01-28 17:29:38,943 - INFO - market_maker - XBT7D Ticker: Buy: 388.62, Sell: 389.88
2016-01-28 17:29:38,943 - INFO - market_maker - Start Positions: Buy: 388.62, Sell: 389.88, Mid: 389.25
2016-01-28 17:29:38,944 - INFO - market_maker - Current XBT Balance: 3.443496
2016-01-28 17:29:38,944 - INFO - market_maker - Current Contract Position: -2
2016-01-28 17:29:38,944 - INFO - market_maker - Avg Cost Price: 389.75
2016-01-28 17:29:38,944 - INFO - market_maker - Avg Entry Price: 389.75
2016-01-28 17:29:38,944 - INFO - market_maker - Contracts Traded This Run: -1
2016-01-28 17:29:38,944 - INFO - market_maker - Total Contract Delta: -17.7510 XBT
2016-01-28 17:29:38,945 - INFO - market_maker - Amending Sell: 99 @ 389.88 to 100 @ 389.88 (+0.00)

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.5 requests, rounded up, per order. For example, placing 9 orders consumes 5 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.

Compatibility

This module is compatible with both Python 2 and 3 using Python's future module.

Some helpful tips on Py2/3 compatibility: http://python-future.org/compatible_idioms.html

market-maker's People

Contributors

bendelo avatar ericscheier avatar joequant avatar strml avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

market-maker's Issues

error on start

authenticates and gets market data fine, then when it gets to resetting current position, it throws the following:

`python marketmaker
2019-08-16 23:26:22,494 - INFO - market_maker - BitMEX Market Maker Version: 1.0

2019-08-16 23:26:22,495 - INFO - ws_thread - Connecting to wss://www.bitmex.com/realtime?subscribe=quote:XBTUSD,trade:XBTUSD,orderBookL2_25:XBTUSD,instrument,order:XBTUSD,execution:XBTUSD,margin,position
2019-08-16 23:26:22,495 - INFO - ws_thread - Authenticating with API Key.
2019-08-16 23:26:22,495 - INFO - ws_thread - Started thread
2019-08-16 23:26:23,497 - INFO - ws_thread - Connected to WS. Waiting for data images, this may take a moment...
2019-08-16 23:26:25,447 - INFO - ws_thread - Got all market data. Starting.
2019-08-16 23:26:25,448 - INFO - market_maker - Using symbol XBTUSD.
2019-08-16 23:26:25,448 - INFO - market_maker - Order Manager initializing, connecting to BitMEX. Live run: executing real trades.
2019-08-16 23:26:25,448 - INFO - market_maker - Resetting current position. Cancelling all existing orders.
Traceback (most recent call last):
File "marketmaker", line 3, in
market_maker.run()
File "/home/toor/market-maker/market_maker/market_maker.py", line 589, in run
om.init()
File "/home/toor/market-maker/market_maker/market_maker.py", line 219, in init
self.reset()
File "/home/toor/market-maker/market_maker/market_maker.py", line 223, in reset
self.sanity_check()
File "/home/toor/market-maker/market_maker/market_maker.py", line 498, in sanity_check
ticker = self.get_ticker()
File "/home/toor/market-maker/market_maker/market_maker.py", line 252, in get_ticker
order_book = sorted(self.exchange.market_depth(), key=itemgetter('level'))
KeyError: 'level'
2019-08-16 23:26:27,225 - INFO - market_maker - Shutting down. All open orders will be cancelled.
2019-08-16 23:26:27,225 - INFO - market_maker - Resetting current position. Cancelling all existing orders.
close status: 31522
2019-08-16 23:26:28,430 - INFO - ws_thread - Websocket Closed
`

Any thoughts? @ericscheier @Behappy123

Place orders only under appropriate liquidity conditions

Quoting from @Behappy123 to start the issue:

Adjust order paramaters to only place orders where there is at least 5000 contracts+ directly behind us in the orderbook(can say it requires at least 5000 contracts at the price that 1 cent behind where we are placing our order, and we place our order 1c ahead of them). The purpose of this is to prevent placing a bid/ask ahead of a miniscule order (which some very small traders trade with order sizes of sometimes 5 contracts, or 20 contracts, etc)

No handlers could be found for logger "websocket"

2018-04-05 18:56:10,407 - INFO - ws_thread - Connecting to wss://testnet.bitmex.com/realtime?subscribe=quote:XBTUSD,trade:XBTUSD,orderBook25:XBTUSD,instrument,order:XBTUSD,execution:XBTUSD,margin,position
2018-04-05 18:56:10,407 - INFO - ws_thread - Authenticating with API Key.
2018-04-05 18:56:10,408 - INFO - ws_thread - Started thread
2018-04-05 18:56:13,503 - INFO - ws_thread - Connected to WS. Waiting for data images, this may take a moment...
2018-04-05 18:56:13,505 - ERROR - ws_thread - Unknown table: orderBook25
No handlers could be found for logger "websocket"

Feedback loop bug

Imported from Trello ( @Behappy123 ):

The bot keeps ticking down 1 cents at a time, even when we are the dominant bid/ask. It does this every LOOP_INTERVAL, doesn't seem to make a difference if its 3 seconds, 5 seconds, or 15 seconds.

Scaling back orders in response to order book changes

Adjusting bot so that orders automatically scale back if the next order behind ours is scaled back (so we are only the best bid/ask by 1 cent (the bot is currently not updating the orders, and thus sometimes we are beating the next order by $1 or more when the order we placed ourselves ahead of is cancelled by its owner)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.