动态标签文本被“覆盖”

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

我遇到了我认为是 pinescript 或 tradeview 本身的错误

我有一个脚本,我正在绘制多个 VWAPS

这些 vwap 上有标签,动态显示价格变化百分比需要移动才能达到目标

这里的问题是标签就像覆盖了之前的文本(附图)

初始标签未被删除。即使我有标签删除逻辑。

我相信这是一个错误的原因是它并不总是发生!它们有时效果很好。

具体来说,如果我禁用/重新启用标签,它们会完美工作,直到我更改时间范围。

我尝试了很多“修复”,但没有任何效果。

这是 MR 代码块:是的,我也尝试过 label.settext,它仍然具有完全相同的行为。

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © traderview2

//@version=5
indicator("VWAP TESTING", overlay = true)

import PineCoders/ConditionalAverages/1 as pc

///------------------------------------------- START USER INPUTS  --------------------------------------------///

// Enable/Disable & length - VWAPs
eDV = "-------- Enable VWAPs --------"
rvOn1 = input.bool(true, "R-VWAP #1:", group = eDV, inline = "rv1")
tfI1 = input.timeframe("1D", "Period", group = eDV, inline = "rv1")
rvOn2 = input.bool(true, "R-VWAP #1:", group = eDV, inline = "rv1")
tfI2 = input.timeframe("1W", "Period", group = eDV, inline = "rv1")

// Main VWAP Settings
m_V = "-------- Main VWAP Settings --------"
rVsrc = input.source(hlc3, "Rolling VWAP Source", group = m_V)
drawVLabels = input.bool(true, "Show VWAP Labels?", group = m_V)
textSize = input.string(defval='Normal', title='Label Text Size', 
 options=['Tiny', 'Small', 'Normal', 'Large', 'Huge'], group = m_V) 

// 1st  Rolling VWAP Inputs
r_v1 = "Rolling VWAP #1"
stdevM1 = input.float(0, "Bands Multiplier", group = r_v1, minval = 0.0, step = 0.5)
rV1color = input.color(color.orange, "Color", group = r_v1)

// 2nd  Rolling VWAP Inputs
r_v2 = "Rolling VWAP #2"
stdevM2 = input.float(0, "Bands Multiplier", group = r_v1, minval = 0.0, step = 0.5)
rV2color = input.color(color.green, "Color", group = r_v1)



///------------------------------------------- START ROLLING VWAP CODE --------------------------------------------///

// Function to calculate Rolling VWAP and bands
RollingVWAP(src, vol, tf, stdevMult) =>
    float sumSrcVol = pc.totalForTimeWhen(src * vol, timeframe.in_seconds(tf) * 1000, true, 10)
    float sumVol = pc.totalForTimeWhen(vol, timeframe.in_seconds(tf) * 1000, true, 10)
    float stdSrcVol = pc.totalForTimeWhen(vol * math.pow(src, 2), timeframe.in_seconds(tf) * 1000, true, 10)
    float rVWAP = sumSrcVol / sumVol
    
    // Calculate bands
    float rV = stdSrcVol / sumVol - math.pow(rVWAP, 2)
    rV := math.max(0, rV)
    float stDev = math.sqrt(rV)
    float upperBand = rVWAP + (stDev * stdevMult)
    float lowerBand = rVWAP - (stDev * stdevMult)
    
    [rVWAP, upperBand, lowerBand]

// Calculate each Rolling VWAP with its respective inputs
[rVWAP1, uppB1, lowB1] = RollingVWAP(rVsrc, volume, tfI1, stdevM1)
[rVWAP2, uppB2, lowB2] = RollingVWAP(rVsrc, volume, tfI2, stdevM2)


///------------------------------------------- START ROLLING VWAP PLOTS --------------------------------------------///

// Plotting Rolling VWAP #1
plot(rvOn1 ? rVWAP1 : na, "rVWAP #1", rV1color, style = plot.style_stepline)
uR1 = plot(rvOn1 and stdevM1 != 0 ? uppB1 : na, "rVWAP #1 Upper Band", color.new(rV1color, 50))
lR1 = plot(rvOn1 and stdevM1 != 0 ? lowB1 : na, "rVWAP #1 Lower Band", color.new(rV1color, 50))
fill(uR1, lR1, color.new(rV1color, 95), "rVWAP #1 Bands Fill")

// Plotting Rolling VWAP #1
plot(rvOn2 ? rVWAP2 : na, "rVWAP #2", rV2color, style = plot.style_stepline)
uR2 = plot(rvOn2 and stdevM2 != 0 ? uppB1 : na, "rVWAP #2 Upper Band", color.new(rV2color, 50))
lR2 = plot(rvOn2 and stdevM2 != 0 ? lowB1 : na, "rVWAP #2 Lower Band", color.new(rV2color, 50))
fill(uR2, lR2, color.new(rV2color, 95), "rVWAP #2 Bands Fill")


///------------------------------------------- START ROLLING VWAP LABELS --------------------------------------------///

//Calculate % deviation
rvwapPer1 = close > rVWAP1 ? -((close - rVWAP1) / close) * 100 : ((rVWAP1 - close) / rVWAP1) * 100

rvwapPer2 = close > rVWAP2 ? -((close - rVWAP2) / close) * 100 : ((rVWAP2 - close) / rVWAP2) * 100

//Draw RVWAP 1 lines & labels
var label rVWAP1_label = na
var line rVWAP1_line = na

if rvOn1 and drawVLabels
    // Delete previous line and label if they exist
    if not na(rVWAP1_label)
        label.delete(rVWAP1_label)
    if not na(rVWAP1_line)
        line.delete(rVWAP1_line)

    // Create new VWAP line and label
    rVWAP1_line := line.new(x1=bar_index + 5,
         y1=rVWAP1, 
         x2=bar_index + 11, 
         y2=rVWAP1, 
         color=rV1color, 
         style=line.style_dotted, 
         width=1)
        
    rVWAP1_label := label.new(x=bar_index + 15,
         y=rVWAP1,
         text=str.tostring(tfI1) + "_" + "rVWAP" + "(" + str.tostring(rvwapPer1, format.percent) +")",
         textcolor=rV1color, 
         color=color.new(color.white, 100), 
         size=size.large,
         style=label.style_label_left, 
         text_font_family=font.family_monospace)

//Draw RVWAP 2 lines & labels
var label rVWAP2_label = na
var line rVWAP2_line = na

if rvOn2 and drawVLabels
    // Delete previous line and label if they exist
    if not na(rVWAP2_label)
        label.delete(rVWAP2_label)
    if not na(rVWAP2_line)
        line.delete(rVWAP2_line)

    // Create new VWAP line and label
    rVWAP2_line := line.new(x1=bar_index + 5,
         y1=rVWAP2, 
         x2=bar_index + 11, 
         y2=rVWAP2, 
         color=rV2color, 
         style=line.style_dotted, 
         width=1)
        
    rVWAP2_label := label.new(x=bar_index + 15,
         y=rVWAP2,
         text=str.tostring(tfI2) + "_" + "rVWAP" + "(" + str.tostring(rvwapPer2, format.percent) +")",
         textcolor=rV2color, 
         color=color.new(color.white, 100), 
         size=size.large,
         style=label.style_label_left, 
         text_font_family=font.family_monospace)

我尝试了很多方法来删除文本(如果%发生变化)并替换它,但没有任何效果。

我相信这“可能”是一个错误的原因是,如果我禁用指标中的标签并重新启用它们,它会完美地工作,直到我切换图表时间范围,我必须再次执行此操作...图像显示文本“重叠”

text label pine-script pine-script-v5
1个回答
0
投票

您无需删除旧标签并在每个栏上创建新标签,只需修改标签对象即可。这也将有助于您提高脚本的性能。

//Draw RVWAP 1 lines & labels
var label rVWAP1_label = label.new(na, na,na, textcolor=rV1color, color=color.new(color.white, 100), size=size.large, style=label.style_label_left, text_font_family=font.family_monospace)
var line rVWAP1_line = na

if rvOn1 and drawVLabels
    // Delete previous line and label if they exist
    // Create new VWAP line and label
    rVWAP1_line := line.new(x1=bar_index + 5,
         y1=rVWAP1, 
         x2=bar_index + 11, 
         y2=rVWAP1, 
         color=rV1color, 
         style=line.style_dotted, 
         width=1)

    label.set_xy(rVWAP1_label, bar_index + 15, rVWAP1)
    label.set_text(rVWAP1_label, str.tostring(tfI1) + "_" + "rVWAP" + "(" + str.tostring(rvwapPer1, format.percent) +")")
© www.soinside.com 2019 - 2024. All rights reserved.