bitFlyerの新しいRealtime APIの利用法
Pythonサンプル
code: websocket.py
# -*- coding: utf-8 -*-
import json
import websocket
def on_message(ws, message):
print('{product_code} {timestamp} bid {best_bid}({best_bid_size:.1f}) ask {best_ask}({best_ask_size:.1f}) ltp {ltp} volume {volume:.1f}'.format(**r))
def on_error(ws, error):
print(error)
def on_close(ws):
def on_open(ws):
ws.send(json.dumps({
'method': 'subscribe',
'params': {'channel': 'lightning_ticker_BTC_JPY'},
}))
if __name__ == "__main__":
ws = websocket.WebSocketApp("wss://ws.lightstream.bitflyer.com/json-rpc",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever()
以下のような出力が得られる。
code: result.txt
BTC_JPY 2018-04-26T13:48:03.0979725Z bid 967911(0.0) ask 968075(0.0) ltp 967940 volume 29799.2
BTC_JPY 2018-04-26T13:48:03.1761241Z bid 967911(0.0) ask 968075(0.0) ltp 967940 volume 29799.2
BTC_JPY 2018-04-26T13:48:03.2854791Z bid 967911(0.0) ask 968075(0.0) ltp 967940 volume 29798.8
BTC_JPY 2018-04-26T13:48:03.4261233Z bid 967911(0.0) ask 968075(0.0) ltp 967940 volume 29798.8
BTC_JPY 2018-04-26T13:48:03.5198919Z bid 967911(0.0) ask 968075(0.0) ltp 967940 volume 29798.8
BTC_JPY 2018-04-26T13:48:03.8636136Z bid 967911(0.0) ask 968075(0.0) ltp 967940 volume 29798.8