如何从 websocket 客户端编译两个收盘价列表?
我正在使用 websocket
和 talib
运行标准的买卖交易程序。使用套接字,我能够以不同的时间间隔(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 数据,以便相应地存储它们的收盘价?
我需要能够将 1m
和 3m
蜡烛的收盘价存储为单独的列表。
我如何将关闭列表分成两个单独的列表,例如。 closes_1m
和 closes_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以用一个简单的
if
语句将它们分开。因此,每 1 分钟我将收盘价数据附加到
closes_1m
中,每 3 分钟将收盘价数据附加到closes_3m
中。I was able to separate them with a simple
if
statement.So every 1 minute I appended a closing price to
closes_1m
and every 3 minutes I appended the closing price data tocloses_3m
.