python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

Close one symbol stream from a multiplex socket

Open dikano opened this issue 8 years ago • 2 comments

Hi im making a bot to open , take positions and close trades based on a list im stuck at closing a stream if a trade is filled . is tried to use the trade socket iterating from the list like so :

for x in mysymbols:
    def process_message(msg):
        print(msg['p'], msg['s'])
        if float(msg['p']) >= float(x[1]):
            bm.stop_socket(conn_key)

    conn_key = bm.start_trade_socket(x[0], process_message)

    bm.start()

the problem above is that the stop_socket stops only the last symbol stream appended to [mysymbols] even if i use something like stop_socket('%s@trade' % msg['s'].lower()) after struggling with that for a week i decided to give the multiplex_socket a try im using this code :

bm = BinanceSocketManager(client)
started = False
scanlist_Binance = ['ethbtc@trade', 'eosbtc@trade']
def process_message(msg):
        search = (msg['data'])['s']
        price = float((msg['data'])['p'])
        print(msg)
        for i in mysymbols :
                        
                if i[1] == search and i[2] > price : 
                    #sell coins
                    bm.stop_socket(conn_key)

conn_key = bm.start_multiplex_socket(scanlist_Binance, process_message)
if not started:
                started = True
                time.sleep(5)
                bm.start()

the code above doesnt close the multiplex socket if i use bm.close() it closes all of it . is there a way to close only specific symbol on a condition thanks any help is appreciated

dikano avatar Sep 02 '18 17:09 dikano

You can close the socket. You can't remove streams. A multiplex socket is exactly that: a single socket with multiple streams coming through it. The way it's implemented by the Binance API is that you connect through a specific URI that lists the streams you want to connect to. In other words, it's not a pub/sub thing just a simple socket that happens to multiplex a bunch of different streams.

Instead of opening/closing sockets all the time, why not just open one with everything you may ever need, and then just ignore what's not important at the moment?

As a side note, it's not a python-binance issue. It's just that the Binance websocket API isn't designed the way you think it is or should've been. As such, I think you could close this issue.

wranai avatar Sep 02 '18 18:09 wranai

thanks for the reply

dikano avatar Sep 02 '18 18:09 dikano