计算交易算法中最有效的VWAP(成交量加权平均价格)的方法

huangapple go评论61阅读模式
英文:

Most efficient way to calculate VWAP(VOLUME WEIGHTED AVERAGE PRICE) for trading algorithm

问题

我正在寻找一个用于包含股票一年内5分钟蜡烛图数据的数据框的高效VWAP算法。

我的代码:

def calculate_vwap(data):
    data['VWAP'] = data.groupby('date').apply(lambda x: ((x['high'] + x['low'] + x['close']) / 3 * x['volume']).cumsum() / x['volume'].cumsum()).reset_index(level=0, drop=True)
    return data['VWAP']

我的代码的输出与TradingView VWAP值不匹配。样本数据链接 -
https://drive.google.com/file/d/12oDiWe9HWQDZEvGmQAqcznklT8NBObGQ/view?usp=sharing
样本:

计算交易算法中最有效的VWAP(成交量加权平均价格)的方法

英文:

I am looking for an efficient VWAP algorithm for a dataframe that contains 5 minute candles data for a stock for an year.

My code:

def calculate_vwap(data):
    data['VWAP'] = data.groupby('date').apply(lambda x: ((x['high'] + x['low'] + x['close']) / 3 * x['volume']).cumsum() / x['volume'].cumsum()).reset_index(level=0, drop=True)
    return data['VWAP']

The output of my code does not match tradingview VWAP values. Link to the sample data -
https://drive.google.com/file/d/12oDiWe9HWQDZEvGmQAqcznklT8NBObGQ/view?usp=sharing
Sample:

计算交易算法中最有效的VWAP(成交量加权平均价格)的方法

答案1

得分: 0

经过一些研究,我发现一些交易平台使用以下平均价格来计算VWAP值 - (high+low+close)/3,而一些使用(high+low)/2。

此外,对于VWAP计算,累积成交量和累积(价格X成交量)是从当天的第一根蜡烛图开始计算的。这导致我修改了我的代码,像这样工作了 -

for ticker in Angel_Hist_Data5Min:
    Angel_Hist_Data5Min[ticker]["AvgP"] = ((Angel_Hist_Data5Min[ticker]["high"]+Angel_Hist_Data5Min[ticker]["low"]+Angel_Hist_Data5Min[ticker]["close"])/3)
    Angel_Hist_Data5Min[ticker]["PV"] = Angel_Hist_Data5Min[ticker]["AvgP"]*Angel_Hist_Data5Min[ticker]["volume"]
    Angel_Hist_Data5Min[ticker]['CumPV'] = Angel_Hist_Data5Min[ticker].groupby(Angel_Hist_Data5Min[ticker]['date'].dt.date)['PV'].cumsum()
    Angel_Hist_Data5Min[ticker]["VWAP_hlc"] = Angel_Hist_Data5Min[ticker]["CumPV"]/Angel_Hist_Data5Min[ticker]["CumVolume"]

    Angel_Hist_Data5Min[ticker]["AvgP_hl"] = ((Angel_Hist_Data5Min[ticker]["high"]+Angel_Hist_Data5Min[ticker]["low"])/2)
    Angel_Hist_Data5Min[ticker]["PV_hl"] = Angel_Hist_Data5Min[ticker]["AvgP_hl"]*Angel_Hist_Data5Min[ticker]["volume"]
    Angel_Hist_Data5Min[ticker]['CumPV_hl'] = Angel_Hist_Data5Min[ticker].groupby(Angel_Hist_Data5Min[ticker]['date'].dt.date)['PV_hl'].cumsum()
    Angel_Hist_Data5Min[ticker]["VWAP_hl"] = Angel_Hist_Data5Min[ticker]["CumPV_hl"]/Angel_Hist_Data5Min[ticker]["CumVolume"]

现在我正在计算两个VWAP,这有助于与所有交易平台的数据匹配。

英文:

After doing some research I found out that some trading platforms use this average price - (high+low+close)/3 and some use (high+low)/2 for arriving at VWAP values.

And also for the VWAP calculation the cumulataive volume and cumulative (priceXvolume) is calculated from first candle of the day. This lead me to modify my codes like this and it worked now -

for ticker in Angel_Hist_Data5Min:

Angel_Hist_Data5Min[ticker]["AvgP"] = ((Angel_Hist_Data5Min[ticker]["high"]+Angel_Hist_Data5Min[ticker]["low"]+Angel_Hist_Data5Min[ticker]["close"])/3)
Angel_Hist_Data5Min[ticker]["PV"] = Angel_Hist_Data5Min[ticker]["AvgP"]*Angel_Hist_Data5Min[ticker]["volume"]
Angel_Hist_Data5Min[ticker]['CumPV'] = Angel_Hist_Data5Min[ticker].groupby(Angel_Hist_Data5Min[ticker]['date'].dt.date)['PV'].cumsum()
Angel_Hist_Data5Min[ticker]["VWAP_hlc"] = Angel_Hist_Data5Min[ticker]["CumPV"]/Angel_Hist_Data5Min[ticker]["CumVolume"] 

Angel_Hist_Data5Min[ticker]["AvgP_hl"] = ((Angel_Hist_Data5Min[ticker]["high"]+Angel_Hist_Data5Min[ticker]["low"])/2)
Angel_Hist_Data5Min[ticker]["PV_hl"] = Angel_Hist_Data5Min[ticker]["AvgP_hl"]*Angel_Hist_Data5Min[ticker]["volume"]  
Angel_Hist_Data5Min[ticker]['CumPV_hl'] = Angel_Hist_Data5Min[ticker].groupby(Angel_Hist_Data5Min[ticker]['date'].dt.date)['PV_hl'].cumsum()
Angel_Hist_Data5Min[ticker]["VWAP_hl"] = Angel_Hist_Data5Min[ticker]["CumPV_hl"]/Angel_Hist_Data5Min[ticker]["CumVolume"]

I am calculating two VWAPs now and this helps to match with all the trading platforms data.

huangapple
  • 本文由 发表于 2023年6月19日 00:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定