Search code examples
pine-scriptpine-script-v5

show lines only for today


I would like these lines only show just for today and doesn't extend backward. I tried like in this post and plenty others, nothing works. I'm not good at this so maybe I did it wrong or something. Please help. Here is the script

//@version=5
indicator(title="_CM_High_Low_Open_Close", shorttitle="_CM_H_L_O_C", overlay=true)

yellow_color = color.new(color.yellow, 0)

// Inputs
st = input(true, title="THL")
swt = input(false, title="WHL")
swy = input(false, title="Prvs-WHL")
smh = input(false, title="MHL")
spmh = input(false, title="Prvs-MHL")

// Daily
ph = request.security(syminfo.tickerid, "D", high)
pl = request.security(syminfo.tickerid, "D", low)

// Weekly
wph = request.security(syminfo.tickerid, "W", high)
wpdh = request.security(syminfo.tickerid, "W", high[1])
wpl = request.security(syminfo.tickerid, "W", low)
wpdl = request.security(syminfo.tickerid, "W", low[1])

// Monthly
mph = request.security(syminfo.tickerid, "M", high)
mpdh = request.security(syminfo.tickerid, "M", high[1])
mpl = request.security(syminfo.tickerid, "M", low)
mpdl = request.security(syminfo.tickerid, "M", low[1])

// Daily Plots
plot(st and ph ? ph : na, title="TH", style=plot.style_line, linewidth=2, color=ta.change(ph) != 0 ? na : yellow_color)
plot(st and pl ? pl : na, title="TL", style=plot.style_line, linewidth=2, color=ta.change(ph) != 0 ? na : yellow_color)

//Weekly Plots
plot(swt and wph ? wph : na, title="WH", style=plot.style_line, linewidth=3, color=ta.change(ph) != 0 ? na : yellow_color)
plot(swy and wpdh ? wpdh : na, title="Prvs-WH", style=plot.style_line, linewidth=3, color=ta.change(ph) != 0 ? na : yellow_color)
plot(swt and wpl ? wpl : na, title="WL", style=plot.style_line, linewidth=3, color=ta.change(ph) != 0 ? na : yellow_color)
plot(swy and wpdl ? wpdl : na, title="Prvs-WL", style=plot.style_line, linewidth=3, color=ta.change(ph) != 0 ? na : yellow_color)

//Monthly Plots
plot(smh and mph ? mph : na, title="MH", style=plot.style_line, linewidth=4, color=ta.change(ph) != 0 ? na : yellow_color)
plot(spmh and mpdh ? mpdh : na, title="Prvs-MH", style=plot.style_line, linewidth=4, color=ta.change(ph) != 0 ? na : yellow_color)
plot(smh and mpl ? mpl : na, title="ML", style=plot.style_line, linewidth=4, color=ta.change(ph) != 0 ? na : yellow_color)
plot(spmh and mpdl ? mpdl : na, title="Prvs-ML", style=plot.style_line, linewidth=4, color=ta.change(ph) != 0 ? na : yellow_color)

enter image description here

enter image description here


Solution

  • You cannot delete historical values of a plot. You need to use line for that.

    Here is how you would do it for TH.

    is_new_day = timeframe.change("D")
    
    var line ph_line = na
    
    if (is_new_day)
        if (st and ph)
            ph_line := line.new(bar_index - 1, ph, bar_index + 1, ph, color=yellow_color)   // Create the new line
            line.delete(ph_line[1])                                                         // Delete the previous line
    else                                                                                    // If it is not a new day
        if (st)
            line.set_x2(ph_line, bar_index)                                                 // Extend the line