提问人:party911 提问时间:10/24/2023 最后编辑:party911 更新时间:10/24/2023 访问量:26
阻止在 Python 中从 Steam 数据中附加值
blocking to append values from steam data in python
问:
我正在机器人上工作,只是为了处理来自 Binance Websocket 的值,并且我遇到了异步 python 中append_values的问题。当我通过键将值附加到 self.candlesticks = {} 时,键值的一些数据会丢失。下面是输出示例
{'LTCUSDT_1m/10_2': [{'open': Decimal('70.97'), 'high': Decimal('71.15'), 'low': Decimal('70.97'), 'close': Decimal('71.09'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:34:59'}, {'open': Decimal('71.08'), 'high': Decimal('71.18'), 'low': Decimal('71.03'), 'close': Decimal('71.04'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:35:59'}, {'open': Decimal('71.03'), 'high': Decimal('71.08'), 'low': Decimal('71'), 'close': Decimal('71.08'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:36:59'}, {'open': Decimal('71.09'), 'high': Decimal('71.10'), 'low': Decimal('71.03'), 'close': Decimal('71.09'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:37:59'}, {'open': Decimal('71.09'), 'high': Decimal('71.16'), 'low': Decimal('71.08'), 'close': Decimal('71.11'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:38:59'}, {'open': Decimal('71.12'), 'high': Decimal('71.27'), 'low': Decimal('71.12'), 'close': Decimal('71.21'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:39:59'}, {'open': Decimal('71.21'), 'high': Decimal('71.23'), 'low': Decimal('71.16'), 'close': Decimal('71.23'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:40:59'}, {'open': Decimal('71.23'), 'high': Decimal('71.29'), 'low': Decimal('71.23'), 'close': Decimal('71.25'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:41:59'}, {'open': Decimal('71.25'), 'high': Decimal('71.40'), 'low': Decimal('71.21'), 'close': Decimal('71.23'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:42:59'}, {'open': Decimal('71.23'), 'high': Decimal('71.30'), 'low': Decimal('71.21'), 'close': Decimal('71.25'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:43:59'}],
'XRPUSDT_1m/10_2': [{'open': Decimal('0.5809'), 'high': Decimal('0.5809'), 'low': Decimal('0.5775'), 'close': Decimal('0.5798'), 'ma': Decimal('0.5809599999999999209165935099008493125438690185546875'), 'close_time': '2023-10-24 12:46:59'}, {'open': Decimal('0.5798'), 'high': Decimal('0.5817'), 'low': Decimal('0.5796'), 'close': Decimal('0.5808'), 'ma': Decimal('0.58104999999999995541344333105371333658695220947265625'), 'close_time': '2023-10-24 12:47:59'}, {'open': Decimal('0.5808'), 'high': Decimal('0.5820'), 'low': Decimal('0.5804'), 'close': Decimal('0.5808'), 'ma': Decimal('0.581069999999999975415221342700533568859100341796875'), 'close_time': '2023-10-24 12:48:59'}, {'open': Decimal('0.5808'), 'high': Decimal('0.5843'), 'low': Decimal('0.5806'), 'close': Decimal('0.5842'), 'ma': Decimal('0.58150000000000001687538997430237941443920135498046875'), 'close_time': '2023-10-24 12:49:59'}, {'open': Decimal('0.5842'), 'high': Decimal('0.5868'), 'low': Decimal('0.5814'), 'close': Decimal('0.5823'), 'ma': Decimal('0.5815200000000000368771679859491996467113494873046875'), 'close_time': '2023-10-24 12:50:59'}, {'open': Decimal('0.5823'), 'high': Decimal('0.5825'), 'low': Decimal('0.5800'), 'close': Decimal('0.5807'), 'ma': Decimal('0.5815799999999999858601995583740063011646270751953125'), 'close_time': '2023-10-24 12:51:59'}, {'open': Decimal('0.5808'), 'high': Decimal('0.5812'), 'low': Decimal('0.5798'), 'close': Decimal('0.5807'), 'ma': Decimal('0.58168000000000008586908961660810746252536773681640625'), 'close_time': '2023-10-24 12:52:59'}, {'open': Decimal('0.5806'), 'high': Decimal('0.5806'), 'low': Decimal('0.5759'), 'close': Decimal('0.5770'), 'ma': Decimal('0.58108999999999999541699935434735380113124847412109375'), 'close_time': '2023-10-24 12:53:59'}, {'open': Decimal('0.5769'), 'high': Decimal('0.5780'), 'low': Decimal('0.5627'), 'close': Decimal('0.5691'), 'ma': Decimal('0.579639999999999933066874291398562490940093994140625'), 'close_time': '2023-10-24 12:54:59'}, {'open': Decimal('0.5691'), 'high': Decimal('0.5733'), 'low': Decimal('0.5681'), 'close': Decimal('0.5721'), 'ma': Decimal('0.57874999999999998667732370449812151491641998291015625'), 'close_time': '2023-10-24 12:55:59'}],
'ETHUSDT_1m/5_3': [{'open': Decimal('1847.21'), 'high': Decimal('1848.11'), 'low': Decimal('1847.21'), 'close': Decimal('1848.03'), 'ma': Decimal('1847.92999999999983629095368087291717529296875'), 'close_time': '2023-10-24 12:51:59'}, {'open': Decimal('1848.03'), 'high': Decimal('1848.93'), 'low': Decimal('1847.32'), 'close': Decimal('1848.41'), 'ma': Decimal('1847.656000000000176441972143948078155517578125'), 'close_time': '2023-10-24 12:52:59'}, {'open': Decimal('1848.42'), 'high': Decimal('1848.42'), 'low': Decimal('1845.32'), 'close': Decimal('1845.45'), 'ma': Decimal('1847.19799999999986539478413760662078857421875'), 'close_time': '2023-10-24 12:53:59'}, {'open': Decimal('1845.44'), 'high': Decimal('1846.04'), 'low': Decimal('1841.24'), 'close': Decimal('1841.24'), 'ma': Decimal('1846.066000000000030922819860279560089111328125'), 'close_time': '2023-10-24 12:54:59'}, {'open': Decimal('1841.24'), 'high': Decimal('1843.16'), 'low': Decimal('1839.17'), 'close': Decimal('1841.45'), 'ma': Decimal('1844.915999999999939973349682986736297607421875'), 'close_time': '2023-10-24 12:55:59'}],
'BTCUSDT_1m/10_2': [{'open': Decimal('34769.60'), 'high': Decimal('34794.40'), 'low': Decimal('34725.30'), 'close': Decimal('34734.80'), 'ma': Decimal('34740.0599999999976716935634613037109375'), 'close_time': '2023-10-24 12:45:59'}, {'open': Decimal('34734.70'), 'high': Decimal('34734.80'), 'low': Decimal('34675.20'), 'close': Decimal('34696.50'), 'ma': Decimal('34740.759999999994761310517787933349609375'), 'close_time': '2023-10-24 12:46:59'}, {'open': Decimal('34696.60'), 'high': Decimal('34714.60'), 'low': Decimal('34686.70'), 'close': Decimal('34704.90'), 'ma': Decimal('34744.4400000000023283064365386962890625'), 'close_time': '2023-10-24 12:47:59'}, {'open': Decimal('34704.90'), 'high': Decimal('34712'), 'low': Decimal('34669.20'), 'close': Decimal('34669.20'), 'ma': Decimal('34739.3400000000037834979593753814697265625'), 'close_time': '2023-10-24 12:48:59'}, {'open': Decimal('34669.20'), 'high': Decimal('34684'), 'low': Decimal('34650.20'), 'close': Decimal('34660.40'), 'ma': Decimal('34728.050000000002910383045673370361328125'), 'close_time': '2023-10-24 12:49:59'}, {'open': Decimal('34660.50'), 'high': Decimal('34682.70'), 'low': Decimal('34652.80'), 'close': Decimal('34671.90'), 'ma': Decimal('34719.9400000000096042640507221221923828125'), 'close_time': '2023-10-24 12:50:59'}, {'open': Decimal('34672'), 'high': Decimal('34700'), 'low': Decimal('34671.90'), 'close': Decimal('34699.20'), 'ma': Decimal('34710.91000000000349245965480804443359375'), 'close_time': '2023-10-24 12:51:59'}, {'open': Decimal('34699.30'), 'high': Decimal('34719.50'), 'low': Decimal('34691.10'), 'close': Decimal('34713.20'), 'ma': Decimal('34707.2300000000032014213502407073974609375'), 'close_time': '2023-10-24 12:52:59'}, {'open': Decimal('34713.20'), 'high': Decimal('34713.20'), 'low': Decimal('34657.50'), 'close': Decimal('34657.80'), 'ma': Decimal('34697.759999999994761310517787933349609375'), 'close_time': '2023-10-24 12:53:59'}, {'open': Decimal('34657.80'), 'high': Decimal('34677.90'), 'low': Decimal('34610.80'), 'close': Decimal('34610.80'), 'ma': Decimal('34681.8700000000026193447411060333251953125'), 'close_time': '2023-10-24 12:54:59'}],
'BNBUSDT_1m/10_2': [{'open': Decimal('232.960'), 'high': Decimal('232.990'), 'low': Decimal('232.760'), 'close': Decimal('232.920'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:37:59'}, {'open': Decimal('232.930'), 'high': Decimal('233.050'), 'low': Decimal('232.850'), 'close': Decimal('233.010'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:38:59'}, {'open': Decimal('233.020'), 'high': Decimal('233.200'), 'low': Decimal('233.020'), 'close': Decimal('233.030'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:39:59'}, {'open': Decimal('233.030'), 'high': Decimal('233.120'), 'low': Decimal('233.010'), 'close': Decimal('233.090'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:40:59'}, {'open': Decimal('233.090'), 'high': Decimal('233.380'), 'low': Decimal('233.090'), 'close': Decimal('233.240'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:41:59'}, {'open': Decimal('233.250'), 'high': Decimal('233.510'), 'low': Decimal('232.750'), 'close': Decimal('232.750'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:42:59'}, {'open': Decimal('232.880'), 'high': Decimal('233.180'), 'low': Decimal('232.670'), 'close': Decimal('233.050'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:43:59'}, {'open': Decimal('233.050'), 'high': Decimal('233.280'), 'low': Decimal('232.960'), 'close': Decimal('233.220'), 'ma': Decimal('233.01200000000000045474735088646411895751953125'), 'close_time': '2023-10-24 12:44:59'}, {'open': Decimal('233.210'), 'high': Decimal('233.240'), 'low': Decimal('232.900'), 'close': Decimal('232.950'), 'ma': Decimal('233.02199999999999135980033315718173980712890625'), 'close_time': '2023-10-24 12:45:59'}, {'open': Decimal('232.950'), 'high': Decimal('233.090'), 'low': Decimal('232.780'), 'close': Decimal('233.040'), 'ma': Decimal('233.029999999999972715158946812152862548828125'), 'close_time': '2023-10-24 12:46:59'}],
'EOSUSDT_3m/4_1': [{'open': Decimal('0.606'), 'high': Decimal('0.608'), 'low': Decimal('0.605'), 'close': Decimal('0.607'), 'ma': Decimal('0'), 'close_time': '2023-10-24 12:44:59'}, {'open': Decimal('0.607'), 'high': Decimal('0.608'), 'low': Decimal('0.605'), 'close': Decimal('0.607'), 'ma': Decimal('0.606500000000000039079850466805510222911834716796875'), 'close_time': '2023-10-24 12:47:59'}, {'open': Decimal('0.606'), 'high': Decimal('0.607'), 'low': Decimal('0.605'), 'close': Decimal('0.605'), 'ma': Decimal('0.606500000000000039079850466805510222911834716796875'), 'close_time': '2023-10-24 12:50:59'}, {'open': Decimal('0.605'), 'high': Decimal('0.608'), 'low': Decimal('0.605'), 'close': Decimal('0.606'), 'ma': Decimal('0.6062499999999999555910790149937383830547332763671875'), 'close_time': '2023-10-24 12:53:59'}]}
所以你可以看到像 LTCUSDT 上次更新在 like 但其他具有相对参数的符号更新在2023-10-24 12:43:59
2023-10-24 12:55:59
append_values这是我将值附加到
async def append_values(self, trade_id, symbol, interval, length, barlimit, data):
"""
Calculates the moving average (MA) for a given symbol and interval.
Args:
symbol (str): The symbol for which to calculate the MA.
interval (str): The interval at which to calculate the MA.
length (int): The length of the MA.
close_price (decimal): The current close price of the symbol.
timestamp (int): The timestamp of the current close price.
Returns:
decimal or None: The calculated MA if there are enough close prices, None otherwise.
"""
try:
key = f"{symbol.upper()}_{interval}/{length}_{barlimit}"
if key not in self.candlesticks:
self.candlesticks[key] = []
close_prices_list = self.candlesticks[key]
close_prices_list.append(data)
if len(close_prices_list) > length:
close_prices_list.pop(0)
asyncio.create_task(self.ma_calculation(trade_id, close_prices_list, barlimit, key))
except Exception as e:
logger.error(f"Error in appending values for {symbol} {interval}: {str(e)}")
所以我猜其中的一些地方是一个问题。是 django 频道消费者的一部分。我称这段代码像asyncio.ensure_future(self.append_values(trade_id, symbol, interval, length, barlimit, candle))
答: 暂无答案
评论