简体   繁体   English

如何处理来自币安websocket的多个stream数据?

[英]How to process multiple stream data from binance websocket?

I am using the unicorn_binance_websocket_api to stream price data for 100 cryptos and from 2 different timeframes, I want to process this data to store the close prices of the different cryptos respective to their timeframes and then execute my strategy to see which crypto and timeframe i need to trade我正在使用 unicorn_binance_websocket_api 到 stream 价格数据,用于 100 个加密货币和 2 个不同的时间范围,我想处理这些数据以存储不同加密货币各自时间范围的收盘价,然后执行我的策略以查看我需要哪个加密货币和时间范围交易

I'll share the code as to how I was able to code my strategy for a single crypto and single timeframe我将分享关于我如何为单一加密货币和单一时间框架编写策略的代码

from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import
BinanceWebSocketApiManager
import json, numpy, talib

binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com-futures")

binance_websocket_api_manager.create_stream('kline_1m', 'btcusdt')


closes =[]

RSI_PERIOD = 14
RSI_OVERBOUGHT = 70
RSI_OVERSOLD = 30

while True:
received_stream_data_json = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if received_stream_data_json:
    json_data = json.loads(received_stream_data_json)
    candle_data = json_data.get('data',{})
    candle = candle_data.get('k', {})

    symboll = candle.get('s',{})
    timeframe = candle.get('i',{})
    close_prices = candle.get('c',{})
    open_prices = candle.get('o',{})
    is_candle_closed = candle.get('x',{})

    if is_candle_closed:
        closes.append(float(close_prices))

    if len(closes) > RSI_PERIOD:
        np_closes = numpy.array(closes)
        rsi = talib.RSI(np_closes,RSI_PERIOD)
        
    if (rsi[-1] > RSI_OVERBOUGHT):
        print("SELL")

    elif (rsi[-1] < RSI_OVERSOLD):
        print('BUY')

You simply use the subscribe_to_stream function and append the additional channels and markets that you want to watch.您只需使用subscribe_to_stream function 和 append 您想观看的其他频道和市场。 I was trying to write this manually via the python-binance library and it seemed gross, hacky and ineffecient.我试图通过 python-binance 库手动编写它,但它看起来很粗糙、笨拙且效率低下。 So i found your question and decided to use this unicorn lib instead, and i gotta say, it's pretty awesome.所以我发现了你的问题并决定改用这个独角兽库,我得说,它非常棒。 Here is my solution, you don't need to use asyncio btw这是我的解决方案,您不需要使用 asyncio btw

   class BinanceWs:

    def __init__(self, channels, markets):
        market = 'btcusdt'
        tf = 'kline_1w'
        self.binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com-futures")
        stream = self.binance_websocket_api_manager.create_stream(tf, market)
        self.binance_websocket_api_manager.subscribe_to_stream(stream, channels, markets)

    async def run(self):
        while True:
            received_stream_data_json = self.binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
            if received_stream_data_json:
                json_data = json.loads(received_stream_data_json)
                candle_data = json_data.get('data', {})

                candle = candle_data.get('k', {})
                symbol = candle.get('s', {})
                timeframe = candle.get('i', {})
                close_prices = candle.get('c', {})
                open_prices = candle.get('o', {})
                is_candle_closed = candle.get('x', {})
                print(candle_data)
                # do stuff with data ... 

async def main():
    tasks = []

    channels = ['kline_1m', 'kline_5m', 'kline_15m', 'kline_30m', 'kline_1h', 'kline_12h', 'miniTicker']
    markets = {'btcusdt', 'ethusdt', 'ltcusdt'}

    print(f'Main starting streams ... ')
    kl_socket = BinanceWs(channels=channels, markets=markets)
    task = await kl_socket.run()
    tasks.append(task)
    print(tasks)
    await asyncio.gather(*tasks)


if __name__ == "__main__":
    asyncio.run(main())

How can we get 80+ or 100+ streams this way?我们如何才能以这种方式获得 80 多个或 100 多个流?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM