如何为Koalafied VWAP指标添加额外标准差水平?
为Koalafied VWAP指标添加额外标准差水平的解决方案
我需要给Koalafied VWAP指标新增两个额外的标准差水平(±3σ),但直接复制现有标准差代码修改后无法正常工作,以下是修复后的完整代码及修改说明:
修改要点
- 新增
stDevMultiplier_3输入参数,用于设置3倍标准差的乘数 - 更新
compute_stDEV函数,加入±3σ波段的计算逻辑 - 新增±3σ波段的绘图、标签及填充逻辑
- 更新
previouslevels函数,支持保存和返回历史±3σ水平 - 新增历史±3σ水平的绘图与标签
修改后的完整代码
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © TJ_667 // Volume Weighted Average Price for multiple periods with Standard Deviation band selection - Daily, Weekly, Monthly, Quarterly, Yearly indicator("Koalafied VWAP D/W/M/Q/Y ", overlay = true) // ---------- Functions ---------- // // TradingView vwap function. Taken out stdev calcs to put in seperate function. computeVWAP(src, isNewPeriod) => var float sumSrcVol = na var float sumVol = na var float sumSrcSrcVol = na sumSrcVol := isNewPeriod ? src * volume : src * volume + sumSrcVol[1] sumVol := isNewPeriod ? volume : volume + sumVol[1] // sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation sumSrcSrcVol := isNewPeriod ? volume * math.pow(src, 2) : volume * math.pow(src, 2) + sumSrcSrcVol[1] _vwap = sumSrcVol / sumVol variance = sumSrcSrcVol / sumVol - math.pow(_vwap, 2) variance := variance < 0 ? 0 : variance stDev = math.sqrt(variance) [_vwap, stDev] // Standard Deviation Band calculation function - 新增±3σ计算 compute_stDEV(_vwap, stDev, stDevMultiplier_1, stDevMultiplier_2, stDevMultiplier_3) => upperBand_3 = _vwap + stDev * stDevMultiplier_3 upperBand_2 = _vwap + stDev * stDevMultiplier_2 upperBand_1 = _vwap + stDev * stDevMultiplier_1 lowerBand_1 = _vwap - stDev * stDevMultiplier_1 lowerBand_2 = _vwap - stDev * stDevMultiplier_2 lowerBand_3 = _vwap - stDev * stDevMultiplier_3 [upperBand_3, upperBand_2, upperBand_1, lowerBand_1, lowerBand_2, lowerBand_3] // Label function from sbtnc - Daily Weekly Monthly Opens script f_drawLabel(_x, _y, _text, _textcolor, _style, _size) => var _label = label.new( x = _x, y = _y, text = _text, textcolor = _textcolor, style = _style, size = _size, xloc = xloc.bar_time ) label.set_xy(_label, _x, _y) // ---------- VWAP selection ---------- // src = input(hlc3, "VWAP Source") plot_vD = input(true, "Plot Daily VWAP") plot_vW = input(true, "Plot Weekly VWAP") plot_vM = input(true, "Plot Monthly VWAP") plot_vQ = input(true, "Plot Quarterly VWAP") plot_vY = input(true, "Plot Yearly VWAP") showStd = input(false, "Plot Standard Deviation") Std_selection = input.string(title = "Standard Deviation Option", defval="Day", options = ["Day", "Week", "Month", "Quarter", "Year"]) // 新增3倍标准差乘数输入 stDevMultiplier_1 = input(1.0, "StDev mult 1") stDevMultiplier_2 = input(2.0, "StDev mult 2") stDevMultiplier_3 = input(3.0, "StDev mult 3") show_labels = input(true, "Show Labels") fill_bands = input(false, "Fill Bands") // 新增控制是否绘制±3σ历史水平的参数 plot_pL1 = input(false, "Plot Previous Period 1+- Levels") plot_pL2 = input(false, "Plot Previous Period 2+- Levels") plot_pL3 = input(false, "Plot Previous Period 3+- Levels") show_plabels = input(true, "Show Previous Labels") fill_pbands = input(false, "Fill Previous Bands") off_mult = input(15, "Label offset") var DEFAULT_LABEL_SIZE = size.small var DEFAULT_LABEL_STYLE = label.style_none ll_offset = timenow + math.round(ta.change(time)*off_mult) // ---------- VWAP Calculations ---------- // timeChange(period) => ta.change(time(period)) newSessionD = timeChange("D") newSessionW = timeChange("W") newSessionM = timeChange("M") newSessionQ = timeChange("3M") newSessionY = timeChange("12M") // ---------- VWAP Function Calls ---------- // [vD, stdevD] = computeVWAP(src, newSessionD) [vW, stdevW] = computeVWAP(src, newSessionW) [vM, stdevM] = computeVWAP(src, newSessionM) [vQ, stdevQ] = computeVWAP(src, newSessionQ) [vY, stdevY] = computeVWAP(src, newSessionY) // ---------- Plot VWAPs ---------- // vDplot = plot(plot_vD ? vD : na, title = "VWAP - Daily", color = color.red, style = plot.style_line, transp = 0, linewidth = 1) f_drawLabel(ll_offset, show_labels and plot_vD ? vD : na, "--------- vD ", color.red, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) vWplot = plot(plot_vW ? vW : na, title = "VWAP - Weekly", color = color.white, style = plot.style_line, transp = 0, linewidth = 1) f_drawLabel(ll_offset, show_labels and plot_vW ? vW : na, "--------- vW ", color.white, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) vMplot = plot(plot_vM ? vM : na, title = "VWAP - Monthly", color = color.blue, style = plot.style_line, transp = 0, linewidth = 1) f_drawLabel(ll_offset, show_labels and plot_vM ? vM : na, "--------- vM ", color.blue, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) vQplot = plot(plot_vQ ? vQ : na, title = "VWAP - Quarter", color = color.green, style = plot.style_line, transp = 0, linewidth = 1) f_drawLabel(ll_offset, show_labels and plot_vQ ? vQ : na, "--------- vQ ", color.green, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) vYplot = plot(plot_vY ? vY : na, title = "VWAP - Year", color = color.yellow, style = plot.style_line, transp = 0, linewidth = 1) f_drawLabel(ll_offset, show_labels and plot_vY ? vY : na, "--------- vY ", color.yellow, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) // ---------- Standard Deviation Calc ---------- // stdev_sel = Std_selection == "Day" ? stdevD : Std_selection == "Week" ? stdevW : Std_selection == "Month" ? stdevM : Std_selection == "Quarter" ? stdevQ : Std_selection == "Year" ? stdevY : na vwap_sel = Std_selection == "Day" ? vD : Std_selection == "Week" ? vW : Std_selection == "Month" ? vM : Std_selection == "Quarter" ? vQ : Std_selection == "Year" ? vY : na prev_period = Std_selection == "Day" ? newSessionD : Std_selection == "Week" ? newSessionW : Std_selection == "Month" ? newSessionM : Std_selection == "Quarter" ? newSessionQ : Std_selection == "Year" ? newSessionY : na // 调用更新后的compute_stDEV函数,传入3倍乘数参数 [s3up, s2up, s1up, s1dn, s2dn, s3dn] = compute_stDEV(vwap_sel, stdev_sel, stDevMultiplier_1, stDevMultiplier_2, stDevMultiplier_3) // 新增±3σ的绘图与标签 e = plot(showStd ? s3up : na, title = "VWAP - STDEV +3", color = color.purple, style = plot.style_linebr, linewidth = 3, transp = 70) f_drawLabel(ll_offset, show_labels and showStd ? s3up : na , " -------- SD+3 ", color.purple, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) a = plot(showStd ? s2up : na, title = "VWAP - STDEV +2", color = color.red, style = plot.style_linebr, linewidth = 3, transp = 70) f_drawLabel(ll_offset, show_labels and showStd ? s2up : na , " -------- SD+2 ", color.red, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) b = plot(showStd ? s1up : na, title = "VWAP - STDEV +1", color = color.gray, style = plot.style_linebr, linewidth = 3, transp = 60) f_drawLabel(ll_offset, show_labels and showStd ? s1up : na , " -------- SD+1 ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) c = plot(showStd ? s1dn : na, title = "VWAP - STDEV -1", color = color.gray, style = plot.style_linebr, linewidth = 3, transp = 60) f_drawLabel(ll_offset, show_labels and showStd ? s1dn : na , " -------- SD-1 ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) d = plot(showStd ? s2dn : na, title = "VWAP - STDEV -2", color = color.blue, style = plot.style_linebr, linewidth = 3, transp = 70) f_drawLabel(ll_offset, show_labels and showStd ? s2dn : na , " -------- SD-2 ", color.blue, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) f = plot(showStd ? s3dn : na, title = "VWAP - STDEV -3", color = color.purple, style = plot.style_linebr, linewidth = 3, transp = 70) f_drawLabel(ll_offset, show_labels and showStd ? s3dn : na , " -------- SD-3 ", color.purple, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) // 新增±3σ的填充颜色 fill_col_up_extra = color.new(color.purple, 95) fill_col_up = color.new(color.red, 95) fill_col_mid = color.new(color.gray, 90) fill_col_down = color.new(color.blue, 95) fill_col_down_extra = color.new(color.purple, 95) // 更新填充逻辑,加入±3σ和±2σ之间的填充 fill(e, a, fill_bands ? fill_col_up_extra : na) fill(a, b, fill_bands ? fill_col_up : na) fill(b, c, fill_bands ? fill_col_mid : na) fill(c, d, fill_bands ? fill_col_down : na) fill(d, f, fill_bands ? fill_col_down_extra : na) // ---------- Previous period levels ---------- // // 更新previouslevels函数,加入±3σ历史水平的保存 previouslevels(period, VWAP, s3up, s2up, s1up, s1dn, s2dn, s3dn) => var float pV = na var float ps3up = na var float ps2up = na var float ps1up = na var float ps1dn = na var float ps2dn = na var float ps3dn = na pV := period ? VWAP[1] : pV ps3up := period ? s3up[1] : ps3up ps2up := period ? s2up[1] : ps2up ps1up := period ? s1up[1] : ps1up ps1dn := period ? s1dn[1] : ps1dn ps2dn := period ? s2dn[1] : ps2dn ps3dn := period ? s3dn[1] : ps3dn [ pV, ps3up, ps2up, ps1up, ps1dn, ps2dn, ps3dn ] // 调用更新后的previouslevels函数 [ pV, ps3up, ps2up, ps1up, ps1dn, ps2dn, ps3dn ] = previouslevels(prev_period, vwap_sel, s3up, s2up, s1up, s1dn, s2dn, s3dn) // ---------- Plot previous levels ---------- // pVplot = plot(plot_pL1 ? pV : na, title = "Previous VWAP", color = color.orange, style = plot.style_linebr, transp = 60, linewidth = 1) f_drawLabel(ll_offset, show_plabels and plot_pL1 ? pV : na, "--------- pVWAP ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) // 新增历史±3σ水平的绘图与标签 pe = plot(plot_pL3 ? ps3up : na, title = "Previous VWAP - STDEV +3", color = color.purple, style = plot.style_linebr, linewidth = 1, transp = 70) f_drawLabel(ll_offset, show_plabels and plot_pL3 ? ps3up : na , " -------- pSD+3 ", color.purple, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) pa = plot(plot_pL2 ? ps2up : na, title = "Previous VWAP - STDEV +2", color = color.red, style = plot.style_linebr, linewidth = 1, transp = 70) f_drawLabel(ll_offset, show_plabels and plot_pL2 ? ps2up : na , " -------- pSD+2 ", color.red, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) pb = plot(plot_pL1 ? ps1up : na, title = "Previous VWAP - STDEV +1", color = color.gray, style = plot.style_linebr, linewidth = 1, transp = 60) f_drawLabel(ll_offset, show_plabels and plot_pL1 ? ps1up : na , " -------- pSD+1 ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) pc = plot(plot_pL1 ? ps1dn : na, title = "Previous VWAP - STDEV -1", color = color.gray, style = plot.style_linebr, linewidth = 1, transp = 60) f_drawLabel(ll_offset, show_plabels and plot_pL1 ? ps1dn : na , " -------- pSD-1 ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) pd = plot(plot_pL2 ? ps2dn : na, title = "Previous VWAP - STDEV -2", color = color.blue, style = plot.style_linebr, linewidth = 1, transp = 70) f_drawLabel(ll_offset, show_plabels and plot_pL2 ? ps2dn : na , " -------- pSD-2 ", color.blue, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) pf = plot(plot_pL3 ? ps3dn : na, title = "Previous VWAP - STDEV -3", color = color.purple, style = plot.style_linebr, linewidth = 1, transp = 70) f_drawLabel(ll_offset, show_plabels and plot_pL3 ? ps3dn : na , " -------- pSD-3 ", color.purple, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) // 更新历史波段的填充逻辑 fill(pe, pa, fill_pbands ? fill_col_up_extra : na) fill(pa, pb, fill_pbands ? fill_col_up : na) fill(pb, pc, fill_pbands ? fill_col_mid : na) fill(pc, pd, fill_pbands ? fill_col_down : na) fill(pd, pf, fill_pbands ? fill_col_down_extra : na)
内容的提问来源于stack exchange,提问作者Notorious
相关产品推荐
相关产品推荐

