2

I've done binance websocket class collecting candles for several crypto pairs at once using start_multiplex_socket. When candles for particular crypto pair come, I do some traiding algorithm for this pair by running class instance function for this pair. Algorithm has some pauses as the trading logic says. The problem is that when a delay occurs in particular crypto pair instance function, it causes a delay for other instances also. How can I run them independantly? Thank you in advance!

    import time
    from binance.websockets import BinanceSocketManager
    from binance.client import Client
    from levels import TradeSignals as Levels
    from teleg import sendtext_me
    
    
    api_key = ''
    api_secret = ''
    client = Client(api_key, api_secret)
    bm = BinanceSocketManager(client, user_timeout=60)
    
    """
    TradeSignals structure: 
    __init__(self, ticker, candle, commission, entry_amount, lots_left)
    """
    class BinanceTrade(Levels):
        
        def __init__(self, dic):
            self.streams = []
            self.trades = {}
            
            for ticker, params in dic.items():
                super().__init__(ticker, params[0], params[1], params[2], params[3])
                self.trades.update({Levels(ticker, params[0], params[1], params[2], params[3]): ticker})
                candle = params[0]
                self.streams.append(f'{ticker.lower()}@kline_{candle}')
            
            self.connect()
            
        
        def connect(self):
            bm.start_multiplex_socket(self.streams, self.process_message)
    
    
        def process_message(self, msg):
            if msg['data']['e'] == 'error':
                self.close_ws()
            else:
                self.run(msg['data'])
                
                
        def run(self, msg):
            for instance, ticker in self.trades.items():
                if ticker == msg['s']:
                    instance.get_signals(msg)
    
    
        def close_ws(self):
            bm.close()
            sendtext_me(f'Binance websocket is closed')
            time.sleep(10)
            self.start_ws()
        
        
        def start_ws(self):
            sendtext_me(f'Run Binance websocket again')
            self.connect()
            
    
    if __name__ == '__main__':
        
        trade = BinanceTrade({
            'ETHUSDT':   ['1h', 0.00075, 100, 0.01],
            'BNBUSDT':   ['1h', 0.00075, 100, 3.00],
            'EOSUSDT':   ['1h', 0.00075, 100, 0.01],
            'LTCUSDT':   ['1h', 0.00075, 100, 0.01],
            'ZECUSDT':   ['1h', 0.00075, 100, 0.01],
            'NEOUSDT':   ['1h', 0.00075, 100, 0.01],
            'BCHUSDT':   ['1h', 0.00075, 100, 0.01],
            'LINKUSDT':  ['1h', 0.00075, 100, 0.01],
            'SUSHIUSDT': ['1h', 0.00075, 100, 0.01]})
        
        bm.start()
        
  • hey, did u managed to use multipairs? – aleXela Jun 01 '22 at 23:06
  • @aleXela Yes, this task was done in this question: https://stackoverflow.com/questions/66205387/how-to-run-websockets-independently I have created a binance manager and a queue for each thread – Ruslan Asadullin Jun 02 '22 at 07:36

0 Answers0