Examples of Websocket access to the market for Binance - Python

Author: scottliyq, Created: 2021-06-15 20:16:47, Updated:

I've been using FMZ for almost 2 months, and I've run a few strategies that have been very good, I feel it is necessary to make a small contribution to the community, looking at the following examples of Websocket, donate an example of subscribing to multi-stream access to multi-currency Websocket market, more simple, access to market, printing market in the loop json.

Interface reactions are faster than rest in multi-currency hedging strategies, and the difference is significant if multiple currencies are being traded at the same time, hopefully helping.

#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)



More

Difficult to quantify Dial("wss://stream.binance.com:9443/stream?streams=btcusdt@aggTrade/ethusdt@aggTrade/axsusdt@aggTrade/ltcusdt@aggTrade/dogeusdt@aggTrade|reconnect=true");

scottliyqIt was written in this way to begin with, and it was convenient, but the tick of all the coins was in a queue, which was not a good fit for my own strategy.

The grassWSS subscriptions can be started with a URL, and/or connection in the middle.

WusanAsk Da Da: Is there a simple way for Bin An to get the price depth of over 100 listings in real time?