将刻度数据转换为 OHLC 并用最后一个关闭值填充缺失的行 - Pandas

问题描述

我想将以下美元价格和加密价格的报价数据分别转换为带有 Pandas 的 OHLCV 图表。

[{'timestamp': datetime.datetime(2021,4,12,14,29,57,tzinfo=<UTC>),'usdPrice': 0.36041124,'cryptoPrice': 0.00016807,'volume': 3.47548411},{'timestamp': datetime.datetime(2021,18,'usdPrice': 0.36569221,'cryptoPrice': 0.00017033,'volume': 1.97019816},13,16,'usdPrice': 0.36300652,'cryptoPrice': 0.00016856,'volume': 0.20226772},51,25,'usdPrice': 0.36024739,'cryptoPrice': 0.00016763,'volume': 0.92823055},33,34,'usdPrice': 0.35983675,'cryptoPrice': 0.00016768,'volume': 2.21936536},23,54,'usdPrice': 0.36459511,'cryptoPrice': 0.00017019,'volume': 0.85093906},5,'usdPrice': 0.36758283,'cryptoPrice': 0.00017162,'volume': 0.88855535},21,46,'usdPrice': 0.35900551,'cryptoPrice': 0.0001676,'volume': 6.99156633},15,20,'usdPrice': 0.34367198,'cryptoPrice': 0.00016019,'volume': 3.13015057},'usdPrice': 0.34398321,'cryptoPrice': 0.00016033,'volume': 2.11},6,'usdPrice': 0.34961258,'cryptoPrice': 0.00016295,'volume': 0.32899408},7,'usdPrice': 0.35181042,'cryptoPrice': 0.00016393,'volume': 0.33097374},11,30,'usdPrice': 0.34867568,'cryptoPrice': 0.00016301,'volume': 0.40396023},28,31,'usdPrice': 0.34946758,'cryptoPrice': 0.00016339,'volume': 0.07352692},22,'usdPrice': 0.35196562,'cryptoPrice': 0.00016464,'volume': 1.48178883},10,1,'usdPrice': 0.35395163,'cryptoPrice': 0.00016556,'volume': 1.56942121},'usdPrice': 0.34676295,'cryptoPrice': 0.00016297,'volume': 1.65755483},'usdPrice': 0.35037081,'cryptoPrice': 0.00016466,'volume': 5.0},44,35,'usdPrice': 0.35859668,'cryptoPrice': 0.00016846,'volume': 1.58},9,47,56,'usdPrice': 0.35941458,'cryptoPrice': 0.00016651,'volume': 0.82065224},19,'usdPrice': 0.35769754,'cryptoPrice': 0.00016409,'volume': 3.42449907},'usdPrice': 0.35766811,'cryptoPrice': 0.00016407,'volume': 2.19008954},8,'usdPrice': 0.36400554,'cryptoPrice': 0.00016643,'volume': 1.7598},'usdPrice': 0.36348966,'cryptoPrice': 0.00016616,'volume': 2.64353494},'usdPrice': 0.3689597,'cryptoPrice': 0.00016866,'volume': 0.80032254},'usdPrice': 0.36612052,'cryptoPrice': 0.00016738,'volume': 0.77320281},39,'usdPrice': 0.365533,'cryptoPrice': 0.00016711,'volume': 1.67112081},'usdPrice': 0.36628922,'cryptoPrice': 0.00016773,'volume': 2.14326929},'usdPrice': 0.36327858,'cryptoPrice': 0.00016635,'volume': 1.66350658},'usdPrice': 0.36921121,'cryptoPrice': 0.00016908,'volume': 1.69078327},'usdPrice': 0.3668634,'cryptoPrice': 0.0001681,'volume': 1.68098086},'usdPrice': 0.37013507,'cryptoPrice': 0.0001696,'volume': 2.29386496},'usdPrice': 0.37291019,'cryptoPrice': 0.00017087,'volume': 1.7086914},53,'usdPrice': 0.37444301,'cryptoPrice': 0.00017157,'usdPrice': 0.37150272,'cryptoPrice': 0.00017022,'volume': 1.70215914}]

我目前已经进行了一些重新采样,但响应没有准确出现,即;缺失的时间戳没有最后一个关闭值。

df = pd.json_normalize(res)
df = df.set_index('timestamp')
ohlcData = df.resample(resolution).agg({'usdPrice': 'ohlc','volume': 'sum'})
ohlcData = ohlcData.reset_index(level='timestamp')
ohlcData['timestamp'] = ohlcData['timestamp'].apply(lambda x: int(x.timestamp()))
print(ohlcData)

缺口没有被准确填补,需要为所有缺失的 OHLC 值和最后收盘价调用 fillna 方法。无法准确解决这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)