├── __init__.py ├── market_maker ├── ws │ ├── __init__.py │ └── ws_thread.py ├── utils │ ├── __init__.py │ ├── errors.py │ ├── dotdict.py │ ├── constants.py │ ├── math.py │ └── log.py ├── auth │ ├── __init__.py │ ├── AccessTokenAuth.py │ ├── APIKeyAuthWithExpires.py │ └── APIKeyAuth.py ├── settings.py ├── __init__.py ├── custom_strategy_wp.py ├── custom_strategy.py ├── _settings_base.py ├── bitmex.py └── market_maker.py ├── setup.cfg ├── .gitignore ├── marketmaker ├── requirements.txt ├── Makefile ├── setup.py ├── test ├── websocket-multiplexing-test.py └── websocket-apikey-auth-test.py ├── README.md └── LICENSE /__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /market_maker/ws/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /market_maker/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /settings.py 2 | *.pyc 3 | dist/ 4 | *.egg-info/ 5 | env 6 | .idea/ 7 | -------------------------------------------------------------------------------- /marketmaker: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from market_maker import custom_strategy 4 | custom_strategy.run() 5 | 6 | -------------------------------------------------------------------------------- /market_maker/auth/__init__.py: -------------------------------------------------------------------------------- 1 | from market_maker.auth.AccessTokenAuth import * 2 | from market_maker.auth.APIKeyAuth import * 3 | from market_maker.auth.APIKeyAuthWithExpires import * 4 | -------------------------------------------------------------------------------- /market_maker/utils/errors.py: -------------------------------------------------------------------------------- 1 | class AuthenticationError(Exception): 2 | pass 3 | 4 | class MarketClosedError(Exception): 5 | pass 6 | 7 | class MarketEmptyError(Exception): 8 | pass 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | appdirs==1.4.3 2 | backports.ssl-match-hostname==3.5.0.1 3 | future==0.16.0 4 | packaging==16.8 5 | pyparsing==2.2.0 6 | requests==2.13.0 7 | six==1.10.0 8 | websocket-client==0.56.0 9 | -------------------------------------------------------------------------------- /market_maker/utils/dotdict.py: -------------------------------------------------------------------------------- 1 | class dotdict(dict): 2 | """dot.notation access to dictionary attributes""" 3 | def __getattr__(self, attr): 4 | return self.get(attr) 5 | __setattr__ = dict.__setitem__ 6 | __delattr__ = dict.__delitem__ 7 | -------------------------------------------------------------------------------- /market_maker/utils/constants.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | # Constants 3 | XBt_TO_XBT = 100000000 4 | VERSION = 'v1.1' 5 | try: 6 | VERSION = str(subprocess.check_output(["git", "describe", "--tags"], stderr=subprocess.DEVNULL).rstrip()) 7 | except Exception as e: 8 | # git not available, ignore 9 | pass 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean dev publish 2 | DIST := dist 3 | 4 | clean: 5 | rm -rf $(DIST) 6 | 7 | build: clean 8 | python setup.py sdist 9 | 10 | dev: 11 | python setup.py develop 12 | 13 | publish: clean build 14 | python -m twine upload $(DIST)/* 15 | 16 | publish-test: clean build 17 | python -m twine upload -r pypitest $(DIST)/* 18 | -------------------------------------------------------------------------------- /market_maker/utils/math.py: -------------------------------------------------------------------------------- 1 | from decimal import Decimal 2 | 3 | def toNearest(num, tickSize): 4 | """Given a number, round it to the nearest tick. Very useful for sussing float error 5 | out of numbers: e.g. toNearest(401.46, 0.01) -> 401.46, whereas processing is 6 | normally with floats would give you 401.46000000000004. 7 | Use this after adding/subtracting/multiplying numbers.""" 8 | tickDec = Decimal(str(tickSize)) 9 | return float((Decimal(round(num / tickSize, 0)) * tickDec)) 10 | -------------------------------------------------------------------------------- /market_maker/auth/AccessTokenAuth.py: -------------------------------------------------------------------------------- 1 | from requests.auth import AuthBase 2 | 3 | 4 | class AccessTokenAuth(AuthBase): 5 | 6 | """Attaches Access Token Authentication to the given Request object.""" 7 | 8 | def __init__(self, accessToken): 9 | """Init with Token.""" 10 | self.token = accessToken 11 | 12 | def __call__(self, r): 13 | """Called when forming a request - generates access token header.""" 14 | if (self.token): 15 | r.headers['access-token'] = self.token 16 | return r 17 | -------------------------------------------------------------------------------- /market_maker/utils/log.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from market_maker.settings import settings 3 | 4 | loggers = {} 5 | 6 | 7 | def setup_custom_logger(name, log_level=settings.LOG_LEVEL): 8 | if loggers.get(name): 9 | return loggers[name] 10 | 11 | logger = logging.getLogger(name) 12 | loggers[name] = logger 13 | 14 | formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s') 15 | handler = logging.StreamHandler() 16 | handler.setFormatter(formatter) 17 | logger.setLevel(log_level) 18 | logger.addHandler(handler) 19 | return logger 20 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from setuptools import setup 3 | from os.path import dirname, join 4 | 5 | import market_maker 6 | 7 | 8 | here = dirname(__file__) 9 | 10 | 11 | setup(name='bitmex-market-maker', 12 | version=market_maker.__version__, 13 | description='Market making bot for BitMEX API', 14 | url='https://github.com/BitMEX/sample-market-maker', 15 | long_description=open(join(here, 'README.md')).read(), 16 | long_description_content_type='text/markdown', 17 | author='Samuel Reed', 18 | author_email='sam@bitmex.com', 19 | install_requires=[ 20 | 'requests', 21 | 'websocket-client', 22 | 'future' 23 | ], 24 | packages=['market_maker', 'market_maker.auth', 'market_maker.utils', 'market_maker.ws'], 25 | entry_points={ 26 | 'console_scripts': ['marketmaker = market_maker:run'] 27 | } 28 | ) 29 | -------------------------------------------------------------------------------- /market_maker/auth/APIKeyAuthWithExpires.py: -------------------------------------------------------------------------------- 1 | from requests.auth import AuthBase 2 | import time 3 | from market_maker.auth.APIKeyAuth import generate_signature 4 | 5 | 6 | class APIKeyAuthWithExpires(AuthBase): 7 | 8 | """Attaches API Key Authentication to the given Request object. This implementation uses `expires`.""" 9 | 10 | def __init__(self, apiKey, apiSecret): 11 | """Init with Key & Secret.""" 12 | self.apiKey = apiKey 13 | self.apiSecret = apiSecret 14 | 15 | def __call__(self, r): 16 | """ 17 | Called when forming a request - generates api key headers. This call uses `expires` instead of nonce. 18 | 19 | This way it will not collide with other processes using the same API Key if requests arrive out of order. 20 | For more details, see https://www.bitmex.com/app/apiKeys 21 | """ 22 | # modify and return the request 23 | expires = int(round(time.time()) + 5) # 5s grace period in case of clock skew 24 | r.headers['api-expires'] = str(expires) 25 | r.headers['api-key'] = self.apiKey 26 | r.headers['api-signature'] = generate_signature(self.apiSecret, r.method, r.url, expires, r.body or '') 27 | 28 | return r 29 | -------------------------------------------------------------------------------- /market_maker/settings.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | import importlib 4 | import os 5 | import sys 6 | 7 | from market_maker.utils.dotdict import dotdict 8 | import market_maker._settings_base as baseSettings 9 | 10 | 11 | def import_path(fullpath): 12 | """ 13 | Import a file with full path specification. Allows one to 14 | import from anywhere, something __import__ does not do. 15 | """ 16 | path, filename = os.path.split(fullpath) 17 | filename, ext = os.path.splitext(filename) 18 | sys.path.insert(0, path) 19 | module = importlib.import_module(filename, path) 20 | importlib.reload(module) # Might be out of date 21 | del sys.path[0] 22 | return module 23 | 24 | 25 | userSettings = import_path(os.path.join('.', 'settings')) 26 | symbolSettings = None 27 | symbol = sys.argv[1] if len(sys.argv) > 1 else None 28 | if symbol: 29 | print("Importing symbol settings for %s..." % symbol) 30 | try: 31 | symbolSettings = import_path(os.path.join('..', 'settings-%s' % symbol)) 32 | except Exception as e: 33 | print("Unable to find settings-%s.py." % symbol) 34 | 35 | # Assemble settings. 36 | settings = {} 37 | settings.update(vars(baseSettings)) 38 | settings.update(vars(userSettings)) 39 | if symbolSettings: 40 | settings.update(vars(symbolSettings)) 41 | 42 | # Main export 43 | settings = dotdict(settings) 44 | -------------------------------------------------------------------------------- /market_maker/__init__.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import os 3 | 4 | import shutil 5 | 6 | 7 | __version__ = 'v1.5.1' 8 | 9 | 10 | def run(): 11 | parser = argparse.ArgumentParser(description='sample BitMEX market maker') 12 | parser.add_argument('command', nargs='?', help='Instrument symbol on BitMEX or "setup" for first-time config') 13 | args = parser.parse_args() 14 | 15 | if args.command is not None and args.command.strip().lower() == 'setup': 16 | copy_files() 17 | 18 | else: 19 | # import market_maker here rather than at the top because it depends on settings.py existing 20 | try: 21 | from market_maker import market_maker 22 | market_maker.run() 23 | except ImportError: 24 | print('Can\'t find settings.py. Run "marketmaker setup" to create project.') 25 | 26 | 27 | def copy_files(): 28 | package_base = os.path.dirname(__file__) 29 | 30 | if not os.path.isfile(os.path.join(os.getcwd(), 'settings.py')): 31 | shutil.copyfile(os.path.join(package_base, '_settings_base.py'), 'settings.py') 32 | 33 | try: 34 | shutil.copytree(package_base, os.path.join(os.getcwd(), 'market_maker')) 35 | print('Created marketmaker project.\n**** \nImportant!!!\nEdit settings.py before starting the bot.\n****') 36 | except FileExistsError: 37 | print('Market Maker project already exists!') 38 | -------------------------------------------------------------------------------- /market_maker/custom_strategy_wp.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import pandas as pd 4 | 5 | from market_maker.market_maker import OrderManager 6 | 7 | 8 | class CustomOrderManager(OrderManager): 9 | """A sample order manager for implementing your own custom strategy""" 10 | 11 | def place_orders(self) -> None: 12 | order_qty = 100 13 | threshold = 0.10407176816108477 14 | 15 | ticker = self.get_ticker() 16 | market_depth = self.exchange.get_market_depth() 17 | bid = map(lambda x: (x['price'], x['size']), sorted(filter(lambda x: x['side'] == 'Buy', market_depth), key=lambda x: -x['price'])) 18 | ask = map(lambda x: (x['price'], x['size']), sorted(filter(lambda x: x['side'] == 'Sell', market_depth), key=lambda x: x['price'])) 19 | bid = pd.DataFrame(bid, columns=['price', 'size']) 20 | bid = bid[bid['size'].cumsum() <= 130000000] 21 | ask = pd.DataFrame(ask, columns=['price', 'size']) 22 | ask = ask[ask['size'].cumsum() <= 130000000] 23 | weighted_price = ((bid['price'] * bid['size']).sum() + (ask['price'] * ask['size']).sum()) / (bid['size'].sum() + ask['size'].sum()) 24 | alpha = weighted_price - ticker['last'] 25 | 26 | buy_orders = [] 27 | sell_orders = [] 28 | 29 | if alpha > threshold and not self.long_position_limit_exceeded(): 30 | new_bid_price = ticker['last'] - self.instrument['tickSize'] 31 | buy_orders.append({'price': new_bid_price, 'orderQty': order_qty, 'side': "Buy"}) 32 | if alpha < -threshold and not self.short_position_limit_exceeded(): 33 | new_ask_price = ticker['last'] + self.instrument['tickSize'] 34 | sell_orders.append({'price': new_ask_price, 'orderQty': order_qty, 'side': "Sell"}) 35 | 36 | self.converge_orders(buy_orders, sell_orders) 37 | 38 | 39 | def run() -> None: 40 | order_manager = CustomOrderManager() 41 | 42 | # Try/except just keeps ctrl-c from printing an ugly stacktrace 43 | try: 44 | order_manager.run_loop() 45 | except (KeyboardInterrupt, SystemExit): 46 | sys.exit() 47 | -------------------------------------------------------------------------------- /market_maker/custom_strategy.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | from market_maker.settings import settings 4 | from market_maker.market_maker import OrderManager 5 | from market_maker.utils import math 6 | 7 | 8 | class CustomOrderManager(OrderManager): 9 | """A sample order manager for implementing your own custom strategy""" 10 | 11 | def place_orders(self) -> None: 12 | order_qty = 100 13 | a = 707.477466 14 | b = 19.8732468 15 | half_spread = 0.0108980949 16 | depth = 0.05 17 | 18 | ticker = self.get_ticker() 19 | market_depth = self.exchange.get_market_depth() 20 | buy = sum(map(lambda x: x['size'], filter(lambda x: x['side'] == 'Buy' and x['price'] > ticker['mid'] * (1 - depth), market_depth))) 21 | sell = sum(map(lambda x: x['size'], filter(lambda x: x['side'] == 'Sell' and x['price'] < ticker['mid'] * (1 + depth), market_depth))) 22 | excessive_buy = (buy - sell) / 100000000 23 | 24 | skew = self.running_qty / settings.MAX_POSITION * b * -1 25 | quote_mid_price = ticker['last'] + a * excessive_buy + skew 26 | new_bid_price = min(quote_mid_price * (1 - half_spread), ticker['last'] - self.instrument['tickSize']) 27 | new_ask_price = max(quote_mid_price * (1 + half_spread), ticker['last'] + self.instrument['tickSize']) 28 | 29 | new_bid_price = math.toNearest(new_bid_price, self.instrument['tickSize']) 30 | new_ask_price = math.toNearest(new_ask_price, self.instrument['tickSize']) 31 | 32 | buy_orders = [] 33 | sell_orders = [] 34 | 35 | if not self.long_position_limit_exceeded(): 36 | buy_orders.append({'price': new_bid_price, 'orderQty': order_qty, 'side': "Buy"}) 37 | if not self.short_position_limit_exceeded(): 38 | sell_orders.append({'price': new_ask_price, 'orderQty': order_qty, 'side': "Sell"}) 39 | 40 | self.converge_orders(buy_orders, sell_orders) 41 | 42 | 43 | def run() -> None: 44 | order_manager = CustomOrderManager() 45 | 46 | # Try/except just keeps ctrl-c from printing an ugly stacktrace 47 | try: 48 | order_manager.run_loop() 49 | except (KeyboardInterrupt, SystemExit): 50 | sys.exit() 51 | -------------------------------------------------------------------------------- /market_maker/auth/APIKeyAuth.py: -------------------------------------------------------------------------------- 1 | from requests.auth import AuthBase 2 | import time 3 | import hashlib 4 | import hmac 5 | from future.builtins import bytes 6 | from future.standard_library import hooks 7 | with hooks(): # Python 2/3 compat 8 | from urllib.parse import urlparse 9 | 10 | 11 | class APIKeyAuth(AuthBase): 12 | 13 | """Attaches API Key Authentication to the given Request object.""" 14 | 15 | def __init__(self, apiKey, apiSecret): 16 | """Init with Key & Secret.""" 17 | self.apiKey = apiKey 18 | self.apiSecret = apiSecret 19 | 20 | def __call__(self, r): 21 | """Called when forming a request - generates api key headers.""" 22 | # modify and return the request 23 | nonce = generate_expires() 24 | r.headers['api-expires'] = str(nonce) 25 | r.headers['api-key'] = self.apiKey 26 | r.headers['api-signature'] = generate_signature(self.apiSecret, r.method, r.url, nonce, r.body or '') 27 | 28 | return r 29 | 30 | 31 | def generate_expires(): 32 | return int(time.time() + 3600) 33 | 34 | 35 | # Generates an API signature. 36 | # A signature is HMAC_SHA256(secret, verb + path + nonce + data), hex encoded. 37 | # Verb must be uppercased, url is relative, nonce must be an increasing 64-bit integer 38 | # and the data, if present, must be JSON without whitespace between keys. 39 | # 40 | # For example, in psuedocode (and in real code below): 41 | # 42 | # verb=POST 43 | # url=/api/v1/order 44 | # nonce=1416993995705 45 | # data={"symbol":"XBTZ14","quantity":1,"price":395.01} 46 | # signature = HEX(HMAC_SHA256(secret, 'POST/api/v1/order1416993995705{"symbol":"XBTZ14","quantity":1,"price":395.01}')) 47 | def generate_signature(secret, verb, url, nonce, data): 48 | """Generate a request signature compatible with BitMEX.""" 49 | # Parse the url so we can remove the base and extract just the path. 50 | parsedURL = urlparse(url) 51 | path = parsedURL.path 52 | if parsedURL.query: 53 | path = path + '?' + parsedURL.query 54 | 55 | if isinstance(data, (bytes, bytearray)): 56 | data = data.decode('utf8') 57 | 58 | # print "Computing HMAC: %s" % verb + path + str(nonce) + data 59 | message = verb + path + str(nonce) + data 60 | 61 | signature = hmac.new(bytes(secret, 'utf8'), bytes(message, 'utf8'), digestmod=hashlib.sha256).hexdigest() 62 | return signature 63 | -------------------------------------------------------------------------------- /test/websocket-multiplexing-test.py: -------------------------------------------------------------------------------- 1 | import codecs 2 | import hashlib 3 | import hmac 4 | import json 5 | import time 6 | import urllib 7 | import uuid 8 | 9 | from websocket import create_connection 10 | 11 | ### 12 | # websocket-apikey-auth-test.py 13 | # 14 | # Reference Python implementation for multiplexing realtime data for multiple 15 | # users through a single websocket connection. 16 | ### 17 | 18 | # Replace these with your keys. 19 | KEYS = { 20 | "CfwQ4SZ6gM_t6dIy1bCLJylX": "f9XOPLacPCZJ1dvPzN8B6Et7nMEaPGeomMSHk8Cr2zD4NfCY" 21 | } 22 | 23 | # Switch these comments to use testnet instead. 24 | BITMEX_URL = "ws://localhost:3000" 25 | # BITMEX_URL="wss://testnet.bitmex.com" 26 | # BITMEX_URL="wss://www.bitmex.com" 27 | 28 | VERB = "GET" 29 | AUTH_ENDPOINT = "/realtime" # for the purpose of the API Key check, we're still using /realtime 30 | ENDPOINT = "/realtimemd?transport=websocket&b64=1" 31 | 32 | 33 | def main(): 34 | """Authenticate with the BitMEX API & request account information.""" 35 | test_with_message() 36 | 37 | 38 | def test_with_message(): 39 | # Initial connection - BitMEX sends a welcome message. 40 | print("Connecting to " + BITMEX_URL + ENDPOINT) 41 | ws = create_connection(BITMEX_URL + ENDPOINT) 42 | 43 | # Open multiplexed connections. 44 | for key, secret in KEYS.items(): 45 | # This is up to you, most use microtime but you may have your own scheme so long as it's increasing 46 | # and doesn't repeat. 47 | nonce = int(round(time.time() * 1000)) 48 | # See signature generation reference at https://www.bitmex.com/app/apiKeys 49 | signature = bitmex_signature(secret, VERB, AUTH_ENDPOINT, nonce) 50 | 51 | connID = random_id() 52 | 53 | # Open a new multiplexed connection. 54 | # See https://github.com/cayasso/primus-multiplex for more details 55 | # Format is "type", "id", "topic", "payload" 56 | # Types are 0 - Message, 1 - Subscribe, 2 - Unsubscribe 57 | # connID = id() 58 | channelName = "userAuth:" + key + ":" + str(nonce) + ":" + signature 59 | request = [1, connID, channelName] 60 | print(json.dumps(request)) 61 | ws.send(json.dumps(request)) 62 | 63 | request = [0, connID, channelName, {'op': 'authKey', 'args': [key, nonce, signature]}] 64 | print('sending auth request: {}'.format(json.dumps(request))) 65 | ws.send(json.dumps(request)) 66 | print("Sent Auth request") 67 | result = ws.recv() 68 | print('Received {}'.format(result)) 69 | 70 | # Send a request that requires authorization on this multiplexed connection. 71 | op = {"op":"subscribe", "args":"position"} 72 | request = [0, connID, channelName, op] 73 | ws.send(json.dumps(request)) 74 | print("Sent subscribe") 75 | result = ws.recv() 76 | print("Received '%s'" % result) 77 | result = ws.recv() 78 | print("Received '%s'" % result) 79 | 80 | ws.close() 81 | 82 | 83 | # Generates a random ID. 84 | def random_id(): 85 | return codecs.encode(uuid.uuid4().bytes, 'base64').rstrip(b'=\n').decode('utf-8') 86 | 87 | 88 | # Generates an API signature. 89 | # A signature is HMAC_SHA256(secret, verb + path + nonce + data), base64 encoded. 90 | # Verb must be uppercased, url is relative, nonce must be an increasing 64-bit integer 91 | # and the data, if present, must be JSON without whitespace between keys. 92 | def bitmex_signature(apiSecret, verb, url, nonce, postdict=None): 93 | """Given an API Secret and data, create a BitMEX-compatible signature.""" 94 | data = '' 95 | if postdict: 96 | # separators remove spaces from json 97 | # BitMEX expects signatures from JSON built without spaces 98 | data = json.dumps(postdict, separators=(',', ':')) 99 | parsedURL = urllib.parse.urlparse(url) 100 | path = parsedURL.path 101 | if parsedURL.query: 102 | path = path + '?' + parsedURL.query 103 | # print("Computing HMAC: %s" % verb + path + str(nonce) + data) 104 | message = (verb + path + str(nonce) + data).encode('utf-8') 105 | 106 | signature = hmac.new(apiSecret.encode('utf-8'), message, digestmod=hashlib.sha256).hexdigest() 107 | return signature 108 | 109 | 110 | if __name__ == "__main__": 111 | main() 112 | -------------------------------------------------------------------------------- /test/websocket-apikey-auth-test.py: -------------------------------------------------------------------------------- 1 | import hashlib 2 | import hmac 3 | import json 4 | import time 5 | import urllib 6 | 7 | from websocket import create_connection 8 | 9 | ### 10 | # websocket-apikey-auth-test.py 11 | # 12 | # Reference Python implementation for authorizing with websocket. 13 | # See https://www.bitmex.com/app/wsAPI for more details, including a list 14 | # of methods. 15 | ### 16 | 17 | # These are not real keys - replace them with your keys. 18 | API_KEY = "CfwQ4SZ6gM_t6dIy1bCLJylX" 19 | API_SECRET = "f9XOPLacPCZJ1dvPzN8B6Et7nMEaPGeomMSHk8Cr2zD4NfCY" 20 | 21 | # Switch these comments to use testnet instead. 22 | # BITMEX_URL = "wss://testnet.bitmex.com" 23 | BITMEX_URL = "wss://www.bitmex.com" 24 | 25 | VERB = "GET" 26 | ENDPOINT = "/realtime" 27 | 28 | 29 | def main(): 30 | """Authenticate with the BitMEX API & request account information.""" 31 | test_with_message() 32 | test_with_querystring() 33 | 34 | 35 | def test_with_message(): 36 | # This is up to you, most use microtime but you may have your own scheme so long as it's increasing 37 | # and doesn't repeat. 38 | expires = int(time.time()) + 5 39 | # See signature generation reference at https://www.bitmex.com/app/apiKeys 40 | signature = bitmex_signature(API_SECRET, VERB, ENDPOINT, expires) 41 | 42 | # Initial connection - BitMEX sends a welcome message. 43 | ws = create_connection(BITMEX_URL + ENDPOINT) 44 | print("Receiving Welcome Message...") 45 | result = ws.recv() 46 | print("Received '%s'" % result) 47 | 48 | # Send API Key with signed message. 49 | request = {"op": "authKeyExpires", "args": [API_KEY, expires, signature]} 50 | ws.send(json.dumps(request)) 51 | print("Sent Auth request") 52 | result = ws.recv() 53 | print("Received '%s'" % result) 54 | 55 | # Send a request that requires authorization. 56 | request = {"op": "subscribe", "args": "position"} 57 | ws.send(json.dumps(request)) 58 | print("Sent subscribe") 59 | result = ws.recv() 60 | print("Received '%s'" % result) 61 | result = ws.recv() 62 | print("Received '%s'" % result) 63 | 64 | ws.close() 65 | 66 | 67 | def test_with_querystring(): 68 | # This is up to you, most use microtime but you may have your own scheme so long as it's increasing 69 | # and doesn't repeat. 70 | expires = int(time.time()) + 5 71 | # See signature generation reference at https://www.bitmex.com/app/apiKeys 72 | signature = bitmex_signature(API_SECRET, VERB, ENDPOINT, expires) 73 | 74 | # Initial connection - BitMEX sends a welcome message. 75 | ws = create_connection(BITMEX_URL + ENDPOINT + 76 | "?api-expires=%s&api-signature=%s&api-key=%s" % (expires, signature, API_KEY)) 77 | print("Receiving Welcome Message...") 78 | result = ws.recv() 79 | print("Received '%s'" % result) 80 | 81 | # Send a request that requires authorization. 82 | request = {"op": "subscribe", "args": "position"} 83 | ws.send(json.dumps(request)) 84 | print("Sent subscribe") 85 | result = ws.recv() 86 | print("Received '%s'" % result) 87 | result = ws.recv() 88 | print("Received '%s'" % result) 89 | 90 | ws.close() 91 | 92 | 93 | # Generates an API signature. 94 | # A signature is HMAC_SHA256(secret, verb + path + nonce + data), hex encoded. 95 | # Verb must be uppercased, url is relative, nonce must be an increasing 64-bit integer 96 | # and the data, if present, must be JSON without whitespace between keys. 97 | def bitmex_signature(apiSecret, verb, url, nonce, postdict=None): 98 | """Given an API Secret key and data, create a BitMEX-compatible signature.""" 99 | data = '' 100 | if postdict: 101 | # separators remove spaces from json 102 | # BitMEX expects signatures from JSON built without spaces 103 | data = json.dumps(postdict, separators=(',', ':')) 104 | parsedURL = urllib.parse.urlparse(url) 105 | path = parsedURL.path 106 | if parsedURL.query: 107 | path = path + '?' + parsedURL.query 108 | # print("Computing HMAC: %s" % verb + path + str(nonce) + data) 109 | message = (verb + path + str(nonce) + data).encode('utf-8') 110 | print("Signing: %s" % str(message)) 111 | 112 | signature = hmac.new(apiSecret.encode('utf-8'), message, digestmod=hashlib.sha256).hexdigest() 113 | print("Signature: %s" % signature) 114 | return signature 115 | 116 | if __name__ == "__main__": 117 | main() 118 | -------------------------------------------------------------------------------- /market_maker/_settings_base.py: -------------------------------------------------------------------------------- 1 | from os.path import join 2 | import logging 3 | 4 | ######################################################################################################################## 5 | # Connection/Auth 6 | ######################################################################################################################## 7 | 8 | # API URL. 9 | BASE_URL = "https://testnet.bitmex.com/api/v1/" 10 | # BASE_URL = "https://www.bitmex.com/api/v1/" # Once you're ready, uncomment this. 11 | 12 | # The BitMEX API requires permanent API keys. Go to https://testnet.bitmex.com/app/apiKeys to fill these out. 13 | API_KEY = "" 14 | API_SECRET = "" 15 | 16 | 17 | ######################################################################################################################## 18 | # Target 19 | ######################################################################################################################## 20 | 21 | # Instrument to market make on BitMEX. 22 | SYMBOL = "XBTUSD" 23 | 24 | 25 | ######################################################################################################################## 26 | # Order Size & Spread 27 | ######################################################################################################################## 28 | 29 | # How many pairs of buy/sell orders to keep open 30 | ORDER_PAIRS = 1 31 | 32 | # ORDER_START_SIZE will be the number of contracts submitted on level 1 33 | # Number of contracts from level 1 to ORDER_PAIRS - 1 will follow the function 34 | # [ORDER_START_SIZE + ORDER_STEP_SIZE (Level -1)] 35 | ORDER_START_SIZE = 100 36 | ORDER_STEP_SIZE = 100 37 | 38 | # Distance between successive orders, as a percentage (example: 0.005 for 0.5%) 39 | INTERVAL = 0.005 40 | 41 | # Minimum spread to maintain, in percent, between asks & bids 42 | MIN_SPREAD = 0.01 43 | 44 | # If True, market-maker will place orders just inside the existing spread and work the interval % outwards, 45 | # rather than starting in the middle and killing potentially profitable spreads. 46 | MAINTAIN_SPREADS = True 47 | 48 | # This number defines far much the price of an existing order can be from a desired order before it is amended. 49 | # This is useful for avoiding unnecessary calls and maintaining your ratelimits. 50 | # 51 | # Further information: 52 | # Each order is designed to be (INTERVAL*n)% away from the spread. 53 | # If the spread changes and the order has moved outside its bound defined as 54 | # abs((desired_order['price'] / order['price']) - 1) > settings.RELIST_INTERVAL) 55 | # it will be resubmitted. 56 | # 57 | # 0.01 == 1% 58 | RELIST_INTERVAL = 0.0 59 | 60 | 61 | ######################################################################################################################## 62 | # Trading Behavior 63 | ######################################################################################################################## 64 | 65 | # Position limits - set to True to activate. Values are in contracts. 66 | # If you exceed a position limit, the bot will log and stop quoting that side. 67 | CHECK_POSITION_LIMITS = True 68 | MIN_POSITION = -10000 69 | MAX_POSITION = 10000 70 | 71 | # If True, will only send orders that rest in the book (ExecInst: ParticipateDoNotInitiate). 72 | # Use to guarantee a maker rebate. 73 | # However -- orders that would have matched immediately will instead cancel, and you may end up with 74 | # unexpected delta. Be careful. 75 | POST_ONLY = True 76 | 77 | ######################################################################################################################## 78 | # Misc Behavior, Technicals 79 | ######################################################################################################################## 80 | 81 | # If true, don't set up any orders, just say what we would do 82 | # DRY_RUN = True 83 | DRY_RUN = False 84 | 85 | # How often to re-check and replace orders. 86 | # Generally, it's safe to make this short because we're fetching from websockets. But if too many 87 | # order amend/replaces are done, you may hit a ratelimit. If so, email BitMEX if you feel you need a higher limit. 88 | LOOP_INTERVAL = 5 89 | 90 | # Wait times between orders / errors 91 | API_REST_INTERVAL = 1 92 | API_ERROR_INTERVAL = 10 93 | TIMEOUT = 7 94 | 95 | # If we're doing a dry run, use these numbers for BTC balances 96 | DRY_BTC = 50 97 | 98 | # Available levels: logging.(DEBUG|INFO|WARN|ERROR) 99 | LOG_LEVEL = logging.INFO 100 | 101 | # To uniquely identify orders placed by this bot, the bot sends a ClOrdID (Client order ID) that is attached 102 | # to each order so its source can be identified. This keeps the market maker from cancelling orders that are 103 | # manually placed, or orders placed by another bot. 104 | # 105 | # If you are running multiple bots on the same symbol, give them unique ORDERID_PREFIXes - otherwise they will 106 | # cancel each others' orders. 107 | # Max length is 13 characters. 108 | ORDERID_PREFIX = "mm_bitmex_" 109 | 110 | # If any of these files (and this file) changes, reload the bot. 111 | WATCHED_FILES = [join('market_maker', 'market_maker.py'), join('market_maker', 'bitmex.py'), 'settings.py'] 112 | 113 | 114 | ######################################################################################################################## 115 | # BitMEX Portfolio 116 | ######################################################################################################################## 117 | 118 | # Specify the contracts that you hold. These will be used in portfolio calculations. 119 | CONTRACTS = ['XBTUSD'] 120 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BitMEX Market Maker 2 | 3 | ## Implementation 4 | Modified from [BitMEX sample market maker](https://github.com/BitMEX/sample-market-maker) 5 | 6 | Simple Order Book Imbalance [custom_strategy.py](https://github.com/nkaz001/sample-market-maker/blob/master/market_maker/custom_strategy.py) 7 | Weighted Depth Order Book Price [custom_strategy_wp.py](https://github.com/nkaz001/sample-market-maker/blob/master/market_maker/custom_strategy_wp.py) 8 | 9 | [Backtest/Optimization examples](https://github.com/nkaz001/algotrading-example) 10 | # 11 | 12 | This is a sample market making bot for use with [BitMEX](https://www.bitmex.com). 13 | 14 | It is free to use and modify for your own strategies. It provides the following: 15 | 16 | * A `BitMEX` object wrapping the REST and WebSocket APIs. 17 | * All data is realtime and efficiently [fetched via the WebSocket](market_maker/ws/ws_thread.py). This is the fastest way to get market data. 18 | * Orders may be created, queried, and cancelled via `BitMEX.buy()`, `BitMEX.sell()`, `BitMEX.open_orders()` and the like. 19 | * Withdrawals may be requested (but they still must be confirmed via email and 2FA). 20 | * Connection errors and WebSocket reconnection is handled for you. 21 | * [Permanent API Key](https://testnet.bitmex.com/app/apiKeys) support is included. 22 | * [A scaffolding for building your own trading strategies.](#advanced-usage) 23 | * Out of the box, a simple market making strategy is implemented that blankets the bid and ask. 24 | * More complicated strategies are up to the user. Try incorporating [index data](https://testnet.bitmex.com/app/index/.XBT), 25 | query other markets to catch moves early, or develop your own completely custom strategy. 26 | 27 | **Develop on [Testnet](https://testnet.bitmex.com) first!** Testnet trading is completely free and is identical to the live market. 28 | 29 | > BitMEX is not responsible for any losses incurred when using this code. This code is intended for sample purposes ONLY - do not 30 | use this code for real trades unless you fully understand what it does and what its caveats are. 31 | 32 | > This is not a sophisticated market making program. It is intended to show the basics of market making while abstracting some 33 | of the rote work of interacting with the BitMEX API. It does not make smart decisions and will likely lose money. 34 | 35 | ## Getting Started 36 | 37 | 1. Create a [Testnet BitMEX Account](https://testnet.bitmex.com) and [deposit some TBTC](https://testnet.bitmex.com/app/deposit). 38 | 2. Install: `pip install bitmex-market-maker`. It is strongly recommeded to use a virtualenv. 39 | 3. Create a marketmaker project: run `marketmaker setup` 40 | * This will create `settings.py` and `market_maker/` in the working directory. 41 | * Modify `settings.py` to tune parameters. 42 | 4. Edit settings.py to add your [BitMEX API Key and Secret](https://testnet.bitmex.com/app/apiKeys) and change bot parameters. 43 | * Note that user/password authentication is not supported. 44 | * Run with `DRY_RUN=True` to test cost and spread. 45 | 5. Run it: `marketmaker [symbol]` 46 | 6. Satisfied with your bot's performance? Create a [live API Key](https://www.bitmex.com/app/apiKeys) for your 47 | BitMEX account, set the `BASE_URL` and start trading! 48 | 49 | ## Operation Overview 50 | 51 | This market maker works on the following principles: 52 | 53 | * The market maker tracks the last `bidPrice` and `askPrice` of the quoted instrument to determine where to start quoting. 54 | * Based on parameters set by the user, the bot creates a descriptions of orders it would like to place. 55 | - If `settings.MAINTAIN_SPREADS` is set, the bot will start inside the current spread and work outwards. 56 | - Otherwise, spread is determined by interval calculations. 57 | * If the user specifies position limits, these are checked. If the current position is beyond a limit, 58 | the bot stops quoting that side of the market. 59 | * These order descriptors are compared with what the bot has currently placed in the market. 60 | - If an existing order can be amended to the desired value, it is amended. 61 | - Otherwise, a new order is created. 62 | - Extra orders are canceled. 63 | * The bot then prints details of contracts traded, tickers, and total delta. 64 | 65 | ## Simplified Output 66 | 67 | The following is some of what you can expect when running this bot: 68 | 69 | ``` 70 | 2016-01-28 17:29:31,054 - INFO - market_maker - BitMEX Market Maker Version: 1.0 71 | 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 72 | 2016-01-28 17:29:31,074 - INFO - ws_thread - Authenticating with API Key. 73 | 2016-01-28 17:29:31,075 - INFO - ws_thread - Started thread 74 | 2016-01-28 17:29:32,079 - INFO - ws_thread - Connected to WS. Waiting for data images, this may take a moment... 75 | 2016-01-28 17:29:32,079 - INFO - ws_thread - Got all market data. Starting. 76 | 2016-01-28 17:29:32,079 - INFO - market_maker - Using symbol XBT7D. 77 | 2016-01-28 17:29:32,079 - INFO - market_maker - Order Manager initializing, connecting to BitMEX. Live run: executing real trades. 78 | 2016-01-28 17:29:32,079 - INFO - market_maker - Resetting current position. Cancelling all existing orders. 79 | 2016-01-28 17:29:33,460 - INFO - market_maker - XBT7D Ticker: Buy: 388.61, Sell: 389.89 80 | 2016-01-28 17:29:33,461 - INFO - market_maker - Start Positions: Buy: 388.62, Sell: 389.88, Mid: 389.25 81 | 2016-01-28 17:29:33,461 - INFO - market_maker - Current XBT Balance: 3.443498 82 | 2016-01-28 17:29:33,461 - INFO - market_maker - Current Contract Position: -1 83 | 2016-01-28 17:29:33,461 - INFO - market_maker - Avg Cost Price: 389.75 84 | 2016-01-28 17:29:33,461 - INFO - market_maker - Avg Entry Price: 389.75 85 | 2016-01-28 17:29:33,462 - INFO - market_maker - Contracts Traded This Run: 0 86 | 2016-01-28 17:29:33,462 - INFO - market_maker - Total Contract Delta: -17.7510 XBT 87 | 2016-01-28 17:29:33,462 - INFO - market_maker - Creating 4 orders: 88 | 2016-01-28 17:29:33,462 - INFO - market_maker - Sell 100 @ 389.88 89 | 2016-01-28 17:29:33,462 - INFO - market_maker - Sell 200 @ 390.27 90 | 2016-01-28 17:29:33,463 - INFO - market_maker - Buy 100 @ 388.62 91 | 2016-01-28 17:29:33,463 - INFO - market_maker - Buy 200 @ 388.23 92 | ----- 93 | 2016-01-28 17:29:37,366 - INFO - ws_thread - Execution: Sell 1 Contracts of XBT7D at 389.88 94 | 2016-01-28 17:29:38,943 - INFO - market_maker - XBT7D Ticker: Buy: 388.62, Sell: 389.88 95 | 2016-01-28 17:29:38,943 - INFO - market_maker - Start Positions: Buy: 388.62, Sell: 389.88, Mid: 389.25 96 | 2016-01-28 17:29:38,944 - INFO - market_maker - Current XBT Balance: 3.443496 97 | 2016-01-28 17:29:38,944 - INFO - market_maker - Current Contract Position: -2 98 | 2016-01-28 17:29:38,944 - INFO - market_maker - Avg Cost Price: 389.75 99 | 2016-01-28 17:29:38,944 - INFO - market_maker - Avg Entry Price: 389.75 100 | 2016-01-28 17:29:38,944 - INFO - market_maker - Contracts Traded This Run: -1 101 | 2016-01-28 17:29:38,944 - INFO - market_maker - Total Contract Delta: -17.7510 XBT 102 | 2016-01-28 17:29:38,945 - INFO - market_maker - Amending Sell: 99 @ 389.88 to 100 @ 389.88 (+0.00) 103 | 104 | ``` 105 | 106 | ## Advanced usage 107 | 108 | You can implement custom trading strategies using the market maker. `market_maker.OrderManager` 109 | controls placing, updating, and monitoring orders on BitMEX. To implement your own custom 110 | strategy, subclass `market_maker.OrderManager` and override `OrderManager.place_orders()`: 111 | 112 | ``` 113 | from market_maker.market_maker import OrderManager 114 | 115 | class CustomOrderManager(OrderManager): 116 | def place_orders(self) -> None: 117 | # implement your custom strategy here 118 | ``` 119 | 120 | Your strategy should provide a set of orders. An order is a dict containing price, quantity, and 121 | whether the order is buy or sell. For example: 122 | 123 | ``` 124 | buy_order = { 125 | 'price': 1234.5, # float 126 | 'orderQty': 100, # int 127 | 'side': 'Buy' 128 | } 129 | 130 | sell_order = { 131 | 'price': 9876.5, # float 132 | 'orderQty': 100, # int 133 | 'side': 'Sell' 134 | } 135 | ``` 136 | 137 | Call `self.converge_orders()` to submit your orders. `converge_orders()` will create, amend, 138 | and delete orders on BitMEX as necessary to match what you pass in: 139 | 140 | ``` 141 | def place_orders(self) -> None: 142 | buy_orders = [] 143 | sell_orders = [] 144 | 145 | # populate buy and sell orders, e.g. 146 | buy_orders.append({'price': 998.0, 'orderQty': 100, 'side': "Buy"}) 147 | buy_orders.append({'price': 999.0, 'orderQty': 100, 'side': "Buy"}) 148 | sell_orders.append({'price': 1000.0, 'orderQty': 100, 'side': "Sell"}) 149 | sell_orders.append({'price': 1001.0, 'orderQty': 100, 'side': "Sell"}) 150 | 151 | self.converge_orders(buy_orders, sell_orders) 152 | ``` 153 | 154 | To run your strategy, call `run_loop()`: 155 | ``` 156 | order_manager = CustomOrderManager() 157 | order_manager.run_loop() 158 | ``` 159 | 160 | Your custom strategy will run until you terminate the program with CTRL-C. There is an example 161 | in `custom_strategy.py`. 162 | 163 | ## Notes on Rate Limiting 164 | 165 | By default, the BitMEX API rate limit is 300 requests per 5 minute interval (avg 1/second). 166 | 167 | This bot uses the WebSocket and bulk order placement/amend to greatly reduce the number of calls sent to the BitMEX API. 168 | 169 | Most calls to the API consume one request, except: 170 | 171 | * Bulk order placement/amend: Consumes 0.1 requests, rounded up, per order. For example, placing 16 orders consumes 172 | 2 requests. 173 | * Bulk order cancel: Consumes 1 request no matter the size. Is not blocked by an exceeded ratelimit; cancels will 174 | always succeed. This bot will always cancel all orders on an error or interrupt. 175 | 176 | If you are quoting multiple contracts and your ratelimit is becoming an obstacle, please 177 | [email support](mailto:support@bitmex.com) with details of your quoting. In the vast majority of cases, 178 | we are able to raise a user's ratelimit without issue. 179 | 180 | ## Troubleshooting 181 | 182 | Common errors we've seen: 183 | 184 | * `TypeError: __init__() got an unexpected keyword argument 'json'` 185 | * This is caused by an outdated version of `requests`. Run `pip install -U requests` to update. 186 | 187 | 188 | ## Compatibility 189 | 190 | This module supports Python 3.5 and later. 191 | 192 | ## See also 193 | 194 | BitMEX has a Python [REST client](https://github.com/BitMEX/api-connectors/tree/master/official-http/python-swaggerpy) 195 | and [websocket client.](https://github.com/BitMEX/api-connectors/tree/master/official-ws/python) 196 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2019 HDR Global Trading Ltd 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /market_maker/ws/ws_thread.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import websocket 3 | import threading 4 | import traceback 5 | import ssl 6 | from time import sleep 7 | import json 8 | import decimal 9 | import logging 10 | from market_maker.settings import settings 11 | from market_maker.auth.APIKeyAuth import generate_expires, generate_signature 12 | from market_maker.utils import log 13 | from market_maker.utils.math import toNearest 14 | from future.utils import iteritems 15 | from future.standard_library import hooks 16 | with hooks(): # Python 2/3 compat 17 | from urllib.parse import urlparse, urlunparse 18 | 19 | 20 | logger = log.setup_custom_logger('root') 21 | 22 | 23 | # Connects to BitMEX websocket for streaming realtime data. 24 | # The Marketmaker still interacts with this as if it were a REST Endpoint, but now it can get 25 | # much more realtime data without heavily polling the API. 26 | # 27 | # The Websocket offers a bunch of data as raw properties right on the object. 28 | # On connect, it synchronously asks for a push of all this data then returns. 29 | # Right after, the MM can start using its data. It will be updated in realtime, so the MM can 30 | # poll as often as it wants. 31 | class BitMEXWebsocket(): 32 | 33 | # Don't grow a table larger than this amount. Helps cap memory usage. 34 | MAX_TABLE_LEN = 200 35 | 36 | def __init__(self): 37 | self.__reset() 38 | 39 | def __del__(self): 40 | self.exit() 41 | 42 | def connect(self, endpoint="", symbol="XBTN15", shouldAuth=True): 43 | '''Connect to the websocket and initialize data stores.''' 44 | 45 | logger.debug("Connecting WebSocket.") 46 | self.symbol = symbol 47 | self.shouldAuth = shouldAuth 48 | 49 | # We can subscribe right in the connection querystring, so let's build that. 50 | # Subscribe to all pertinent endpoints 51 | subscriptions = [sub + ':' + symbol for sub in ["quote", "trade", "orderBookL2"]] 52 | subscriptions += ["instrument"] # We want all of them 53 | if self.shouldAuth: 54 | subscriptions += [sub + ':' + symbol for sub in ["order", "execution"]] 55 | subscriptions += ["margin", "position"] 56 | 57 | # Get WS URL and connect. 58 | urlParts = list(urlparse(endpoint)) 59 | urlParts[0] = urlParts[0].replace('http', 'ws') 60 | urlParts[2] = "/realtime?subscribe=" + ",".join(subscriptions) 61 | wsURL = urlunparse(urlParts) 62 | logger.info("Connecting to %s" % wsURL) 63 | self.__connect(wsURL) 64 | logger.info('Connected to WS. Waiting for data images, this may take a moment...') 65 | 66 | # Connected. Wait for partials 67 | self.__wait_for_symbol(symbol) 68 | if self.shouldAuth: 69 | self.__wait_for_account() 70 | logger.info('Got all market data. Starting.') 71 | 72 | # 73 | # Data methods 74 | # 75 | def get_instrument(self, symbol): 76 | instruments = self.data['instrument'] 77 | matchingInstruments = [i for i in instruments if i['symbol'] == symbol] 78 | if len(matchingInstruments) == 0: 79 | raise Exception("Unable to find instrument or index with symbol: " + symbol) 80 | instrument = matchingInstruments[0] 81 | # Turn the 'tickSize' into 'tickLog' for use in rounding 82 | # http://stackoverflow.com/a/6190291/832202 83 | instrument['tickLog'] = decimal.Decimal(str(instrument['tickSize'])).as_tuple().exponent * -1 84 | return instrument 85 | 86 | def get_ticker(self, symbol): 87 | '''Return a ticker object. Generated from instrument.''' 88 | 89 | instrument = self.get_instrument(symbol) 90 | 91 | # If this is an index, we have to get the data from the last trade. 92 | if instrument['symbol'][0] == '.': 93 | ticker = {} 94 | ticker['mid'] = ticker['buy'] = ticker['sell'] = ticker['last'] = instrument['markPrice'] 95 | # Normal instrument 96 | else: 97 | bid = instrument['bidPrice'] or instrument['lastPrice'] 98 | ask = instrument['askPrice'] or instrument['lastPrice'] 99 | ticker = { 100 | "last": instrument['lastPrice'], 101 | "buy": bid, 102 | "sell": ask, 103 | "mid": (bid + ask) / 2 104 | } 105 | 106 | # The instrument has a tickSize. Use it to round values. 107 | return {k: toNearest(float(v or 0), instrument['tickSize']) for k, v in iteritems(ticker)} 108 | 109 | def funds(self): 110 | return self.data['margin'][0] 111 | 112 | def market_depth(self, symbol): 113 | # raise NotImplementedError('orderBook is not subscribed; use askPrice and bidPrice on instrument') 114 | return self.data['orderBookL2'] 115 | 116 | def open_orders(self, clOrdIDPrefix): 117 | orders = self.data['order'] 118 | # Filter to only open orders (leavesQty > 0) and those that we actually placed 119 | return [o for o in orders if str(o['clOrdID']).startswith(clOrdIDPrefix) and o['leavesQty'] > 0] 120 | 121 | def position(self, symbol): 122 | positions = self.data['position'] 123 | pos = [p for p in positions if p['symbol'] == symbol] 124 | if len(pos) == 0: 125 | # No position found; stub it 126 | return {'avgCostPrice': 0, 'avgEntryPrice': 0, 'currentQty': 0, 'symbol': symbol} 127 | return pos[0] 128 | 129 | def recent_trades(self): 130 | return self.data['trade'] 131 | 132 | # 133 | # Lifecycle methods 134 | # 135 | def error(self, err): 136 | self._error = err 137 | logger.error(err) 138 | self.exit() 139 | 140 | def exit(self): 141 | self.exited = True 142 | self.ws.close() 143 | 144 | # 145 | # Private methods 146 | # 147 | 148 | def __connect(self, wsURL): 149 | '''Connect to the websocket in a thread.''' 150 | logger.debug("Starting thread") 151 | 152 | ssl_defaults = ssl.get_default_verify_paths() 153 | sslopt_ca_certs = {'ca_certs': ssl_defaults.cafile} 154 | self.ws = websocket.WebSocketApp(wsURL, 155 | on_message=self.__on_message, 156 | on_close=self.__on_close, 157 | on_open=self.__on_open, 158 | on_error=self.__on_error, 159 | header=self.__get_auth() 160 | ) 161 | 162 | self.wst = threading.Thread(target=lambda: self.ws.run_forever(sslopt=sslopt_ca_certs)) 163 | self.wst.daemon = True 164 | self.wst.start() 165 | logger.info("Started thread") 166 | 167 | # Wait for connect before continuing 168 | conn_timeout = 5 169 | while (not self.ws.sock or not self.ws.sock.connected) and conn_timeout and not self._error: 170 | sleep(1) 171 | conn_timeout -= 1 172 | 173 | if not conn_timeout or self._error: 174 | logger.error("Couldn't connect to WS! Exiting.") 175 | self.exit() 176 | sys.exit(1) 177 | 178 | def __get_auth(self): 179 | '''Return auth headers. Will use API Keys if present in settings.''' 180 | 181 | if self.shouldAuth is False: 182 | return [] 183 | 184 | logger.info("Authenticating with API Key.") 185 | # To auth to the WS using an API key, we generate a signature of a nonce and 186 | # the WS API endpoint. 187 | nonce = generate_expires() 188 | return [ 189 | "api-expires: " + str(nonce), 190 | "api-signature: " + generate_signature(settings.API_SECRET, 'GET', '/realtime', nonce, ''), 191 | "api-key:" + settings.API_KEY 192 | ] 193 | 194 | def __wait_for_account(self): 195 | '''On subscribe, this data will come down. Wait for it.''' 196 | # Wait for the keys to show up from the ws 197 | while not {'margin', 'position', 'order'} <= set(self.data): 198 | sleep(0.1) 199 | 200 | def __wait_for_symbol(self, symbol): 201 | '''On subscribe, this data will come down. Wait for it.''' 202 | while not {'instrument', 'trade', 'quote'} <= set(self.data): 203 | sleep(0.1) 204 | 205 | def __send_command(self, command, args): 206 | '''Send a raw command.''' 207 | self.ws.send(json.dumps({"op": command, "args": args or []})) 208 | 209 | def __on_message(self, message): 210 | '''Handler for parsing WS messages.''' 211 | message = json.loads(message) 212 | logger.debug(json.dumps(message)) 213 | 214 | table = message['table'] if 'table' in message else None 215 | action = message['action'] if 'action' in message else None 216 | try: 217 | if 'subscribe' in message: 218 | if message['success']: 219 | logger.debug("Subscribed to %s." % message['subscribe']) 220 | else: 221 | self.error("Unable to subscribe to %s. Error: \"%s\" Please check and restart." % 222 | (message['request']['args'][0], message['error'])) 223 | elif 'status' in message: 224 | if message['status'] == 400: 225 | self.error(message['error']) 226 | if message['status'] == 401: 227 | self.error("API Key incorrect, please check and restart.") 228 | elif action: 229 | 230 | if table not in self.data: 231 | self.data[table] = [] 232 | 233 | if table not in self.keys: 234 | self.keys[table] = [] 235 | 236 | # There are four possible actions from the WS: 237 | # 'partial' - full table image 238 | # 'insert' - new row 239 | # 'update' - update row 240 | # 'delete' - delete row 241 | if action == 'partial': 242 | logger.debug("%s: partial" % table) 243 | self.data[table] += message['data'] 244 | # Keys are communicated on partials to let you know how to uniquely identify 245 | # an item. We use it for updates. 246 | self.keys[table] = message['keys'] 247 | elif action == 'insert': 248 | logger.debug('%s: inserting %s' % (table, message['data'])) 249 | self.data[table] += message['data'] 250 | 251 | # Limit the max length of the table to avoid excessive memory usage. 252 | # Don't trim orders because we'll lose valuable state if we do. 253 | if table not in ['order', 'orderBookL2'] and len(self.data[table]) > BitMEXWebsocket.MAX_TABLE_LEN: 254 | self.data[table] = self.data[table][(BitMEXWebsocket.MAX_TABLE_LEN // 2):] 255 | 256 | elif action == 'update': 257 | logger.debug('%s: updating %s' % (table, message['data'])) 258 | # Locate the item in the collection and update it. 259 | for updateData in message['data']: 260 | item = findItemByKeys(self.keys[table], self.data[table], updateData) 261 | if not item: 262 | continue # No item found to update. Could happen before push 263 | 264 | # Log executions 265 | if table == 'order': 266 | is_canceled = 'ordStatus' in updateData and updateData['ordStatus'] == 'Canceled' 267 | if 'cumQty' in updateData and not is_canceled: 268 | contExecuted = updateData['cumQty'] - item['cumQty'] 269 | if contExecuted > 0: 270 | instrument = self.get_instrument(item['symbol']) 271 | logger.info("Execution: %s %d Contracts of %s at %.*f" % 272 | (item['side'], contExecuted, item['symbol'], 273 | instrument['tickLog'], item['price'] or updateData['price'])) 274 | 275 | # Update this item. 276 | item.update(updateData) 277 | 278 | # Remove canceled / filled orders 279 | if table == 'order' and item['leavesQty'] <= 0: 280 | self.data[table].remove(item) 281 | 282 | elif action == 'delete': 283 | logger.debug('%s: deleting %s' % (table, message['data'])) 284 | # Locate the item in the collection and remove it. 285 | for deleteData in message['data']: 286 | item = findItemByKeys(self.keys[table], self.data[table], deleteData) 287 | self.data[table].remove(item) 288 | else: 289 | raise Exception("Unknown action: %s" % action) 290 | except: 291 | logger.error(traceback.format_exc()) 292 | 293 | def __on_open(self): 294 | logger.debug("Websocket Opened.") 295 | 296 | def __on_close(self): 297 | logger.info('Websocket Closed') 298 | self.exit() 299 | 300 | def __on_error(self, error): 301 | if not self.exited: 302 | self.error(error) 303 | 304 | def __reset(self): 305 | self.data = {} 306 | self.keys = {} 307 | self.exited = False 308 | self._error = None 309 | 310 | 311 | def findItemByKeys(keys, table, matchData): 312 | for item in table: 313 | matched = True 314 | for key in keys: 315 | if item[key] != matchData[key]: 316 | matched = False 317 | if matched: 318 | return item 319 | 320 | if __name__ == "__main__": 321 | # create console handler and set level to debug 322 | logger = log.setup_custom_logger('websocket', logging.DEBUG) 323 | ws = BitMEXWebsocket() 324 | ws.connect("https://testnet.bitmex.com/api/v1") 325 | while(ws.ws.sock.connected): 326 | sleep(1) 327 | 328 | -------------------------------------------------------------------------------- /market_maker/bitmex.py: -------------------------------------------------------------------------------- 1 | """BitMEX API Connector.""" 2 | from __future__ import absolute_import 3 | import requests 4 | import time 5 | import datetime 6 | import json 7 | import base64 8 | import uuid 9 | from market_maker.auth import APIKeyAuthWithExpires 10 | from market_maker.utils import constants, errors, log 11 | from market_maker.ws.ws_thread import BitMEXWebsocket 12 | 13 | logger = log.setup_custom_logger('root') 14 | 15 | 16 | # https://www.bitmex.com/api/explorer/ 17 | class BitMEX(object): 18 | 19 | """BitMEX API Connector.""" 20 | 21 | def __init__(self, base_url=None, symbol=None, apiKey=None, apiSecret=None, 22 | orderIDPrefix='mm_bitmex_', shouldWSAuth=True, postOnly=False, timeout=7): 23 | """Init connector.""" 24 | self.base_url = base_url 25 | self.symbol = symbol 26 | self.postOnly = postOnly 27 | if (apiKey is None): 28 | raise Exception("Please set an API key and Secret to get started. See " + 29 | "https://github.com/BitMEX/sample-market-maker/#getting-started for more information." 30 | ) 31 | self.apiKey = apiKey 32 | self.apiSecret = apiSecret 33 | if len(orderIDPrefix) > 13: 34 | raise ValueError("settings.ORDERID_PREFIX must be at most 13 characters long!") 35 | self.orderIDPrefix = orderIDPrefix 36 | self.retries = 0 # initialize counter 37 | 38 | # Prepare HTTPS session 39 | self.session = requests.Session() 40 | # These headers are always sent 41 | self.session.headers.update({'user-agent': 'liquidbot-' + constants.VERSION}) 42 | self.session.headers.update({'content-type': 'application/json'}) 43 | self.session.headers.update({'accept': 'application/json'}) 44 | 45 | # Create websocket for streaming data 46 | self.ws = BitMEXWebsocket() 47 | self.ws.connect(base_url, symbol, shouldAuth=shouldWSAuth) 48 | 49 | self.timeout = timeout 50 | 51 | def __del__(self): 52 | self.exit() 53 | 54 | def exit(self): 55 | self.ws.exit() 56 | 57 | # 58 | # Public methods 59 | # 60 | def ticker_data(self, symbol=None): 61 | """Get ticker data.""" 62 | if symbol is None: 63 | symbol = self.symbol 64 | return self.ws.get_ticker(symbol) 65 | 66 | def instrument(self, symbol): 67 | """Get an instrument's details.""" 68 | return self.ws.get_instrument(symbol) 69 | 70 | def instruments(self, filter=None): 71 | query = {} 72 | if filter is not None: 73 | query['filter'] = json.dumps(filter) 74 | return self._curl_bitmex(path='instrument', query=query, verb='GET') 75 | 76 | def market_depth(self, symbol): 77 | """Get market depth / orderbook.""" 78 | return self.ws.market_depth(symbol) 79 | 80 | def recent_trades(self): 81 | """Get recent trades. 82 | 83 | Returns 84 | ------- 85 | A list of dicts: 86 | {u'amount': 60, 87 | u'date': 1306775375, 88 | u'price': 8.7401099999999996, 89 | u'tid': u'93842'}, 90 | 91 | """ 92 | return self.ws.recent_trades() 93 | 94 | # 95 | # Authentication required methods 96 | # 97 | def authentication_required(fn): 98 | """Annotation for methods that require auth.""" 99 | def wrapped(self, *args, **kwargs): 100 | if not (self.apiKey): 101 | msg = "You must be authenticated to use this method" 102 | raise errors.AuthenticationError(msg) 103 | else: 104 | return fn(self, *args, **kwargs) 105 | return wrapped 106 | 107 | @authentication_required 108 | def funds(self): 109 | """Get your current balance.""" 110 | return self.ws.funds() 111 | 112 | @authentication_required 113 | def position(self, symbol): 114 | """Get your open position.""" 115 | return self.ws.position(symbol) 116 | 117 | @authentication_required 118 | def isolate_margin(self, symbol, leverage, rethrow_errors=False): 119 | """Set the leverage on an isolated margin position""" 120 | path = "position/leverage" 121 | postdict = { 122 | 'symbol': symbol, 123 | 'leverage': leverage 124 | } 125 | return self._curl_bitmex(path=path, postdict=postdict, verb="POST", rethrow_errors=rethrow_errors) 126 | 127 | @authentication_required 128 | def delta(self): 129 | return self.position(self.symbol)['homeNotional'] 130 | 131 | @authentication_required 132 | def buy(self, quantity, price): 133 | """Place a buy order. 134 | 135 | Returns order object. ID: orderID 136 | """ 137 | return self.place_order(quantity, price) 138 | 139 | @authentication_required 140 | def sell(self, quantity, price): 141 | """Place a sell order. 142 | 143 | Returns order object. ID: orderID 144 | """ 145 | return self.place_order(-quantity, price) 146 | 147 | @authentication_required 148 | def place_order(self, quantity, price): 149 | """Place an order.""" 150 | if price < 0: 151 | raise Exception("Price must be positive.") 152 | 153 | endpoint = "order" 154 | # Generate a unique clOrdID with our prefix so we can identify it. 155 | clOrdID = self.orderIDPrefix + base64.b64encode(uuid.uuid4().bytes).decode('utf8').rstrip('=\n') 156 | postdict = { 157 | 'symbol': self.symbol, 158 | 'orderQty': quantity, 159 | 'price': price, 160 | 'clOrdID': clOrdID 161 | } 162 | return self._curl_bitmex(path=endpoint, postdict=postdict, verb="POST") 163 | 164 | @authentication_required 165 | def amend_bulk_orders(self, orders): 166 | """Amend multiple orders.""" 167 | # Note rethrow; if this fails, we want to catch it and re-tick 168 | return self._curl_bitmex(path='order/bulk', postdict={'orders': orders}, verb='PUT', rethrow_errors=True) 169 | 170 | @authentication_required 171 | def create_bulk_orders(self, orders): 172 | """Create multiple orders.""" 173 | for order in orders: 174 | order['clOrdID'] = self.orderIDPrefix + base64.b64encode(uuid.uuid4().bytes).decode('utf8').rstrip('=\n') 175 | order['symbol'] = self.symbol 176 | if self.postOnly: 177 | order['execInst'] = 'ParticipateDoNotInitiate' 178 | return self._curl_bitmex(path='order/bulk', postdict={'orders': orders}, verb='POST') 179 | 180 | @authentication_required 181 | def open_orders(self): 182 | """Get open orders.""" 183 | return self.ws.open_orders(self.orderIDPrefix) 184 | 185 | @authentication_required 186 | def http_open_orders(self): 187 | """Get open orders via HTTP. Used on close to ensure we catch them all.""" 188 | path = "order" 189 | orders = self._curl_bitmex( 190 | path=path, 191 | query={ 192 | 'filter': json.dumps({'ordStatus.isTerminated': False, 'symbol': self.symbol}), 193 | 'count': 500 194 | }, 195 | verb="GET" 196 | ) 197 | # Only return orders that start with our clOrdID prefix. 198 | return [o for o in orders if str(o['clOrdID']).startswith(self.orderIDPrefix)] 199 | 200 | @authentication_required 201 | def cancel(self, orderID): 202 | """Cancel an existing order.""" 203 | path = "order" 204 | postdict = { 205 | 'orderID': orderID, 206 | } 207 | return self._curl_bitmex(path=path, postdict=postdict, verb="DELETE") 208 | 209 | @authentication_required 210 | def withdraw(self, amount, fee, address): 211 | path = "user/requestWithdrawal" 212 | postdict = { 213 | 'amount': amount, 214 | 'fee': fee, 215 | 'currency': 'XBt', 216 | 'address': address 217 | } 218 | return self._curl_bitmex(path=path, postdict=postdict, verb="POST", max_retries=0) 219 | 220 | def _curl_bitmex(self, path, query=None, postdict=None, timeout=None, verb=None, rethrow_errors=False, 221 | max_retries=None): 222 | """Send a request to BitMEX Servers.""" 223 | # Handle URL 224 | url = self.base_url + path 225 | 226 | if timeout is None: 227 | timeout = self.timeout 228 | 229 | # Default to POST if data is attached, GET otherwise 230 | if not verb: 231 | verb = 'POST' if postdict else 'GET' 232 | 233 | # By default don't retry POST or PUT. Retrying GET/DELETE is okay because they are idempotent. 234 | # In the future we could allow retrying PUT, so long as 'leavesQty' is not used (not idempotent), 235 | # or you could change the clOrdID (set {"clOrdID": "new", "origClOrdID": "old"}) so that an amend 236 | # can't erroneously be applied twice. 237 | if max_retries is None: 238 | max_retries = 0 if verb in ['POST', 'PUT'] else 3 239 | 240 | # Auth: API Key/Secret 241 | auth = APIKeyAuthWithExpires(self.apiKey, self.apiSecret) 242 | 243 | def exit_or_throw(e): 244 | if rethrow_errors: 245 | raise e 246 | else: 247 | exit(1) 248 | 249 | def retry(): 250 | self.retries += 1 251 | if self.retries > max_retries: 252 | raise Exception("Max retries on %s (%s) hit, raising." % (path, json.dumps(postdict or ''))) 253 | return self._curl_bitmex(path, query, postdict, timeout, verb, rethrow_errors, max_retries) 254 | 255 | # Make the request 256 | response = None 257 | try: 258 | logger.info("sending req to %s: %s" % (url, json.dumps(postdict or query or ''))) 259 | req = requests.Request(verb, url, json=postdict, auth=auth, params=query) 260 | prepped = self.session.prepare_request(req) 261 | response = self.session.send(prepped, timeout=timeout) 262 | # Make non-200s throw 263 | response.raise_for_status() 264 | 265 | except requests.exceptions.HTTPError as e: 266 | if response is None: 267 | raise e 268 | 269 | # 401 - Auth error. This is fatal. 270 | if response.status_code == 401: 271 | logger.error("API Key or Secret incorrect, please check and restart.") 272 | logger.error("Error: " + response.text) 273 | if postdict: 274 | logger.error(postdict) 275 | # Always exit, even if rethrow_errors, because this is fatal 276 | exit(1) 277 | 278 | # 404, can be thrown if order canceled or does not exist. 279 | elif response.status_code == 404: 280 | if verb == 'DELETE': 281 | logger.error("Order not found: %s" % postdict['orderID']) 282 | return 283 | logger.error("Unable to contact the BitMEX API (404). " + 284 | "Request: %s \n %s" % (url, json.dumps(postdict))) 285 | exit_or_throw(e) 286 | 287 | # 429, ratelimit; cancel orders & wait until X-RateLimit-Reset 288 | elif response.status_code == 429: 289 | logger.error("Ratelimited on current request. Sleeping, then trying again. Try fewer " + 290 | "order pairs or contact support@bitmex.com to raise your limits. " + 291 | "Request: %s \n %s" % (url, json.dumps(postdict))) 292 | 293 | # Figure out how long we need to wait. 294 | ratelimit_reset = response.headers['X-RateLimit-Reset'] 295 | to_sleep = int(ratelimit_reset) - int(time.time()) 296 | reset_str = datetime.datetime.fromtimestamp(int(ratelimit_reset)).strftime('%X') 297 | 298 | # We're ratelimited, and we may be waiting for a long time. Cancel orders. 299 | logger.warning("Canceling all known orders in the meantime.") 300 | self.cancel([o['orderID'] for o in self.open_orders()]) 301 | 302 | logger.error("Your ratelimit will reset at %s. Sleeping for %d seconds." % (reset_str, to_sleep)) 303 | time.sleep(to_sleep) 304 | 305 | # Retry the request. 306 | return retry() 307 | 308 | # 503 - BitMEX temporary downtime, likely due to a deploy. Try again 309 | elif response.status_code == 503: 310 | logger.warning("Unable to contact the BitMEX API (503), retrying. " + 311 | "Request: %s \n %s" % (url, json.dumps(postdict))) 312 | time.sleep(3) 313 | return retry() 314 | 315 | elif response.status_code == 400: 316 | error = response.json()['error'] 317 | message = error['message'].lower() if error else '' 318 | 319 | # Duplicate clOrdID: that's fine, probably a deploy, go get the order(s) and return it 320 | if 'duplicate clordid' in message: 321 | orders = postdict['orders'] if 'orders' in postdict else postdict 322 | 323 | IDs = json.dumps({'clOrdID': [order['clOrdID'] for order in orders]}) 324 | orderResults = self._curl_bitmex('/order', query={'filter': IDs}, verb='GET') 325 | 326 | for i, order in enumerate(orderResults): 327 | if ( 328 | order['orderQty'] != abs(postdict['orderQty']) or 329 | order['side'] != ('Buy' if postdict['orderQty'] > 0 else 'Sell') or 330 | order['price'] != postdict['price'] or 331 | order['symbol'] != postdict['symbol']): 332 | raise Exception('Attempted to recover from duplicate clOrdID, but order returned from API ' + 333 | 'did not match POST.\nPOST data: %s\nReturned order: %s' % ( 334 | json.dumps(orders[i]), json.dumps(order))) 335 | # All good 336 | return orderResults 337 | 338 | elif 'insufficient available balance' in message: 339 | logger.error('Account out of funds. The message: %s' % error['message']) 340 | exit_or_throw(Exception('Insufficient Funds')) 341 | 342 | 343 | # If we haven't returned or re-raised yet, we get here. 344 | logger.error("Unhandled Error: %s: %s" % (e, response.text)) 345 | logger.error("Endpoint was: %s %s: %s" % (verb, path, json.dumps(postdict))) 346 | exit_or_throw(e) 347 | 348 | except requests.exceptions.Timeout as e: 349 | # Timeout, re-run this request 350 | logger.warning("Timed out on request: %s (%s), retrying..." % (path, json.dumps(postdict or ''))) 351 | return retry() 352 | 353 | except requests.exceptions.ConnectionError as e: 354 | logger.warning("Unable to contact the BitMEX API (%s). Please check the URL. Retrying. " + 355 | "Request: %s %s \n %s" % (e, url, json.dumps(postdict))) 356 | time.sleep(1) 357 | return retry() 358 | 359 | # Reset retry counter on success 360 | self.retries = 0 361 | 362 | return response.json() 363 | -------------------------------------------------------------------------------- /market_maker/market_maker.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from time import sleep 3 | import sys 4 | from datetime import datetime 5 | from os.path import getmtime 6 | import random 7 | import requests 8 | import atexit 9 | import signal 10 | 11 | from market_maker import bitmex 12 | from market_maker.settings import settings 13 | from market_maker.utils import log, constants, errors, math 14 | 15 | # Used for reloading the bot - saves modified times of key files 16 | import os 17 | watched_files_mtimes = [(f, getmtime(f)) for f in settings.WATCHED_FILES] 18 | 19 | 20 | # 21 | # Helpers 22 | # 23 | logger = log.setup_custom_logger('root') 24 | 25 | 26 | class ExchangeInterface: 27 | def __init__(self, dry_run=False): 28 | self.dry_run = dry_run 29 | if len(sys.argv) > 1: 30 | self.symbol = sys.argv[1] 31 | else: 32 | self.symbol = settings.SYMBOL 33 | self.bitmex = bitmex.BitMEX(base_url=settings.BASE_URL, symbol=self.symbol, 34 | apiKey=settings.API_KEY, apiSecret=settings.API_SECRET, 35 | orderIDPrefix=settings.ORDERID_PREFIX, postOnly=settings.POST_ONLY, 36 | timeout=settings.TIMEOUT) 37 | 38 | def cancel_order(self, order): 39 | tickLog = self.get_instrument()['tickLog'] 40 | logger.info("Canceling: %s %d @ %.*f" % (order['side'], order['orderQty'], tickLog, order['price'])) 41 | while True: 42 | try: 43 | self.bitmex.cancel(order['orderID']) 44 | sleep(settings.API_REST_INTERVAL) 45 | except ValueError as e: 46 | logger.info(e) 47 | sleep(settings.API_ERROR_INTERVAL) 48 | else: 49 | break 50 | 51 | def cancel_all_orders(self): 52 | if self.dry_run: 53 | return 54 | 55 | logger.info("Resetting current position. Canceling all existing orders.") 56 | tickLog = self.get_instrument()['tickLog'] 57 | 58 | # In certain cases, a WS update might not make it through before we call this. 59 | # For that reason, we grab via HTTP to ensure we grab them all. 60 | orders = self.bitmex.http_open_orders() 61 | 62 | for order in orders: 63 | logger.info("Canceling: %s %d @ %.*f" % (order['side'], order['orderQty'], tickLog, order['price'])) 64 | 65 | if len(orders): 66 | self.bitmex.cancel([order['orderID'] for order in orders]) 67 | 68 | sleep(settings.API_REST_INTERVAL) 69 | 70 | def get_portfolio(self): 71 | contracts = settings.CONTRACTS 72 | portfolio = {} 73 | for symbol in contracts: 74 | position = self.bitmex.position(symbol=symbol) 75 | instrument = self.bitmex.instrument(symbol=symbol) 76 | 77 | if instrument['isQuanto']: 78 | future_type = "Quanto" 79 | elif instrument['isInverse']: 80 | future_type = "Inverse" 81 | elif not instrument['isQuanto'] and not instrument['isInverse']: 82 | future_type = "Linear" 83 | else: 84 | raise NotImplementedError("Unknown future type; not quanto or inverse: %s" % instrument['symbol']) 85 | 86 | if instrument['underlyingToSettleMultiplier'] is None: 87 | multiplier = float(instrument['multiplier']) / float(instrument['quoteToSettleMultiplier']) 88 | else: 89 | multiplier = float(instrument['multiplier']) / float(instrument['underlyingToSettleMultiplier']) 90 | 91 | portfolio[symbol] = { 92 | "currentQty": float(position['currentQty']), 93 | "futureType": future_type, 94 | "multiplier": multiplier, 95 | "markPrice": float(instrument['markPrice']), 96 | "spot": float(instrument['indicativeSettlePrice']) 97 | } 98 | 99 | return portfolio 100 | 101 | def calc_delta(self): 102 | """Calculate currency delta for portfolio""" 103 | portfolio = self.get_portfolio() 104 | spot_delta = 0 105 | mark_delta = 0 106 | for symbol in portfolio: 107 | item = portfolio[symbol] 108 | if item['futureType'] == "Quanto": 109 | spot_delta += item['currentQty'] * item['multiplier'] * item['spot'] 110 | mark_delta += item['currentQty'] * item['multiplier'] * item['markPrice'] 111 | elif item['futureType'] == "Inverse": 112 | spot_delta += (item['multiplier'] / item['spot']) * item['currentQty'] 113 | mark_delta += (item['multiplier'] / item['markPrice']) * item['currentQty'] 114 | elif item['futureType'] == "Linear": 115 | spot_delta += item['multiplier'] * item['currentQty'] 116 | mark_delta += item['multiplier'] * item['currentQty'] 117 | basis_delta = mark_delta - spot_delta 118 | delta = { 119 | "spot": spot_delta, 120 | "mark_price": mark_delta, 121 | "basis": basis_delta 122 | } 123 | return delta 124 | 125 | def get_delta(self, symbol=None): 126 | if symbol is None: 127 | symbol = self.symbol 128 | # return self.get_position(symbol)['currentQty'] 129 | return self.get_position(symbol)['currentQty'] + XBt_to_XBT(self.get_margin()['marginBalance']) * self.get_ticker(symbol)['last'] 130 | 131 | def get_instrument(self, symbol=None): 132 | if symbol is None: 133 | symbol = self.symbol 134 | return self.bitmex.instrument(symbol) 135 | 136 | def get_market_depth(self, symbol=None): 137 | if symbol is None: 138 | symbol = self.symbol 139 | return self.bitmex.market_depth(symbol) 140 | 141 | def get_margin(self): 142 | if self.dry_run: 143 | return {'marginBalance': float(settings.DRY_BTC), 'availableFunds': float(settings.DRY_BTC)} 144 | return self.bitmex.funds() 145 | 146 | def get_orders(self): 147 | if self.dry_run: 148 | return [] 149 | return self.bitmex.open_orders() 150 | 151 | def get_highest_buy(self): 152 | buys = [o for o in self.get_orders() if o['side'] == 'Buy'] 153 | if not len(buys): 154 | return {'price': -2**32} 155 | highest_buy = max(buys or [], key=lambda o: o['price']) 156 | return highest_buy if highest_buy else {'price': -2**32} 157 | 158 | def get_lowest_sell(self): 159 | sells = [o for o in self.get_orders() if o['side'] == 'Sell'] 160 | if not len(sells): 161 | return {'price': 2**32} 162 | lowest_sell = min(sells or [], key=lambda o: o['price']) 163 | return lowest_sell if lowest_sell else {'price': 2**32} # ought to be enough for anyone 164 | 165 | def get_position(self, symbol=None): 166 | if symbol is None: 167 | symbol = self.symbol 168 | return self.bitmex.position(symbol) 169 | 170 | def get_ticker(self, symbol=None): 171 | if symbol is None: 172 | symbol = self.symbol 173 | return self.bitmex.ticker_data(symbol) 174 | 175 | def is_open(self): 176 | """Check that websockets are still open.""" 177 | return not self.bitmex.ws.exited 178 | 179 | def check_market_open(self): 180 | instrument = self.get_instrument() 181 | if instrument["state"] != "Open" and instrument["state"] != "Closed": 182 | raise errors.MarketClosedError("The instrument %s is not open. State: %s" % 183 | (self.symbol, instrument["state"])) 184 | 185 | def check_if_orderbook_empty(self): 186 | """This function checks whether the order book is empty""" 187 | instrument = self.get_instrument() 188 | if instrument['midPrice'] is None: 189 | raise errors.MarketEmptyError("Orderbook is empty, cannot quote") 190 | 191 | def amend_bulk_orders(self, orders): 192 | if self.dry_run: 193 | return orders 194 | return self.bitmex.amend_bulk_orders(orders) 195 | 196 | def create_bulk_orders(self, orders): 197 | if self.dry_run: 198 | return orders 199 | return self.bitmex.create_bulk_orders(orders) 200 | 201 | def cancel_bulk_orders(self, orders): 202 | if self.dry_run: 203 | return orders 204 | return self.bitmex.cancel([order['orderID'] for order in orders]) 205 | 206 | 207 | class OrderManager: 208 | def __init__(self): 209 | self.exchange = ExchangeInterface(settings.DRY_RUN) 210 | # Once exchange is created, register exit handler that will always cancel orders 211 | # on any error. 212 | atexit.register(self.exit) 213 | signal.signal(signal.SIGTERM, self.exit) 214 | 215 | logger.info("Using symbol %s." % self.exchange.symbol) 216 | 217 | if settings.DRY_RUN: 218 | logger.info("Initializing dry run. Orders printed below represent what would be posted to BitMEX.") 219 | else: 220 | logger.info("Order Manager initializing, connecting to BitMEX. Live run: executing real trades.") 221 | 222 | self.start_time = datetime.now() 223 | self.instrument = self.exchange.get_instrument() 224 | self.starting_qty = self.exchange.get_delta() 225 | self.running_qty = self.starting_qty 226 | self.reset() 227 | 228 | def reset(self): 229 | self.exchange.cancel_all_orders() 230 | self.sanity_check() 231 | self.print_status() 232 | 233 | # Create orders and converge. 234 | self.place_orders() 235 | 236 | def print_status(self): 237 | """Print the current MM status.""" 238 | 239 | margin = self.exchange.get_margin() 240 | position = self.exchange.get_position() 241 | self.running_qty = self.exchange.get_delta() 242 | tickLog = self.exchange.get_instrument()['tickLog'] 243 | self.start_XBt = margin["marginBalance"] 244 | 245 | logger.info("Current XBT Balance: %.6f" % XBt_to_XBT(self.start_XBt)) 246 | logger.info("Current Contract Position: %d" % self.running_qty) 247 | if settings.CHECK_POSITION_LIMITS: 248 | logger.info("Position limits: %d/%d" % (settings.MIN_POSITION, settings.MAX_POSITION)) 249 | if position['currentQty'] != 0: 250 | logger.info("Avg Cost Price: %.*f" % (tickLog, float(position['avgCostPrice']))) 251 | logger.info("Avg Entry Price: %.*f" % (tickLog, float(position['avgEntryPrice']))) 252 | logger.info("Contracts Traded This Run: %d" % (self.running_qty - self.starting_qty)) 253 | logger.info("Total Contract Delta: %.4f XBT" % self.exchange.calc_delta()['spot']) 254 | 255 | def get_ticker(self): 256 | ticker = self.exchange.get_ticker() 257 | tickLog = self.exchange.get_instrument()['tickLog'] 258 | 259 | # Set up our buy & sell positions as the smallest possible unit above and below the current spread 260 | # and we'll work out from there. That way we always have the best price but we don't kill wide 261 | # and potentially profitable spreads. 262 | self.start_position_buy = ticker["buy"] + self.instrument['tickSize'] 263 | self.start_position_sell = ticker["sell"] - self.instrument['tickSize'] 264 | 265 | # If we're maintaining spreads and we already have orders in place, 266 | # make sure they're not ours. If they are, we need to adjust, otherwise we'll 267 | # just work the orders inward until they collide. 268 | if settings.MAINTAIN_SPREADS: 269 | if ticker['buy'] == self.exchange.get_highest_buy()['price']: 270 | self.start_position_buy = ticker["buy"] 271 | if ticker['sell'] == self.exchange.get_lowest_sell()['price']: 272 | self.start_position_sell = ticker["sell"] 273 | 274 | # Back off if our spread is too small. 275 | if self.start_position_buy * (1.00 + settings.MIN_SPREAD) > self.start_position_sell: 276 | self.start_position_buy *= (1.00 - (settings.MIN_SPREAD / 2)) 277 | self.start_position_sell *= (1.00 + (settings.MIN_SPREAD / 2)) 278 | 279 | # Midpoint, used for simpler order placement. 280 | self.start_position_mid = ticker["mid"] 281 | logger.info( 282 | "%s Ticker: Buy: %.*f, Sell: %.*f" % 283 | (self.instrument['symbol'], tickLog, ticker["buy"], tickLog, ticker["sell"]) 284 | ) 285 | # logger.info('Start Positions: Buy: %.*f, Sell: %.*f, Mid: %.*f' % 286 | # (tickLog, self.start_position_buy, tickLog, self.start_position_sell, 287 | # tickLog, self.start_position_mid)) 288 | return ticker 289 | 290 | def get_price_offset(self, index): 291 | """Given an index (1, -1, 2, -2, etc.) return the price for that side of the book. 292 | Negative is a buy, positive is a sell.""" 293 | # Maintain existing spreads for max profit 294 | if settings.MAINTAIN_SPREADS: 295 | start_position = self.start_position_buy if index < 0 else self.start_position_sell 296 | # First positions (index 1, -1) should start right at start_position, others should branch from there 297 | index = index + 1 if index < 0 else index - 1 298 | else: 299 | # Offset mode: ticker comes from a reference exchange and we define an offset. 300 | start_position = self.start_position_buy if index < 0 else self.start_position_sell 301 | 302 | # If we're attempting to sell, but our sell price is actually lower than the buy, 303 | # move over to the sell side. 304 | if index > 0 and start_position < self.start_position_buy: 305 | start_position = self.start_position_sell 306 | # Same for buys. 307 | if index < 0 and start_position > self.start_position_sell: 308 | start_position = self.start_position_buy 309 | 310 | return math.toNearest(start_position * (1 + settings.INTERVAL) ** index, self.instrument['tickSize']) 311 | 312 | ### 313 | # Orders 314 | ### 315 | 316 | def place_orders(self): 317 | """Create order items for use in convergence.""" 318 | 319 | buy_orders = [] 320 | sell_orders = [] 321 | # Create orders from the outside in. This is intentional - let's say the inner order gets taken; 322 | # then we match orders from the outside in, ensuring the fewest number of orders are amended and only 323 | # a new order is created in the inside. If we did it inside-out, all orders would be amended 324 | # down and a new order would be created at the outside. 325 | for i in reversed(range(1, settings.ORDER_PAIRS + 1)): 326 | if not self.long_position_limit_exceeded(): 327 | buy_orders.append(self.prepare_order(-i)) 328 | if not self.short_position_limit_exceeded(): 329 | sell_orders.append(self.prepare_order(i)) 330 | 331 | return self.converge_orders(buy_orders, sell_orders) 332 | 333 | def prepare_order(self, index): 334 | """Create an order object.""" 335 | 336 | if settings.RANDOM_ORDER_SIZE is True: 337 | quantity = random.randint(settings.MIN_ORDER_SIZE, settings.MAX_ORDER_SIZE) 338 | else: 339 | quantity = settings.ORDER_START_SIZE + ((abs(index) - 1) * settings.ORDER_STEP_SIZE) 340 | 341 | price = self.get_price_offset(index) 342 | 343 | return {'price': price, 'orderQty': quantity, 'side': "Buy" if index < 0 else "Sell"} 344 | 345 | def converge_orders(self, buy_orders, sell_orders): 346 | """Converge the orders we currently have in the book with what we want to be in the book. 347 | This involves amending any open orders and creating new ones if any have filled completely. 348 | We start from the closest orders outward.""" 349 | 350 | tickLog = self.exchange.get_instrument()['tickLog'] 351 | to_amend = [] 352 | to_create = [] 353 | to_cancel = [] 354 | buys_matched = 0 355 | sells_matched = 0 356 | existing_orders = self.exchange.get_orders() 357 | 358 | # Check all existing orders and match them up with what we want to place. 359 | # If there's an open one, we might be able to amend it to fit what we want. 360 | for order in existing_orders: 361 | try: 362 | if order['side'] == 'Buy': 363 | desired_order = buy_orders[buys_matched] 364 | buys_matched += 1 365 | else: 366 | desired_order = sell_orders[sells_matched] 367 | sells_matched += 1 368 | 369 | # Found an existing order. Do we need to amend it? 370 | if desired_order['orderQty'] != order['leavesQty'] or ( 371 | # If price has changed, and the change is more than our RELIST_INTERVAL, amend. 372 | desired_order['price'] != order['price'] and 373 | abs((desired_order['price'] / order['price']) - 1) > settings.RELIST_INTERVAL): 374 | to_amend.append({'orderID': order['orderID'], 'orderQty': order['cumQty'] + desired_order['orderQty'], 375 | 'price': desired_order['price'], 'side': order['side']}) 376 | except IndexError: 377 | # Will throw if there isn't a desired order to match. In that case, cancel it. 378 | to_cancel.append(order) 379 | 380 | while buys_matched < len(buy_orders): 381 | to_create.append(buy_orders[buys_matched]) 382 | buys_matched += 1 383 | 384 | while sells_matched < len(sell_orders): 385 | to_create.append(sell_orders[sells_matched]) 386 | sells_matched += 1 387 | 388 | if len(to_amend) > 0: 389 | for amended_order in reversed(to_amend): 390 | reference_order = [o for o in existing_orders if o['orderID'] == amended_order['orderID']][0] 391 | logger.info("Amending %4s: %d @ %.*f to %d @ %.*f (%+.*f)" % ( 392 | amended_order['side'], 393 | reference_order['leavesQty'], tickLog, reference_order['price'], 394 | (amended_order['orderQty'] - reference_order['cumQty']), tickLog, amended_order['price'], 395 | tickLog, (amended_order['price'] - reference_order['price']) 396 | )) 397 | # This can fail if an order has closed in the time we were processing. 398 | # The API will send us `invalid ordStatus`, which means that the order's status (Filled/Canceled) 399 | # made it not amendable. 400 | # If that happens, we need to catch it and re-tick. 401 | try: 402 | self.exchange.amend_bulk_orders(to_amend) 403 | except requests.exceptions.HTTPError as e: 404 | errorObj = e.response.json() 405 | if errorObj['error']['message'] == 'Invalid ordStatus': 406 | logger.warn("Amending failed. Waiting for order data to converge and retrying.") 407 | sleep(0.5) 408 | return self.place_orders() 409 | else: 410 | logger.error("Unknown error on amend: %s. Exiting" % errorObj) 411 | sys.exit(1) 412 | 413 | if len(to_create) > 0: 414 | logger.info("Creating %d orders:" % (len(to_create))) 415 | for order in reversed(to_create): 416 | logger.info("%4s %d @ %.*f" % (order['side'], order['orderQty'], tickLog, order['price'])) 417 | self.exchange.create_bulk_orders(to_create) 418 | 419 | # Could happen if we exceed a delta limit 420 | if len(to_cancel) > 0: 421 | logger.info("Canceling %d orders:" % (len(to_cancel))) 422 | for order in reversed(to_cancel): 423 | logger.info("%4s %d @ %.*f" % (order['side'], order['leavesQty'], tickLog, order['price'])) 424 | self.exchange.cancel_bulk_orders(to_cancel) 425 | 426 | ### 427 | # Position Limits 428 | ### 429 | 430 | def short_position_limit_exceeded(self): 431 | """Returns True if the short position limit is exceeded""" 432 | if not settings.CHECK_POSITION_LIMITS: 433 | return False 434 | position = self.exchange.get_delta() 435 | return position <= settings.MIN_POSITION 436 | 437 | def long_position_limit_exceeded(self): 438 | """Returns True if the long position limit is exceeded""" 439 | if not settings.CHECK_POSITION_LIMITS: 440 | return False 441 | position = self.exchange.get_delta() 442 | return position >= settings.MAX_POSITION 443 | 444 | ### 445 | # Sanity 446 | ## 447 | 448 | def sanity_check(self): 449 | """Perform checks before placing orders.""" 450 | 451 | # Check if OB is empty - if so, can't quote. 452 | self.exchange.check_if_orderbook_empty() 453 | 454 | # Ensure market is still open. 455 | self.exchange.check_market_open() 456 | 457 | # Get ticker, which sets price offsets and prints some debugging info. 458 | ticker = self.get_ticker() 459 | 460 | # Sanity check: 461 | if self.get_price_offset(-1) >= ticker["sell"] or self.get_price_offset(1) <= ticker["buy"]: 462 | logger.error("Buy: %s, Sell: %s" % (self.start_position_buy, self.start_position_sell)) 463 | logger.error("First buy position: %s\nBitMEX Best Ask: %s\nFirst sell position: %s\nBitMEX Best Bid: %s" % 464 | (self.get_price_offset(-1), ticker["sell"], self.get_price_offset(1), ticker["buy"])) 465 | logger.error("Sanity check failed, exchange data is inconsistent") 466 | self.exit() 467 | 468 | # Messaging if the position limits are reached 469 | if self.long_position_limit_exceeded(): 470 | logger.info("Long delta limit exceeded") 471 | logger.info("Current Position: %.f, Maximum Position: %.f" % 472 | (self.exchange.get_delta(), settings.MAX_POSITION)) 473 | 474 | if self.short_position_limit_exceeded(): 475 | logger.info("Short delta limit exceeded") 476 | logger.info("Current Position: %.f, Minimum Position: %.f" % 477 | (self.exchange.get_delta(), settings.MIN_POSITION)) 478 | 479 | ### 480 | # Running 481 | ### 482 | 483 | def check_file_change(self): 484 | """Restart if any files we're watching have changed.""" 485 | for f, mtime in watched_files_mtimes: 486 | if getmtime(f) > mtime: 487 | self.restart() 488 | 489 | def check_connection(self): 490 | """Ensure the WS connections are still open.""" 491 | return self.exchange.is_open() 492 | 493 | def exit(self): 494 | logger.info("Shutting down. All open orders will be cancelled.") 495 | try: 496 | self.exchange.cancel_all_orders() 497 | self.exchange.bitmex.exit() 498 | except errors.AuthenticationError as e: 499 | logger.info("Was not authenticated; could not cancel orders.") 500 | except Exception as e: 501 | logger.info("Unable to cancel orders: %s" % e) 502 | 503 | sys.exit() 504 | 505 | def run_loop(self): 506 | while True: 507 | sys.stdout.write("-----\n") 508 | sys.stdout.flush() 509 | 510 | self.check_file_change() 511 | sleep(settings.LOOP_INTERVAL) 512 | 513 | # This will restart on very short downtime, but if it's longer, 514 | # the MM will crash entirely as it is unable to connect to the WS on boot. 515 | if not self.check_connection(): 516 | logger.error("Realtime data connection unexpectedly closed, restarting.") 517 | self.restart() 518 | 519 | self.sanity_check() # Ensures health of mm - several cut-out points here 520 | self.print_status() # Print skew, delta, etc 521 | self.place_orders() # Creates desired orders and converges to existing orders 522 | 523 | def restart(self): 524 | logger.info("Restarting the market maker...") 525 | os.execv(sys.executable, [sys.executable] + sys.argv) 526 | 527 | # 528 | # Helpers 529 | # 530 | 531 | 532 | def XBt_to_XBT(XBt): 533 | return float(XBt) / constants.XBt_TO_XBT 534 | 535 | 536 | def cost(instrument, quantity, price): 537 | mult = instrument["multiplier"] 538 | P = mult * price if mult >= 0 else mult / price 539 | return abs(quantity * P) 540 | 541 | 542 | def margin(instrument, quantity, price): 543 | return cost(instrument, quantity, price) * instrument["initMargin"] 544 | 545 | 546 | def run(): 547 | logger.info('BitMEX Market Maker Version: %s\n' % constants.VERSION) 548 | 549 | om = OrderManager() 550 | # Try/except just keeps ctrl-c from printing an ugly stacktrace 551 | try: 552 | om.run_loop() 553 | except (KeyboardInterrupt, SystemExit): 554 | sys.exit() 555 | --------------------------------------------------------------------------------