BitMexのnodejs鯖を自鯖にインストールする手順メモ

BitMEX-Delta-Server の建て方

更新:2018年8月24日

APIの回数制限を気にすることなく、レート情報他を取得するために自鯖にBitMEX-Delta-Serverを建てて、好きなだけアクセスすることができます。

基本手順は下記に書かれてるが初めてインストールするときはこれだけでは情報が足りない。抜けてる部分を補てんするメモ。

インストール環境はグーグルGCE、ubuntu16.04

node.jsのインストール

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs

npmとその関連のインストール

sudo apt-get install npm

npm install lodash --save
npm install express  --save
npm install debug  --save

インストール終わったら、githubの手順に従って、もろもろを進めて、起動させてみる。ここでエラーが出なければ、インストールは成功している。

起動は次のとおり。

/home/userid/userfolder/api-connectors/official-ws/delta-server 下で

 node index.js &

次はグーグルのGCEのセキュリティ設定、gce-firewallの変更。設定はブラウザで行います。VPCネットワーク:ファイヤーウォール ルール

ネットワーク default
優先度 1000
送信 / 受信
下り (デフォルトは上りだけど、下りに変更した)
一致したときのアクション 許可
ターゲット フィルタ
IP 範囲 127.0.0.1
プロトコルとポート tcp:4444
適用 有効

これでインスタンスを再起動してから、次のコマンドで確認したところ、無事アクセスできるようになりました。config.jsファイルに埋め込んでるAPIキーは、とりあえずtestnetの情報を入力しています。(2018年8月24日時点でtestnet だとエラーになります。リアルだとOK)

curl "http://localhost:4444/instrument?symbol=XBTUSD"

1件表示されます。

 curl "http://localhost:4444/instrument?symbol=XBTUSD"
[
  {
    "symbol": "XBTUSD",
    "rootSymbol": "XBT",
    "state": "Open",
    "typ": "FFWCSX",
    "listing": "2016-05-04T12:00:00.000Z",
    "front": "2016-05-04T12:00:00.000Z",
    "expiry": null,
    "settle": null,
    "relistInterval": null,
    "inverseLeg": "",
    "sellLeg": "",
    "buyLeg": "",
    "optionStrikePcnt": null,
    "optionStrikeRound": null,
    "optionStrikePrice": null,
    "optionMultiplier": null,
    "positionCurrency": "USD",
    "underlying": "XBT",
    "quoteCurrency": "USD",
    "underlyingSymbol": "XBT=",
    "reference": "BMEX",
    "referenceSymbol": ".BXBT",
    "calcInterval": null,
    "publishInterval": null,
    "publishTime": null,
    "maxOrderQty": 10000000,
    "maxPrice": 1000000,
    "lotSize": 1,
    "tickSize": 0.5,
    "multiplier": -100000000,
    "settlCurrency": "XBt",
    "underlyingToPositionMultiplier": null,
    "underlyingToSettleMultiplier": -100000000,
    "quoteToSettleMultiplier": null,
    "isQuanto": false,
    "isInverse": true,
    "initMargin": 0.01,
    "maintMargin": 0.005,
    "riskLimit": 20000000000,
    "riskStep": 10000000000,
    "limit": null,
    "capped": false,
    "taxed": true,
    "deleverage": true,
    "makerFee": -0.00025,
    "takerFee": 0.00075,
    "settlementFee": 0,
    "insuranceFee": 0,
    "fundingBaseSymbol": ".XBTBON8H",
    "fundingQuoteSymbol": ".USDBON8H",
    "fundingPremiumSymbol": ".XBTUSDPI8H",
    "fundingTimestamp": "2018-07-05T12:00:00.000Z",
    "fundingInterval": "2000-01-01T08:00:00.000Z",
    "fundingRate": 0.0001,
    "indicativeFundingRate": 0.000359,
    "rebalanceTimestamp": null,
    "rebalanceInterval": null,
    "openingTimestamp": "2018-07-05T10:00:00.000Z",
    "closingTimestamp": "2018-07-05T12:00:00.000Z",
    "sessionInterval": "2000-01-01T02:00:00.000Z",
    "prevClosePrice": 6515.96,
    "limitDownPrice": null,
    "limitUpPrice": null,
    "bankruptLimitDownPrice": null,
    "bankruptLimitUpPrice": null,
    "prevTotalVolume": 52948679417,
    "totalVolume": 52960481910,
    "volume": 11802493,
    "volume24h": 237084128,
    "prevTotalTurnover": 744466184706335,
    "totalTurnover": 744644925832128,
    "turnover": 178741125793,
    "turnover24h": 3564059338956,
    "prevPrice24h": 6553,
    "vwap": 6652.4747,
    "highPrice": 6900,
    "lowPrice": 6509.5,
    "lastPrice": 6613.5,
    "lastPriceProtected": 6613.5,
    "lastTickDirection": "ZeroMinusTick",
    "lastChangePcnt": 0.0092,
    "bidPrice": 6613.5,
    "midPrice": 6613.75,
    "askPrice": 6614,
    "impactBidPrice": 6609.3853,
    "impactMidPrice": 6615.25,
    "impactAskPrice": 6621.2011,
    "hasLiquidity": true,
    "openInterest": 187136033,
    "openValue": 2818455793013,
    "fairMethod": "FundingRate",
    "fairBasisRate": 0.1095,
    "fairBasis": 0.05,
    "fairPrice": 6639.66,
    "markMethod": "FairPrice",
    "markPrice": 6639.66,
    "indicativeTaxRate": 0,
    "indicativeSettlePrice": 6639.61,
    "optionUnderlyingPrice": null,
    "settledPrice": null,
    "timestamp": "2018-07-05T11:21:35.000Z"
  }

次のコマンドだと

curl "http://localhost:4444/quote?symbol=XBTUSD"

こんなかんじのがずらずらと。 9898件出ました。headとtailコマンドで抜いてみたところ
先頭データ "timestamp": "2018-08-23T15:11:30.668Z",
末尾データ "timestamp": "2018-08-23T15:22:39.967Z",
約11分間のデータが出力されました。

  {
    "timestamp": "2018-07-05T12:21:17.460Z",
    "symbol": "XBTUSD",
    "bidSize": 1080,
    "bidPrice": 6599.5,
    "askPrice": 6600,
    "askSize": 5273
  },
  {
    "timestamp": "2018-07-05T12:21:17.571Z",
    "symbol": "XBTUSD",
    "bidSize": 1080,
    "bidPrice": 6599.5,
    "askPrice": 6600,
    "askSize": 7273
  },
  {
    "timestamp": "2018-07-05T12:21:17.638Z",
    "symbol": "XBTUSD",
    "bidSize": 1080,
    "bidPrice": 6599.5,
    "askPrice": 6600,
    "askSize": 17273
  }

/trade に変えると

 curl "http://localhost:4444/trade?symbol=XBTUSD"

こんな結果がずらずらと。 8896件でました。headとtailコマンドで抜いてみたところ
先頭データ "timestamp": "2018-08-23T15:11:30.668Z",
末尾データ "timestamp": "2018-08-23T15:20:27.584Z",
約9分間のデータが出力されました。

  {
    "timestamp": "2018-07-05T12:24:34.829Z",
    "symbol": "XBTUSD",
    "side": "Buy",
    "size": 1000,
    "price": 6599.5,
    "tickDirection": "ZeroPlusTick",
    "trdMatchID": "26437f9f-c290-855a-115b-864a12430624",
    "grossValue": 15153000,
    "homeNotional": 0.15153,
    "foreignNotional": 1000
  },
  {
    "timestamp": "2018-07-05T12:24:34.894Z",
    "symbol": "XBTUSD",
    "side": "Buy",
    "size": 14,
    "price": 6599.5,
    "tickDirection": "ZeroPlusTick",
    "trdMatchID": "fd01608e-bfd5-53ac-3b06-5bae254f8814",
    "grossValue": 212142,
    "homeNotional": 0.00212142,
    "foreignNotional": 14
  }

config.jsファイルの設定の次の行が件数に影響しているようです。調べてないのでカンです。

// This prevents memory usage from getting out of control. Tweak to your needs.
  maxTableLen: 10000,

GCEのvCPU1個環境(f1-smallとかの仮想CPU1個ではなく、物理CPU1個のイメージ)で実行し、topコマンドを走らせてみました。意外とCPUを喰っています。少ない時で4%、平均10%弱、多い時で15%前後。

2行目以降のpythonの各行はMANAコミニュティで公開されているBitMEXUtility3のOrderbookDBツールです。

稼働後しばらく経過しての、ブラウザから見たCPU使用率がこちら。5%はOrderbookDBツールが使ってるので、node.js鯖で使ってるのは5から25%になります。

config.jsファイルのパラメタ、 maxTableLen: 10000 を変更してみました。

1桁落として1000だとエラーメッセージが流れてダメです、半分の5000だとリコネクトメッセージがでた後は安定しているようです。topコマンドで動きを見ると前回のパラメタよりCPU使用率は低いようです。

負荷率軽減よりも、ETHUSDのデータも取りたい。config.jsファイルのシンボル設定を変えてみました。

 symbols: ['XBTUSD','ETHUSD'],

これで動きましたが、maxTableLen:は5000だと時折エラーがでるので元の10000に戻しました。

約半日経過でのVPSはビジー状態になってしまってSSHリモートログインできない状態でした。メモリ1GBでは2つのペアを処理できないのかもしれません。メモリ2GBに増やして再起動してみました。数時間後確認したら無事動いてました。メモリが少なかったのが原因のようです。

しかし同時に動かしていたOrderbookDBツールはwebsokectエラー連発、同じ口座のキーを使っていたせいでしょうか。口座を変えて、node.jsを止めたら正常処理するようになりました。

nodejs鯖はVPSでリソースの少ない動かすには厳しいことがわかったので今後は自宅PCで動かす限定にしてみます。


チップはこちらからお願いします