【发布时间】:2016-08-03 15:08:42
【问题描述】:
我无法使用 pandas DataFrame.resample() 0.18 版从刻度数据生成“准确”的 OHLC 数据。 具体来说,当价格在某个区间内持续时,我会在这些柱的开盘价处得到“未来泄漏”。
可重现的例子:
import pandas as pd
import numpy as np
np.random.seed(1234)
size=1000
df = pd.DataFrame(1.26 + np.random.rand(size)/100.0,index=pd.date_range('20160101 09:00:00',periods=size,freq='7s'),columns=['price'])
dfohlc = df.price.resample('1min').ohlc().ffill()
这是一系列随机生成的价格变动:
2016-01-01 09:00:00 1.261915
2016-01-01 09:00:07 1.266221
2016-01-01 09:00:14 1.264377
2016-01-01 09:00:21 1.267854
2016-01-01 09:00:28 1.267800
2016-01-01 09:00:35 1.262726
2016-01-01 09:00:42 1.262765
2016-01-01 09:00:49 1.268019
2016-01-01 09:00:56 1.269581 << this is the market price until 09.01:03
2016-01-01 09:01:03 1.268759 << this price should not be the open price at 09:01
...
resample() 生成这些条:
2016-01-01 09:00:00 1.261915 1.269581 1.261915 1.269581
2016-01-01 09:01:00 1.268759 1.268759 1.260138 1.260138
...
第一个 OHLC 柱在 09:00 的开盘价和收盘价是正确的。
但是,09:01:00 的第 2 个 OHLC 柱的开盘价“错误”为 1.268759。此价格要到 09:01:03 才能看到。开盘时的“正确”价格仍然是 1.269581。
有没有办法让 resample() 函数生成“正确”的开盘价?
【问题讨论】: