如何从 websocket 客户端编译两个收盘价列表?

发布于 2025-01-10 14:22:56 字数 2067 浏览 1 评论 0原文

我正在使用 websockettalib 运行标准的买卖交易程序。使用套接字,我能够以不同的时间间隔(1 分钟和 3 分钟)获取 2 条消息。我仅获取 ETHUDS 数据,但使用此套接字有两个不同的时间间隔:

TRADE_SYMBOOL = 'ethusdt'
INTERVAL = '1m'
INTERVAL_2 = '3m'
SOCKET = f'wss://stream.binance.com:9443/ws/{TRADE_SYMBOOL}@kline_{INTERVAL}/{TRADE_SYMBOOL}@kline_{INTERVAL_2}'

这给了我一个 json.loads(message)

{'e': 'kline', 'E': 1646017123875, 's': 'ETHUSDT', 'k': {'t': 1646017080000, 'T': 1646017139999, 's': 'ETHUSDT', 'i': '1m', 'f': 769965188, 'L': 769965629, 'o': '2605.00000000', 'c': '2605.88000000', 'h': '2606.98000000', 'l': '2603.21000000', 'v': '191.57300000', 'n': 442, 'x': False, 'q': '499047.95132700', 'V': '78.57690000', 'Q': '204678.10094600', 'B': '0'}}
{'e': 'kline', 'E': 1646017123875, 's': 'ETHUSDT', 'k': {'t': 1646017020000, 'T': 1646017199999, 's': 'ETHUSDT', 'i': '3m', 'f': 769964266, 'L': 769965629, 'o': '2599.08000000', 'c': '2605.88000000', 'h': '2606.98000000', 'l': '2595.10000000', 'v': '922.85610000', 'n': 1364, 'x': False, 'q': '2399363.68094500', 'V': '356.83860000', 'Q': '928388.14101500', 'B': '0'}}

如果滚动一点,'i'< /code> 勾选在一个中显示 1m,在下一个中显示 3m

然后,我从该行中提取关闭 'c' 并编译关闭列表。

我想要做的是从 1m 列表中创建一个关闭列表,然后从 3m 列表中创建一个单独的列表。

closes = []
def on_message(ws, message):
    global in_position  
    json_message = json.loads(message)
    candle = json_message['k']  
    is_candle_closed = candle['x']
    close = candle['c']
    if is_candle_closed:  # this only returns True at the end of each candle close (1minute)
        closes.append(float(close))

这是我在只使用 1m 间隔时所使用的,但现在我不知道如何对每条消息传入的 2 个 json 负载进行排序。如何区分这两个 json 数据,以便相应地存储它们的收盘价?

我需要能够将 1m3m 蜡烛的收盘价存储为单独的列表。

我如何将关闭列表分成两个单独的列表,例如。 closes_1mcloses_3m 好吗?

我希望列表 closes_1m 包含每 1 分钟后的收盘价。

我希望列表 closes_3m 包含每 3 分钟后的收盘价格。

I am running a standard buy/sell trader using websocket and talib. With the socket, I am able to get 2 messages through with different time intervals (1min and 3min). I am getting ETHUDS data only but with two different time intervals using this socket:

TRADE_SYMBOOL = 'ethusdt'
INTERVAL = '1m'
INTERVAL_2 = '3m'
SOCKET = f'wss://stream.binance.com:9443/ws/{TRADE_SYMBOOL}@kline_{INTERVAL}/{TRADE_SYMBOOL}@kline_{INTERVAL_2}'

This gives me a json.loads(message) of:

{'e': 'kline', 'E': 1646017123875, 's': 'ETHUSDT', 'k': {'t': 1646017080000, 'T': 1646017139999, 's': 'ETHUSDT', 'i': '1m', 'f': 769965188, 'L': 769965629, 'o': '2605.00000000', 'c': '2605.88000000', 'h': '2606.98000000', 'l': '2603.21000000', 'v': '191.57300000', 'n': 442, 'x': False, 'q': '499047.95132700', 'V': '78.57690000', 'Q': '204678.10094600', 'B': '0'}}
{'e': 'kline', 'E': 1646017123875, 's': 'ETHUSDT', 'k': {'t': 1646017020000, 'T': 1646017199999, 's': 'ETHUSDT', 'i': '3m', 'f': 769964266, 'L': 769965629, 'o': '2599.08000000', 'c': '2605.88000000', 'h': '2606.98000000', 'l': '2595.10000000', 'v': '922.85610000', 'n': 1364, 'x': False, 'q': '2399363.68094500', 'V': '356.83860000', 'Q': '928388.14101500', 'B': '0'}}

If you scroll across a bit, the 'i' tick is showing 1m in one and 3m in the next.

I am then extracting the close 'c' from the line and compiling a list of closes.

What I want to be able to do is make a list of closes from the 1m list and then a separate list from the 3m list.

closes = []
def on_message(ws, message):
    global in_position  
    json_message = json.loads(message)
    candle = json_message['k']  
    is_candle_closed = candle['x']
    close = candle['c']
    if is_candle_closed:  # this only returns True at the end of each candle close (1minute)
        closes.append(float(close))

This is what I was using when I was only using 1m intervals, but now I don't know how to sort the 2 json loads that are coming in with each message. How can I differentiate between the two pieces of json data so I can store their closing prices accordingly?

I need to be able to store the closing prices from the 1m and 3m candles as separate lists.

How can I list closes into two separate list eg. closes_1m and closes_3m please?

I would like list closes_1m to contain the price of the close after every 1 minute.

I would like list closes_3m to contain the price of the close after every 3 minutes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2025-01-17 14:22:56

我可以用一个简单的 if 语句将它们分开。

closes_1m = []
closes_3m = []    
def on_message(ws, message):
        json_message = json.loads(message)
        candle = json_message['k']
        is_candle_closed = candle['x']
        close = candle['c']
        close_time = int(candle['T']) / 1000
        interval = candle['i']
        if interval == '1m' and is_candle_closed:  # this only runs at the end of each candle close (1minute)
            closes_1m.append(float(close))
        if interval == '3m' and is_candle_closed:  # this only runs at the end of each candle close (3minute)
        closes_3m.append(float(close))

因此,每 1 分钟我将收盘价数据附加到 closes_1m 中,每 3 分钟将收盘价数据附加到 closes_3m 中。

I was able to separate them with a simple if statement.

closes_1m = []
closes_3m = []    
def on_message(ws, message):
        json_message = json.loads(message)
        candle = json_message['k']
        is_candle_closed = candle['x']
        close = candle['c']
        close_time = int(candle['T']) / 1000
        interval = candle['i']
        if interval == '1m' and is_candle_closed:  # this only runs at the end of each candle close (1minute)
            closes_1m.append(float(close))
        if interval == '3m' and is_candle_closed:  # this only runs at the end of each candle close (3minute)
        closes_3m.append(float(close))

So every 1 minute I appended a closing price to closes_1m and every 3 minutes I appended the closing price data to closes_3m.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文