0
Follow
2
Followers
用FMZ快2个月了,跑了几个策略收益还不错,觉得有必要为社区做点小贡献,看了下现在Websocket的例子还不多,贡献一下订阅多stream获取多币种Websocket行情的例子,比较简单,获取行情,循环里打印行情的json。
用在多币种对冲套利类的策略里比rest的接口反应更快,如果同时交易的币多的话,差异还是挺大的,希望能有帮助。
python
#websocket 更新 行情
# {
# "e":"bookTicker", // 事件类型
# "u":400900217, // 更新ID
# "E": 1568014460893, // 事件推送时间
# "T": 1568014460891, // 撮合时间
# "s":"BNBUSDT", // 交易对
# "b":"25.35190000", // 买单最优挂单价格
# "B":"31.21000000", // 买单最优挂单数量
# "a":"25.36520000", // 卖单最优挂单价格
# "A":"40.66000000" // 卖单最优挂单数量
# }
def on_msg(msg) : #更新行情
if msg is not None and len(msg)>0:
bookTicker = json.loads(msg)
else:
# Log('book tick msg is none')
return
Log(bookTicker)
def main():
SetErrorFilter("502:|503:|tcp|character|unexpected|network|timeout|WSARecv|Connect|GetAddr|no such|reset|http|received|EOF|reused|Unknown")
trade_symbols = 'TRX,ZEC,DENT,BLZ,ENJ,ZIL,MANA,ONT,XMR,ICX,SC,THETA,CVC,BAT,STMX,VET,IOST,NEO,MTL,DASH,KNC,ZRX,IOTA'.split(',')
ary_symbol_streams = []
for i in range(len(trade_symbols)):
symbol = trade_symbols[i].lower()
stream_client = Dial(f"wss://fstream.binance.com/ws/{symbol}usdt@bookTicker|reconnect=true")
ary_symbol_streams.append(stream_client)
while (true):
for item in ary_symbol_streams:
#-2读取最新数据
msg = item.read(-2)
on_msg(msg)
Related Recommendations
How to Specify Different Versions of Data for the Rented Strategy by Its Rental Code MetadataAdvanced Tutorial for FMZ Quant platform Strategy WritingElementary Tutorial for FMZ Quant platform Strategy WritingGet Started with FMZ Quant PlatformSECURITY BUGI keep getting error: Exchange_GetAccount: Invalid ContractTypeWe have an incredibly profitable market making algorithm for sideways markets on Bitmex - but need expert to help eliminate wait times during downward volatility in the marketError with deribitLimitations of the backtesting engineHow to install ta-lib on linux docker?
Comment
All comments (4)
Dial("wss://stream.binance.com:9443/stream?streams=btcusdt@aggTrade/ethusdt@aggTrade/axsusdt@aggTrade/ltcusdt@aggTrade/dogeusdt@aggTrade|reconnect=true");
5 years ago
- 1

