Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to delete a line on condition after it has been plotted.

So Im going through 2 steps;

  1. Plot lines ONLY in the last X bars

  2. Delete lines if a condition is met.

For the second case, it is not detecting any lines on the chart, I'm trying to put up a label everytime a line has been found however the labels do not plot. Here is what I've tried so far:

//@version=4
study(title='Testing', overlay=true, max_lines_count=30)

showLines = input(title="Show Lines ?", type=input.bool, defval=true)

if showLines == true and barstate.islast

    line bullLine1 = na //bull line
    
    BARS_BACK = 20

    //Loop to plot a line
    for i = BARS_BACK to 0                
        bullishcase = close[i+1] > high[i+2]

        if bullishcase
            bullLine1 := line.new(x1=bar_index[i + 2], y1=high[i + 2], x2=bar_index[i], y2=high[i + 2], extend=extend.right, color=color.blue)

    //Loop to delete a line
    for i = BARS_BACK to 0
        if not na(bullLine1[i])
            label.new(bar_index[i], high[i], "Line 1 found" )

I tried another approach to this issue by using this solution: How to delete a line when price breaks it in pine script? However, it works most of the times but sometimes it gives me the error "too many drawings. cannot clean the oldest". I'm not sure how I can go about that

question from:https://stackoverflow.com/questions/65649754/tradingview-delete-line-on-condition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

As realtime bars close, this will delete lines outside the last 20 bars.

//@version=4
study(title='Testing', overlay=true, max_lines_count=30)

showLines = input(title="Show Lines ?", type=input.bool, defval=true)

BARS_BACK = 20
line bullLine1 = na //bull line
if showLines == true and barstate.islast
    //Loop to plot a line
    for i = BARS_BACK to 0                
        bullishcase = close[i+1] > high[i+2]

        if bullishcase
            bullLine1 := line.new(x1=bar_index[i + 2], y1=high[i + 2], x2=bar_index[i], y2=high[i + 2], extend=extend.right, color=color.blue)

// If there is one, delete line outside the allowed set of BARS_BACK bars.
line.delete(bullLine1[BARS_BACK + 1])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...