簡體   English   中英

如何從Bitmex Websocket API ws.recent_trades()日志中提取單個和唯一的實時交易

[英]How to extract individual and unique live trades from Bitmex Websocket API ws.recent_trades() log

我正在嘗試從Bitmex流式傳輸實時交易數據以執行一些計算並使我的交易自動化。 我使用了從https://github.com/BitMEX/api-connectors/blob/master/official-ws/python/main.py獲得的以下代碼,我的代碼如下:

from bitmex_websocket import BitMEXWebsocket
import logging

# Basic use of websocket.
def run():
    logger = setup_logger()

    # Instantiating the WS will make it connect. Be sure to add your api_key/api_secret.
    ws = BitMEXWebsocket(endpoint="https://testnet.bitmex.com/api/v1", symbol="XBTUSD",
                         api_key=api_key, api_secret=api_secret)

    logger.info("Instrument data: %s" % ws.get_instrument())

    # Run forever
    while(ws.ws.sock.connected):
        # CODE TO STREAM THE LIVE TRADE
        logger.info("Recent Trades: %s\n\n" % ws.recent_trades())

def setup_logger():
    # Prints logger info to terminal
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)  # Change this to DEBUG if you want a lot more info
    ch = logging.StreamHandler()
    # create formatter
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    # add formatter to ch
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    return logger

if __name__ == "__main__":
    run()

這是我獲得的輸出:

2019-09-08 02:35:35,220 - root - INFO - Recent Trades: [{'timestamp': '2019-09-07T18:35:21.333Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 100, 'price': 10483, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': 'b5f7a502-9d28-5139-19e3-b713f7d86426', 'grossValue': 953900, 'homeNotional': 0.009539, 'foreignNotional': 100}]


2019-09-08 02:35:35,221 - root - INFO - Recent Trades: [{'timestamp': '2019-09-07T18:35:21.333Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 100, 'price': 10483, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': 'b5f7a502-9d28-5139-19e3-b713f7d86426', 'grossValue': 953900, 'homeNotional': 0.009539, 'foreignNotional': 100}]


2019-09-08 02:35:35,222 - root - INFO - Recent Trades: [{'timestamp': '2019-09-07T18:35:21.333Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 100, 'price': 10483, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': 'b5f7a502-9d28-5139-19e3-b713f7d86426', 'grossValue': 953900, 'homeNotional': 0.009539, 'foreignNotional': 100}]

如您所見,所有3個輸出都用於同一筆交易,我如何獲取代碼以僅打印發生的實時獨特交易而不能連續打印同一筆交易?

有時,我會獲得很長的數據流,每個數據流包含一些交易,例如:

2019-09-08 02:36:03,539 - root - INFO - Recent Trades: [{'timestamp': '2019-09-07T18:35:21.333Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 100, 'price': 10483, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': 'b5f7a502-9d28-5139-19e3-b713f7d86426', 'grossValue': 953900, 'homeNotional': 0.009539, 'foreignNotional': 100}, {'timestamp': '2019-09-07T18:36:04.168Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 1485, 'price': 10483, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': '8edc4253-85fa-dc93-23e4-3394be2153cc', 'grossValue': 14165415, 'homeNotional': 0.14165415, 'foreignNotional': 1485}, {'timestamp': '2019-09-07T18:36:04.168Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 10, 'price': 10483, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': '09bea4d1-14e2-86af-7152-38d468e7fbed', 'grossValue': 95390, 'homeNotional': 0.0009539, 'foreignNotional': 10},

我如何將它們拆分為個人交易? 我期望的輸出將是列表中的每個交易: [timestamp, price, qty, side]

並運行一個函數對每筆交易進行一些計算。 謝謝!

好問題-似乎沒有一種明顯的方法可以使用該庫獲取事件驅動的Websocket消息(即使您嘗試僅訂閱trade渠道也是如此!)

根據您當前的代碼,這是最簡單的解決方案。 它會記錄您的上一筆交易,並且僅在發生更改時才記錄日志。

from bitmex_websocket import BitMEXWebsocket
import logging
import time

# Basic use of websocket.
def run():
    logger = setup_logger()

    # Instantiating the WS will make it connect. Be sure to add your api_key/api_secret.
    ws = BitMEXWebsocket(endpoint="https://testnet.bitmex.com/api/v1", symbol="XBTUSD",
                         api_key=api_key, api_secret=api_secret)

    logger.info("Instrument data: %s" % ws.get_instrument())

    # Run forever
    start_time = time.time()
    trades = []

    while(ws.ws.sock.connected):
        recent = ws.recent_trades()[-1] # take only last (most recent) trade

        if not len(trades) or trades[-1] != recent:
            # only store first trade or changed ones
            logger.info("Trade: %s" % recent)
            trades.append(recent)
            # you could call some other useful function here
        else:
            logger.info('Unchanged')

        # below recommended to slow down your poll slightly
        time.sleep(0.05)

        # below is just to exit loop after 5 secs and print the trades
        if time.time() > start_time + 5:
            print(trades)
            break

輸出:

2019-09-11 23:57:28,601 - bitmex_websocket - INFO - Connected to WS.
2019-09-11 23:57:30,139 - bitmex_websocket - INFO - Got all market data. Starting.
2019-09-11 23:57:30,140 - root - INFO - Instrument data: {'symbol': 'XBTUSD', ...
2019-09-11 23:57:30,142 - root - INFO - Trade: {'timestamp': '2019-09-11T22:57:29.880Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 159, 'price': 10147, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': 'cc39257d-dc11-5b90-a0cc-ebabe7b6d104', 'grossValue': 1566945, 'homeNotional': 0.01566945, 'foreignNotional': 159}
2019-09-11 23:57:30,196 - root - INFO - Unchanged
2019-09-11 23:57:30,248 - root - INFO - Unchanged
2019-09-11 23:57:30,299 - root - INFO - Unchanged
2019-09-11 23:57:34,614 - root - INFO - Unchanged
2019-09-11 23:57:34,683 - root - INFO - Trade: {'timestamp': '2019-09-11T22:57:33.298Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 145, 'price': 10147, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': '3f12ae9f-371e-6261-3380-456b3c0a3c06', 'grossValue': 1428975, 'homeNotional': 0.01428975, 'foreignNotional': 145}
2019-09-11 23:57:34,752 - root - INFO - Unchanged

[
    {'timestamp': '2019-09-11T22:57:29.880Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 159, 'price': 10147, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': 'cc39257d-dc11-5b90-a0cc-ebabe7b6d104', 'grossValue': 1566945, 'homeNotional': 0.01566945, 'foreignNotional': 159}, 
    {'timestamp': '2019-09-11T22:57:33.298Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 145, 'price': 10147, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': '3f12ae9f-371e-6261-3380-456b3c0a3c06', 'grossValue': 1428975, 'homeNotional': 0.01428975, 'foreignNotional': 145}
]

希望這可以幫助!

編輯:要僅提取所需的信息,只需執行以下操作:

    # Run forever
    start_time = time.time()
    trades = []

    while(ws.ws.sock.connected):
        recent = ws.recent_trades()[-1] # take only last (most recent) trade

        if not len(trades) or trades[-1] != recent:
            # only store first trade or changed ones
            useful = {
                "time": recent["timestamp"],
                "price": recent["price"],
                "size": recent["size"],
            }
            logger.info("Trade: %s" % useful)
            trades.append(useful)

            my_processing_func(trades) 

        # below recommended to slow down your poll slightly
        time.sleep(0.05)

def my_processing_func(trades):
    # This gets called every new trade, and gives you all past trades
    # You may want to write them all to a file or just pass the most recent trade

    just_sizes = [t["size"] for t in trades]

如果要使其連續運行,請刪除我添加的塊:

        # below is just to exit loop after 5 secs and print the trades
        if time.time() > start_time + 5:
            print(trades)
            break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM