松树脚本中的VWAP

问题描述 投票:0回答:2

TradingView 上有一个 VWAP 指标,您可以调整到锚定周期。 vwap 函数中的 pine 脚本内置功能不可调整。如何以与图表指标相同的方式将内置 pine 函数 vwap 调整为时间段?

问候 斯文

pine-script indicator
2个回答
2
投票

不确定您是否在寻找什么,但可能会有所帮助。您可以在任何时间范围内可视化任何其他时间范围的数据。例如,您可以从 1m 图表中看到每周的 VWAP。

// VWAP
price = input(type=input.source, defval=hlc3, title="VWAP Source")
enable_vwap = input(true, title="Enable VWAP")
vwapResolution = input(title = "VWAP Resolution", defval = "", type=input.resolution)
vwapFunction = vwap(price)
vwapSecurity = security(syminfo.tickerid, vwapResolution, vwapFunction)
plot(enable_vwap ? vwapSecurity : na, title="VWAP", color=color.white, linewidth=2, transp=0, editable=true) // All TimeFrames

这是 1 小时图表的示例,检查我在此提供的 VWAP 中的 1 周信息(白色线),以及来自电视的内置信息(黄色线):

您可以看出其中存在差异,并且您可能会找到更好地利用它的方法。尝试一下,在不同的时间段尝试不同的方法。

顺便说一句,在屏幕截图上的白色 VWAP 中,我使用低价作为白色 VWAP 的源输入,而默认值(黄色 VWAP 使用 hcl3)。


0
投票

我想我会为此做出贡献,因为我昨天做了这件事。

您必须设置许多预先确定的时间范围设置,然后使用由周期变化检测器启动的开关函数分配 timeframe.change(x)。

此脚本会根据您的要求创建一个指标。那里有一些额外的好东西,但它在设置中完全可编辑。

只需更改 VWAP 周期即可。如果您添加更多周期,请确保将它们添加到锚点选项(第 8 行)和新周期检测器的 switch 函数中(第 17 行)。

//@version=5
indicator(title='Volume Weighted Average Price', shorttitle='VWAP', overlay=true, timeframe='', timeframe_gaps=true)

hideonDWM = input(false, title='Hide VWAP on 1D or Above') // hide below daily timeframe

var anchor = input.string(defval = 'Quarter', title='VWAP Period', options=['Session', 'Week', 'Month', 'Earnings', 'Quarter', 'Year']) // period settings
new_earnings = request.earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true) // get earnings

if barstate.islast and ta.cum(volume) == 0
    runtime.error('No volume is provided by the data vendor.') // volume na

float vwap_val = na
new_period = true

new_period := switch anchor // set anchor to period setting. need to be 1to1 or wont render anything
    'Session' => timeframe.change('D')
    'Week' => timeframe.change('W')
    'Month' => timeframe.change('M')
    'Earnings' => not na(new_earnings)
    'Quarter' => timeframe.change('3M')
    'Year' => timeframe.change('12M')
    => false // reset new period detector

src = input(title = 'Source', defval = ohlc4) 

earnings_anchor = anchor == 'Earnings'

if na(src[1]) and not earnings_anchor // new period detector
    new_period := true

if not (hideonDWM and timeframe.isdwm) //
    [_vwap, _stdev, _] = ta.vwap(ohlc4, new_period, 1) // create vwap
    vwap_val := _vwap

vwap_color = close >= vwap_val ? #2ad9f8 : #fc4792 // assign colors to vwap based on price


plot(vwap_val, title='VWAP', color=vwap_color , style=plot.style_stepline)
© www.soinside.com 2019 - 2024. All rights reserved.