【问题标题】:How to extract individual and unique live trades from Bitmex Websocket API ws.recent_trades() log如何从 Bitmex Websocket API ws.recent_trades() 日志中提取个人和独特的实时交易
【发布时间】:2019-09-17 19:52:13
【问题描述】:

我正在尝试从 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]

并运行一个函数来对每笔交易执行一些计算。谢谢!

【问题讨论】:

    标签: python websocket quantitative-finance bitmex


    【解决方案1】:

    好问题 - 使用该库获取事件驱动的 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
    

    【讨论】:

    • 谢谢!如何单独提取时间戳、价格和大小?以及如何让 while 循环连续运行而不在 5 秒后退出?我试过不退出,但没有成功
    • @atjw94 刚刚编辑了答案以添加您想要的位。它在my_processing_func 中显示了一个示例,您将如何仅提取所需的价格或尺寸:)
    • @repoleved 谢谢! =) 您之前是否尝试过通过 websocket API 在 Bitmex 上执行交易?
    • @atjw94 执行您需要使用他们的 REST API 的交易
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 2022-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多