bitflyer websocket 受信

pyscrypter4.0にソースコードを貼り付けてRUN

少し経ってから、画面出力を

pprint(message) から

print(f'BTC {int(last1["price"]):,.0f} {last1["side"]:4} {last1["size"]:.2f}') に変更。

ソースコード

ビットフライヤーのリアルタイムAPI(websocket)からデータを連続的に受信するパイソンのソースコード。

#pip install websocket-client
import websocket
import json
from pprint import pprint

CHANNEL = "lightning_executions_BTC_JPY"

def on_message(ws, message):
    message = json.loads(message)
    pprint(message)
    if message["method"] == "channelMessage":
        last1=message["params"]["message"][0]
        #print(f'BTC {int(last1["price"]):,.0f} {last1["side"]:4} {last1["size"]:.2f}')
def on_open(ws):
    ws.send(json.dumps({"method": "subscribe",
                        "params": {"channel": CHANNEL}}))
if __name__ == "__main__":
    # note: reconnection handling needed.
    ws = websocket.WebSocketApp("wss://ws.lightstream.bitflyer.com/json-rpc",
                                on_message=on_message, on_open=on_open)
    ws.run_forever()

pprint(message) 出力結果

pprintは本来1行につながったデータを解析して、段付して見やすく出力してくれる、高機能printです。

{'jsonrpc': '2.0',
 'method': 'channelMessage',
 'params': {'channel': 'lightning_executions_BTC_JPY',
            'message': [{'buy_child_order_acceptance_id': 'JRF20210602-102115-049092',
                         'exec_date': '2021-06-02T10:21:15.7926115Z',
                         'id': 2221872514,
                         'price': 4085000.0,
                         'sell_child_order_acceptance_id': 'JRF20210602-102115-430118',
                         'side': 'BUY',
                         'size': 0.20002},
                        {'buy_child_order_acceptance_id': 'JRF20210602-102115-328113',
                         'exec_date': '2021-06-02T10:21:15.8426167Z',
                         'id': 2221872515,
                         'price': 4085000.0,
                         'sell_child_order_acceptance_id': 'JRF20210602-102115-430118',
                         'side': 'BUY',
                         'size': 0.21493708},
                        {'buy_child_order_acceptance_id': 'JRF20210602-102115-328112',
                         'exec_date': '2021-06-02T10:21:15.8559022Z',
                         'id': 2221872516,
                         'price': 4085000.0,
                         'sell_child_order_acceptance_id': 'JRF20210602-102115-430118',
                         'side': 'BUY',
                         'size': 0.0834915}]}}

print(f’BTC {int(last1[“price”]):,.0f} {last1[“side”]:4} {last1[“size”]:.2f}’) 出力結果

f’ fストリングは.formatと比べて、場所がそのまま、{}の中に、変数や式や関数を入れられるなど超便利

BTC  {}の中の文字はそのまま表示される

{int(last1[“price”]):,.0f} int(last1[“price”])という式の結果を、カンマ区切り小数点以下無しで出力

{last1[“side”]:4}  last1[“side”]の中身を4文字幅固定で出力

{last1[“size”]:.2f} last1[“size”]の中身を小数点以下2桁まで出力

BTC 4,075,975 SELL 0.01
BTC 4,075,975 BUY  0.02
BTC 4,075,975 BUY  0.18
BTC 4,075,975 BUY  0.08
BTC 4,078,041 BUY  0.13
BTC 4,078,408 SELL 0.12
BTC 4,077,656 BUY  0.08
BTC 4,077,656 BUY  0.13
BTC 4,078,939 BUY  0.02
BTC 4,078,939 BUY  0.17
BTC 4,078,939 BUY  0.03
BTC 4,079,361 BUY  0.10
BTC 4,079,361 BUY  0.01
BTC 4,081,764 BUY  0.07

コメント

タイトルとURLをコピーしました