Python 如何从Bitmex Websocket API ws.recent_trades()日志中提取单个和唯一的实时交易
我正在尝试从Bitmex流式传输实时交易数据,以执行一些计算并自动化我的交易。我使用了从中获得的以下代码,我的代码如下:Python 如何从Bitmex Websocket API ws.recent_trades()日志中提取单个和唯一的实时交易,python,websocket,quantitative-finance,bitmex,Python,Websocket,Quantitative Finance,Bitmex,我正在尝试从Bitmex流式传输实时交易数据,以执行一些计算并自动化我的交易。我使用了从中获得的以下代码,我的代码如下: 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 ad
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},
我如何将它们拆分为单独的交易?我的愿望输出将是列表中的每一笔交易:
[时间戳、价格、数量、侧面]
并运行一个函数,对每笔交易执行一些计算。谢谢大家! 好问题-似乎没有一种明显的方法可以使用该库获取事件驱动的websocket消息(即使您尝试只订阅
trade
频道!)
鉴于您当前的代码,这是最简单的解决方案。它会记录你的上一笔交易,只有在发生变化时才会记录
从bitmex\u websocket导入BitMEXWebsocket
导入日志记录
导入时间
#websocket的基本用法。
def run():
记录器=设置程序\记录器()
#实例化WS将使其连接。请确保添加您的api_密钥/api_密钥。
ws=BitMEXWebsocket(端点=”https://testnet.bitmex.com/api/v1,symbol=“XBTUSD”,
api_key=api_key,api_secret=api_secret)
logger.info(“仪器数据:%s”%ws.get_Instrument())
#长跑
开始时间=time.time()
贸易=[]
而(ws.ws.sock.connected):
最近的=ws.recent_trades()[-1]#只进行最后(最近)一次交易
如果不是len(交易)或交易[-1]!=最近:
#只存储第一笔交易或更改的交易
logger.info(“交易:%s”%recent)
交易。追加(最近)
#您可以在这里调用其他一些有用的函数
其他:
logger.info('未更改')
#下面建议您稍微放慢您的投票速度
睡眠时间(0.05)
#下面是在5秒后退出循环并打印交易
如果time.time()>开始时间+5:
印刷品(贸易)
打破
输出:
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}
]
希望这有帮助
编辑:
要提取您想要的信息,只需执行以下操作:
#永远运行
开始时间=time.time()
贸易=[]
而(ws.ws.sock.connected):
最近的=ws.recent_trades()[-1]#只进行最后(最近)一次交易
如果不是len(交易)或交易[-1]!=最近:
#只存储第一笔交易或更改的交易
有用={
“时间”:最近[“时间戳”],
“价格”:最近的[“价格”],
“大小”:最近的[“大小”],
}
logger.info(“交易:%s”%s)
trades.append(有用)
我的加工职能(交易)
#下面建议您稍微放慢您的投票速度
睡眠时间(0.05)
定义我的处理职能(交易):
#这被称为每一个新的交易,并给你所有过去的交易
#您可能希望将它们全部写入一个文件,或者只传递最近的交易
just_size=[t[“size”]表示交易中的t]
如果要让它连续运行,请删除我添加的块:
#下面是在5秒后退出循环并打印交易
如果time.time()>开始时间+5:
印刷品(贸易)
打破
谢谢!如何分别提取时间戳、价格和尺寸?我怎样才能让while循环连续运行而不在5秒后退出?我尝试过不退出,但没有成功work@atjw94只需编辑答案即可添加所需的位。它显示了my_processing\u func
中的一个示例,说明了如何提取所需的价格或大小:)@repoleved谢谢!=)您以前是否尝试过通过websocket API在Bitmex上执行交易?@atjw94要执行交易,您需要使用他们的REST API