[英]Calculating average daily volume giving incorrect data (Pine v5)
I'm trying to create a custom volume script and have calculated the last 30 days volume average using, what I think is the correct code:我正在尝试创建自定义交易量脚本并使用我认为正确的代码计算了最近 30 天的交易量平均值:
dailyLookback = input(30, "Daily Lookback", group = "Vol Settings", inline = "Volume Settings")
dVol = request.security(syminfo.tickerid, "D", volume)
dAvgVol = math.sum(dVol[1], dailyLookback) / dailyLookback
The data is then plotted in a table column but the calculated result is different on all time frames and none are the correct value.然后将数据绘制在表格列中,但计算结果在所有时间范围内都不同,没有一个是正确的值。 Is there a problem with the code?代码有问题吗?
Your problem is the dVol[1]
.你的问题是dVol[1]
。 You cannot use the history reference operator to access historical data of the higher timeframe bars.您不能使用历史引用运算符来访问较高时间帧柱的历史数据。
dVol[1]
refers to dVol
's previous value on your chart's timeframe. dVol[1]
是指dVol
在图表时间范围内的先前值。 That would still be the same as dVol
unless it is a new bar on the higher timeframe.这仍然与dVol
相同,除非它是更高时间范围内的新柱。
What you need to do is, calculate the average volume based on your chart's timeframe and pass this expression to the security()
function.您需要做的是,根据图表的时间范围计算平均交易量,并将此表达式传递给security()
function。
avgVol = ta.sma(volume, dailyLookback)
dAvgVol = request.security(syminfo.tickerid, "D", avgVol[1], lookahead=barmerge.lookahead_on)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.