GCP Console에서 BitMEX 마켓 메이킹 테스트 봇을 이동하는 단계

최근 비트코인의 고속 거래에 흥미가 있어 여러가지 시험하고 있습니다. 테스트 환경은 어려움을 겪었지만 BitMEX만 테스트 환경 testnet을 제공합니다. 이것에 대해, 마켓 메이킹의 실험을 Google Compute Platform GCPCloud Shell로 간단하게 가 보려고 했습니다.

이 절차는 기본적으로 Sample BitMEX Market Making Bot README과 같지만 몇 가지주의 사항이 있습니다.

기본 거래 전략은 기본 마켓 메이킹입니다. README Operation Overview를 읽어 보자.

Testnet BitMEX 계정 및 API Key 만들기



BitMEX testnet을 사용하여 계정을 만든 후 API Key 만들기 할 때 Key Permissions에 Order를 지정해야합니다. 이제 testnet 테스트 환경에 대해 주문 및 취소가 가능합니다. 물론, 그것을 하지 않고, 주문·캔슬이 실패하는 것을 확인하는 테스트도 가능합니다.



GCP의 Cloud Shell에 Python 3.6 설치



Cloud Shell에는 기본적으로 Python 3.5가 포함되어 있지만 이번에는 Python 3.6을 사용하고 싶으므로 Conda에서 홈에 설치하고 Cloud Shell을 시작할 때마다 다시 설치하지 않고 살 수 있도록했습니다. .
mkdir -p ~/tmp; cd ~/tmp
curl -s \
  https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh ~/tmp/Miniconda3-latest-Linux-x86_64.sh -b -p ~/miniconda3/
echo 'export PATH="~/miniconda3/bin:$PATH"' >> ~/.bashrc
export PATH="~/miniconda3/bin:$PATH"
conda create -n bitmex python=3

설치한 Python3.6을 활성화합니다. 이 명령은 Cloud Shell을 새로 시작할 때마다 필요합니다.
source activate bitmex

Sample BitMEX Market Making Bot 설치



설치


source activate bitmex
pip install bitmex-market-maker
mkdir ~/testnet-bitmex-market-maker/
cd ~/testnet-bitmex-market-maker/
marketmaker setup

설정


testnet-bitmex-market-maker/settings.py를 편집하고 API_KEY 및 API_SECRET을 올바르게 채웁니다. 테스트 환경이므로, 부담없이 DRY_RUN=False 로 해 버립니다.

실행


cd ~/testnet-bitmex-market-maker/
marketmaker

이런 느낌의 출력이 됩니다.
$ marketmaker
2018-06-18 08:17:51,544 - INFO - market_maker - BitMEX Market Maker Version: v1.4

2018-06-18 08:17:51,545 - INFO - ws_thread - Connecting to wss://testnet.bitmex.com/realtime?subscribe=quote:XBTUSD,trade:XBTUSD,instrument,order:XBTUSD,execution:XBTUSD,margin,position
2018-06-18 08:17:51,546 - INFO - ws_thread - Authenticating with API Key.
2018-06-18 08:17:51,547 - INFO - ws_thread - Started thread
2018-06-18 08:17:53,549 - INFO - ws_thread - Connected to WS. Waiting for data images, this may take a moment...
2018-06-18 08:17:53,550 - INFO - ws_thread - Got all market data. Starting.
2018-06-18 08:17:53,550 - INFO - market_maker - Using symbol XBTUSD.
2018-06-18 08:17:53,550 - INFO - market_maker - Order Manager initializing, connecting to BitMEX. Live run: executing real trades.
2018-06-18 08:17:53,550 - INFO - market_maker - Resetting current position. Canceling all existing orders.
2018-06-18 08:17:53,550 - INFO - bitmex - sending req to https://testnet.bitmex.com/api/v1/order: {"filter": "{\"ordStatus.isTerminated\": false, \"symbol\": \"XBTUSD\"}", "count": 500}
2018-06-18 08:17:54,927 - INFO - market_maker - XBTUSD Ticker: Buy: 6474.5, Sell: 6475.0
2018-06-18 08:17:54,927 - INFO - market_maker - Start Positions: Buy: 6471.8, Sell: 6477.7, Mid: 6475.0
2018-06-18 08:17:54,927 - INFO - market_maker - Long delta limit exceeded
2018-06-18 08:17:54,927 - INFO - market_maker - Current Position: 1000, Maximum Position: 10
2018-06-18 08:17:54,927 - INFO - market_maker - Current XBT Balance: 1.228862
2018-06-18 08:17:54,927 - INFO - market_maker - Current Contract Position: 1000
2018-06-18 08:17:54,927 - INFO - market_maker - Position limits: -10/10
2018-06-18 08:17:54,927 - INFO - market_maker - Avg Cost Price: 6472.5
2018-06-18 08:17:54,927 - INFO - market_maker - Avg Entry Price: 6472.5
2018-06-18 08:17:54,927 - INFO - market_maker - Contracts Traded This Run: 0
2018-06-18 08:17:54,927 - INFO - market_maker - Total Contract Delta: 0.1548 XBT
2018-06-18 08:17:54,927 - INFO - market_maker - Creating 3 orders:
2018-06-18 08:17:54,927 - INFO - market_maker - Sell 1000 @ 6477.5
2018-06-18 08:17:54,927 - INFO - market_maker - Sell 1100 @ 6510.0
2018-06-18 08:17:54,927 - INFO - market_maker - Sell 1200 @ 6542.5
2018-06-18 08:17:54,928 - INFO - bitmex - sending req to https://testnet.bitmex.com/api/v1/order/bulk: {"orders": [{"price": 6542.5, "orderQty": 1200, "side": "Sell", "clOrdID": "mm_bitmex_rd5tG3aURzmxMfW9166f7A", "symbol": "XBTUSD"}, {"price": 6510.0, "orderQty": 1100, "side": "Sell", "clOrdID": "mm_bitmex_s8eaXKHES5mxuKpO6xfeWg", "symbol": "XBTUSD"}, {"price": 6477.5, "orderQty": 1000, "side": "Sell", "clOrdID": "mm_bitmex_xoXXUYI3Sd2NUIf6D4Exjg", "symbol": "XBTUSD"}]}
-----
2018-06-18 08:18:00,906 - INFO - market_maker - XBTUSD Ticker: Buy: 6474.5, Sell: 6475.0
2018-06-18 08:18:00,907 - INFO - market_maker - Start Positions: Buy: 6471.8, Sell: 6477.7, Mid: 6475.0
2018-06-18 08:18:00,907 - INFO - market_maker - Long delta limit exceeded
2018-06-18 08:18:00,907 - INFO - market_maker - Current Position: 1000, Maximum Position: 10
2018-06-18 08:18:00,907 - INFO - market_maker - Current XBT Balance: 1.228912
2018-06-18 08:18:00,907 - INFO - market_maker - Current Contract Position: 1000
2018-06-18 08:18:00,907 - INFO - market_maker - Position limits: -10/10
2018-06-18 08:18:00,907 - INFO - market_maker - Avg Cost Price: 6472.5
2018-06-18 08:18:00,907 - INFO - market_maker - Avg Entry Price: 6472.5
2018-06-18 08:18:00,907 - INFO - market_maker - Contracts Traded This Run: 0
2018-06-18 08:18:00,907 - INFO - market_maker - Total Contract Delta: 0.1548 XBT
-----
2018-06-18 08:18:05,913 - INFO - market_maker - XBTUSD Ticker: Buy: 6474.5, Sell: 6475.0
2018-06-18 08:18:05,913 - INFO - market_maker - Start Positions: Buy: 6471.8, Sell: 6477.7, Mid: 6475.0
2018-06-18 08:18:05,913 - INFO - market_maker - Long delta limit exceeded
2018-06-18 08:18:05,914 - INFO - market_maker - Current Position: 1000, Maximum Position: 10
2018-06-18 08:18:05,914 - INFO - market_maker - Current XBT Balance: 1.228932
2018-06-18 08:18:05,914 - INFO - market_maker - Current Contract Position: 1000
2018-06-18 08:18:05,914 - INFO - market_maker - Position limits: -10/10
2018-06-18 08:18:05,915 - INFO - market_maker - Avg Cost Price: 6472.5
2018-06-18 08:18:05,915 - INFO - market_maker - Avg Entry Price: 6472.5
2018-06-18 08:18:05,915 - INFO - market_maker - Contracts Traded This Run: 0
2018-06-18 08:18:05,915 - INFO - market_maker - Total Contract Delta: 0.1547 XBT

가끔 socket.timeout으로 멋지게 죽습니다. 뭐, 샘플이므로 애교라고 하는 것으로.
2018-06-18 08:17:28,547 - INFO - bitmex - sending req to https://testnet.bitmex.com/api/v1/order/bulk: {"orders": [{"price": 6541.5, "orderQty": 1200, "side": "Sell", "clOrdID": "mm_bitmex_e+LjxgfeSkuTv5Qaj4hK0w", "symbol": "XBTUSD"}, {"price": 6509.0, "orderQty": 1100, "side": "Sell", "clOrdID": "mm_bitmex_J+wPHxWoTeSby/BRmHfdsg", "symbol": "XBTUSD"}, {"price": 6476.5, "orderQty": 1000, "side": "Sell", "clOrdID": "mm_bitmex_ZTSNv+l0TDWUeWmaDdXNKg", "symbol": "XBTUSD"}]}
2018-06-18 08:17:35,555 - WARNING - bitmex - Timed out on request: order/bulk ({"orders": [{"price": 6541.5, "orderQty": 1200, "side": "Sell", "clOrdID": "mm_bitmex_e+LjxgfeSkuTv5Qaj4hK0w", "symbol": "XBTUSD"}, {"price": 6509.0, "orderQty": 1100, "side": "Sell", "clOrdID": "mm_bitmex_J+wPHxWoTeSby/BRmHfdsg", "symbol": "XBTUSD"}, {"price": 6476.5, "orderQty": 1000, "side": "Sell", "clOrdID": "mm_bitmex_ZTSNv+l0TDWUeWmaDdXNKg", "symbol": "XBTUSD"}]}), retrying...
Traceback (most recent call last):
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 386, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 382, in _make_request
    httplib_response = conn.getresponse()
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/ssl.py", line 1009, in recv_into
    return self.read(nbytes, buffer)
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/ssl.py", line 871, in read
    return self._sslobj.read(len, buffer)
  File "/home/chicong_v/miniconda3/envs/bitmex/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

...

Exception: Max retries on order/bulk ({"orders": [{"price": 6541.5, "orderQty": 1200, "side": "Sell", "clOrdID": "mm_bitmex_e+LjxgfeSkuTv5Qaj4hK0w", "symbol": "XBTUSD"}, {"price": 6509.0, "orderQty": 1100, "side": "Sell", "clOrdID": "mm_bitmex_J+wPHxWoTeSby/BRmHfdsg", "symbol": "XBTUSD"}, {"price": 6476.5, "orderQty": 1000, "side": "Sell", "clOrdID": "mm_bitmex_ZTSNv+l0TDWUeWmaDdXNKg", "symbol": "XBTUSD"}]}) hit, raising.
2018-06-18 08:17:35,750 - INFO - market_maker - Shutting down. All open orders will be cancelled.
2018-06-18 08:17:35,750 - INFO - market_maker - Resetting current position. Canceling all existing orders.
2018-06-18 08:17:35,751 - INFO - bitmex - sending req to https://testnet.bitmex.com/api/v1/order: {"filter": "{\"ordStatus.isTerminated\": false, \"symbol\": \"XBTUSD\"}", "count": 500}
2018-06-18 08:17:37,430 - INFO - ws_thread - Websocket Closed

완전히 죽기 전에 모든 주문을 취소하려고합니다. 이것은 정상 작동 중 "Ctr-C"를 누르는 것과 같습니다.
-----
^C2018-06-18 08:23:23,680 - INFO - market_maker - Shutting down. All open orders will be cancelled.
2018-06-18 08:23:23,680 - INFO - market_maker - Resetting current position. Canceling all existing orders.
2018-06-18 08:23:23,680 - INFO - bitmex - sending req to https://testnet.bitmex.com/api/v1/order: {"filter": "{\"ordStatus.isTerminated\": false, \"symbol\": \"XBTUSD\"}", "count": 500}
2018-06-18 08:23:24,063 - INFO - market_maker - Canceling: Sell 1200 @ 6542.5
2018-06-18 08:23:24,064 - INFO - market_maker - Canceling: Sell 1100 @ 6510.0
2018-06-18 08:23:24,064 - INFO - market_maker - Canceling: Sell 1100 @ 6480.5
2018-06-18 08:23:24,064 - INFO - bitmex - sending req to https://testnet.bitmex.com/api/v1/order: {"orderID": ["f18f0705-d6e4-3a7d-682e-a0ff5a2f431f", "f78a672c-8c2a-d0a3-ebb9-5c491f672433", "4a35ba81-fce8-0e53-62c5-ec399dbaa6b0"]}
2018-06-18 08:23:25,664 - INFO - ws_thread - Websocket Closed

전망



부담없이 마켓 메이킹 기분을 맛볼 수 있었을까 생각합니다. 앞으로는
  • 장기적으로 실행하려면 VM이나 Container에 넣고 움직여 로그를 볼 수 있도록 합시다.
  • API Key를 환경 변수 등으로 안전하게 전달하고 싶네요.

  • 거래 전략을 설정해 봅시다.
  • 좋은 웹페이지 즐겨찾기