如何使用带有询价和出价的熊猫数据框计算体积加权平均价格(VWAP)?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用带有询价和出价的熊猫数据框计算体积加权平均价格(VWAP)?相关的知识,希望对你有一定的参考价值。

我如何创建另一个名为vwap的列,如果我的表如下所示,它会计算vwap?

             time            bid_size   bid       ask  ask_size trade trade_size phase  
0   2019-01-07 07:45:01.064515  495   152.52    152.54    19     NaN      NaN    OPEN   
1   2019-01-07 07:45:01.110072  31    152.53    152.54    19     NaN      NaN    OPEN   
2   2019-01-07 07:45:01.116596  32    152.53    152.54    19     NaN      NaN    OPEN   
3   2019-01-07 07:45:01.116860  32    152.53    152.54    21     NaN      NaN    OPEN   
4   2019-01-07 07:45:01.116905  34    152.53    152.54    21     NaN      NaN    OPEN   
5   2019-01-07 07:45:01.116982  34    152.53    152.54    31     NaN      NaN    OPEN   
6   2019-01-07 07:45:01.147901  38    152.53    152.54    31     NaN      NaN    OPEN   
7   2019-01-07 07:45:01.189971  38    152.53    152.54    31     ask     15.0    OPEN   
8   2019-01-07 07:45:01.189971  38    152.53    152.54    16     NaN      NaN    OPEN   
9   2019-01-07 07:45:01.190766  37    152.53    152.54    16     NaN      NaN    OPEN   
10  2019-01-07 07:45:01.190856  37    152.53    152.54    15     NaN      NaN    OPEN
11  2019-01-07 07:45:01.190856  37    152.53    152.54    16     ask      1.0    OPEN   
12  2019-01-07 07:45:01.193938  37    152.53    152.55   108     NaN      NaN    OPEN   
13  2019-01-07 07:45:01.193938  37    152.53    152.54    15     ask     15.0    OPEN   
14  2019-01-07 07:45:01.194326  2     152.54    152.55   108     NaN      NaN    OPEN   
15  2019-01-07 07:45:01.194453  2     152.54    152.55    97     NaN      NaN    OPEN   
16  2019-01-07 07:45:01.194479  6     152.54    152.55    97     NaN      NaN    OPEN   
17  2019-01-07 07:45:01.194507  19    152.54    152.55    97     NaN      NaN    OPEN   
18  2019-01-07 07:45:01.194532  19    152.54    152.55    77     NaN      NaN    OPEN   
19  2019-01-07 07:45:01.194598  19    152.54    152.55    79     NaN      NaN    OPEN   

对不起,表格不清楚,但第二个最右边的列是trade_size,左边是交易,它显示交易的一面(买入或卖出)。如果trade_size和trade都是NaN,则表示该时间戳没有交易。

如果df ['trade'] ==“ask”,交易价格将是'ask'栏中的价格,如果df ['trade] ==“bid”,交易价格将是'bid'栏中的价格。由于有2个价格,请问如何计算vwap,df ['vwap']?

我的想法是使用np.cumsum()。谢谢!

答案

您可以使用np.where从正确的列(bidask)中获取价格,具体取决于trade列中的值。请注意,这可以在没有交易发生时为您提供出价,但因为这会乘以NaN交易规模而无关紧要。我也向前填补了VWAP。

volume = df['trade_size']
price = np.where(df['trade'].eq('ask'), df['ask'], df['bid'])  
df = df.assign(VWAP=((volume * price).cumsum() / vol.cumsum()).ffill())

>>> df
        time    bid_size    bid ask ask_size    trade   trade_size  phase   VWAP
0   2019-01-07  07:45:01.064515 495 152.52  152.54  19  NaN NaN OPEN    NaN
1   2019-01-07  07:45:01.110072 31  152.53  152.54  19  NaN NaN OPEN    NaN
2   2019-01-07  07:45:01.116596 32  152.53  152.54  19  NaN NaN OPEN    NaN
3   2019-01-07  07:45:01.116860 32  152.53  152.54  21  NaN NaN OPEN    NaN
4   2019-01-07  07:45:01.116905 34  152.53  152.54  21  NaN NaN OPEN    NaN
5   2019-01-07  07:45:01.116982 34  152.53  152.54  31  NaN NaN OPEN    NaN
6   2019-01-07  07:45:01.147901 38  152.53  152.54  31  NaN NaN OPEN    NaN
7   2019-01-07  07:45:01.189971 38  152.53  152.54  31  ask 15.0    OPEN    152.54
8   2019-01-07  07:45:01.189971 38  152.53  152.54  16  NaN NaN OPEN    152.54
9   2019-01-07  07:45:01.190766 37  152.53  152.54  16  NaN NaN OPEN    152.54
10  2019-01-07  07:45:01.190856 37  152.53  152.54  15  NaN NaN OPEN    152.54
11  2019-01-07  07:45:01.190856 37  152.53  152.54  16  ask 1.0 OPEN    152.54
12  2019-01-07  07:45:01.193938 37  152.53  152.55  108 NaN NaN OPEN    152.54
13  2019-01-07  07:45:01.193938 37  152.53  152.54  15  ask 15.0    OPEN    152.54
14  2019-01-07  07:45:01.194326 2   152.54  152.55  108 NaN NaN OPEN    152.54
15  2019-01-07  07:45:01.194453 2   152.54  152.55  97  NaN NaN OPEN    152.54
16  2019-01-07  07:45:01.194479 6   152.54  152.55  97  NaN NaN OPEN    152.54
17  2019-01-07  07:45:01.194507 19  152.54  152.55  97  NaN NaN OPEN    152.54
18  2019-01-07  07:45:01.194532 19  152.54  152.55  77  NaN NaN OPEN    152.54
19  2019-01-07  07:45:01.194598 19  152.54  152.55  79  NaN NaN OPEN    152.54
另一答案

这是一种可能的方法

附加VMAP列充满NaNs

df['VMAP'] = np.nan

计算VMAP(基于this方程provided by the OP)并根据askbidas requierd by the OP分配值

for trade in ['ask','bid']:
    # Find indexes of `ask` or `buy`
    bid_idx = df[df.trade==trade].index

    # Slice DF based on `ask` or `buy`, using indexes
    df.loc[bid_idx, 'VMAP'] = (
        (df.loc[bid_idx, 'trade_size'] * df.loc[bid_idx, trade]).cumsum()
        /
        (df.loc[bid_idx, 'trade_size']).cumsum()
                )

print(df.iloc[:,1:])
               time  bid_size     bid     ask  ask_size trade  trade_size phase    VMAP
0   07:45:01.064515       495  152.52  152.54        19   NaN         NaN  OPEN     NaN
1   07:45:01.110072        31  152.53  152.54        19   NaN         NaN  OPEN     NaN
2   07:45:01.116596        32  152.53  152.54        19   NaN         NaN  OPEN     NaN
3   07:45:01.116860        32  152.53  152.54        21   NaN         NaN  OPEN     NaN
4   07:45:01.116905        34  152.53  152.54        21   NaN         NaN  OPEN     NaN
5   07:45:01.116982        34  152.53  152.54        31   NaN         NaN  OPEN     NaN
6   07:45:01.147901        38  152.53  152.54        31   NaN         NaN  OPEN     NaN
7   07:45:01.189971        38  152.53  152.54        31   ask        15.0  OPEN  152.54
8   07:45:01.189971        38  152.53  152.54        16   NaN         NaN  OPEN     NaN
9   07:45:01.190766        37  152.53  152.54        16   NaN         NaN  OPEN     NaN
10  07:45:01.190856        37  152.53  152.54        15   NaN         NaN  OPEN     NaN
11  07:45:01.190856        37  152.53  152.54        16   ask         1.0  OPEN  152.54
12  07:45:01.193938        37  152.53  152.55       108   NaN         NaN  OPEN     NaN
13  07:45:01.193938        37  152.53  152.54        15   ask        15.0  OPEN  152.54
14  07:45:01.194326         2  152.54  152.55       108   NaN         NaN  OPEN     NaN
15  07:45:01.194453         2  152.54  152.55        97   NaN         NaN  OPEN     NaN
16  07:45:01.194479         6  152.54  152.55        97   NaN         NaN  OPEN     NaN
17  07:45:01.194507        19  152.54  152.55        97   NaN         NaN  OPEN     NaN
18  07:45:01.194532        19  152.54  152.55        77   NaN         NaN  OPEN     NaN
19  07:45:01.194598        19  152.54  152.55        79   NaN         NaN  OPEN     NaN

编辑

作为@edinho correctly indicatedVMAPtrade_price列相同。

另一答案

好的,这是

df['trade_price'] = df.apply(lambda x: x['bid'] if x['trade']=='bid' else x['ask'], axis=1)
df['vwap'] = (df['trade_price'] * df['trade_size']).cumsum() / df['trade_size'].fillna(0).cumsum()

第一行: 它将trade_price保存在新列中,因此以后更容易检索它。 如果需要,可以删除此行并创建一个函数(可能更容易阅读)。但我更愿意看到中间结果。 问:为什么即使没有交易它也有价值? 答:因为编写lambda的方式。 else捕获了ask的价格。但由于下一步,它不会有所作为。

第二行: 这里进行了真正的计算。 第一部分计算到那个时刻交易的总交易量(如你所说,使用累计金额使生活更轻松)。 第二部分计算到那个时刻交易的总交易量(再次,累计总和)。 如果需要,可以断开此行并创建更多中间列。 问:为什么fillna(0)? - 答:所以总量不得NaNs,你没有得到除法错误问:为什么NaNs列中有这么多vwap? 答:因为没有交易的线路。您可以用0s填充它们,但最好保留“无交易”信息。

Ps:您可能会得到错误的结果,因为它只考虑相同方向的数量和价格。但是,您可以尝试反转某些信号以按预期方式修复音量(例如:将ask价格更改为负值)。

而这段代码输出:

    trade_price vwap
1   152.54  NaN
2   152.54  NaN
3   152.54  NaN
4   152.54  NaN
5   152.54  NaN
6   152.54  NaN
7   152.54  NaN
8   152.54  152.54
9   152.54  NaN
10  152.54  NaN
11  152.54  NaN
12  152.54  152.54
13  152.55  NaN
14  152.54  152.54
15  152.55  NaN
16  152.55  NaN
17  152.55  NaN
18  152.55  NaN
19  152.55  NaN
20  152.55  NaN

以上是关于如何使用带有询价和出价的熊猫数据框计算体积加权平均价格(VWAP)?的主要内容,如果未能解决你的问题,请参考以下文章

熊猫数据框每一行的加权平均值

如何计算熊猫中一行中所有元素的加权和?

基于买卖的加权平均收购成本熊猫

group的加权平均值不等于pandas groupby中的总平均值

熊猫移动平均线[重复]

熊猫滚动加权平均值