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

cJc*_*cJc 3 python lambda pandas pandas-groupby

我已经阅读了类似于我的问题的多个帖子,但我仍然无法弄明白.我有一个pandas 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
...
Run Code Online (Sandbox Code Playgroud)

要计算我能做的vwap:

df['vwap'] = (np.cumsum(df.quantity * df.price) / np.cumsum(df.quantity))
Run Code Online (Sandbox Code Playgroud)

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

df['vwap_day'] = df.groupby(df.index.date)['vwap'].apply(lambda ...
Run Code Online (Sandbox Code Playgroud)

速度至关重要.不胜感激任何帮助:)

小智 8

我以前也使用过这种方法,但如果您试图限制窗口期,它的工作效果不太准确。相反,我发现 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
Run Code Online (Sandbox Code Playgroud)


piR*_*red 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
Run Code Online (Sandbox Code Playgroud)

选项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
Run Code Online (Sandbox Code Playgroud)

  • 花式游戏综合症 (2认同)