英文:
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
样本:
英文:
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:
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论