如何使用groupby计算vwap(交易量加权平均价格)并申请?

时间:2017-06-30 20:39:32

标签: python pandas lambda pandas-groupby

我已经阅读了与我的问题类似的多个帖子,但我仍然无法弄明白。我有一个看起来如下的熊猫df(多天):

Out[1]: 
                     price  quantity
time                                
2016-06-08 09:00:22  32.30    1960.0
2016-06-08 09:00:22  32.30     142.0
2016-06-08 09:00:22  32.30    3857.0
2016-06-08 09:00:22  32.30    1000.0
2016-06-08 09:00:22  32.35     991.0
2016-06-08 09:00:22  32.30     447.0
...

计算我能做的vwap:

df['vwap'] = (np.cumsum(df.quantity * df.price) / np.cumsum(df.quantity))

但是,我想每天重新开始(groupby),但我无法弄清楚如何使用(lambda?)函数。

df['vwap_day'] = df.groupby(df.index.date)['vwap'].apply(lambda ...

速度至关重要。非常感谢任何帮助:)

2 个答案:

答案 0 :(得分:7)

选项0
普通的香草方法

def vwap(df):
    q = df.quantity.values
    p = df.price.values
    return df.assign(vwap=(p * q).cumsum() / q.cumsum())

df = df.groupby(df.index.date, group_keys=False).apply(vwap)
df

                     price  quantity       vwap
time                                           
2016-06-08 09:00:22  32.30    1960.0  32.300000
2016-06-08 09:00:22  32.30     142.0  32.300000
2016-06-08 09:00:22  32.30    3857.0  32.300000
2016-06-08 09:00:22  32.30    1000.0  32.300000
2016-06-08 09:00:22  32.35     991.0  32.306233
2016-06-08 09:00:22  32.30     447.0  32.305901

选项1
投掷一点eval

df = df.assign(
    vwap=df.eval(
        'wgtd = price * quantity', inplace=False
    ).groupby(df.index.date).cumsum().eval('wgtd / quantity')
)
df

                     price  quantity       vwap
time                                           
2016-06-08 09:00:22  32.30    1960.0  32.300000
2016-06-08 09:00:22  32.30     142.0  32.300000
2016-06-08 09:00:22  32.30    3857.0  32.300000
2016-06-08 09:00:22  32.30    1000.0  32.300000
2016-06-08 09:00:22  32.35     991.0  32.306233
2016-06-08 09:00:22  32.30     447.0  32.305901

答案 1 :(得分:4)

我之前也使用过这种方法,但如果您试图限制窗口期,它的效果就不太准确。相反,我发现 TA python 库工作得非常好: https://technical-analysis-library-in-python.readthedocs.io/en/latest/index.html

from ta.volume import VolumeWeightedAveragePrice

# ...
def vwap(dataframe, label='vwap', window=3, fillna=True):
        dataframe[label] = VolumeWeightedAveragePrice(high=dataframe['high'], low=dataframe['low'], close=dataframe["close"], volume=dataframe['volume'], window=window, fillna=fillna).volume_weighted_average_price()
        return dataframe