How to stop the line.new() extend conditionally?

Post Reply
jackbravo
New Trader
Posts: 2
Joined: Mon Dec 23, 2024 3:35 am

How to stop the line.new() extend conditionally?

Post by jackbravo »

Hi, I'm new to coding and pinescript, and just know enough to dabble.
I'm trying to make a swing failure indicator using the line.new() function. I was able to create it but I'm not sure how to stop it. Specifically, I'd like the line to stop when there is a close above. Right now, I'm just getting 100s of lines. Any ideas?

Code: Select all

//@version=6
indicator("Swing Failure", overlay=true)

sfw1 = high > high[1] and close < high[1] and close < open
sfw2 = high < high[1] and close < open[1] and close < open and close[1] > open[1]

sfw3 = low < low[1] and close > low[1] and close > open and close[1] < open[1]

// Initialize a horizontal line at a fixed price level
var line hl_line = na  // Track the horizontal line object
var line hl_line2 = na  // Track the horizontal line object

if (sfw1 or sfw2) and barstate.isconfirmed
    hl_line := line.new(x1=bar_index[1], y1=high[1], x2=bar_index + 5, y2=high[1], color=color.rgb(0, 0, 0), width=2, style=line.style_solid, extend=extend.right)

if (sfw3) and barstate.isconfirmed
    hl_line2 := line.new(x1=bar_index[1], y1=low[1], x2=bar_index + 5, y2=low[1], color=color.blue, width=2, style=line.style_solid, extend=extend.right)
Thanks!
SamiFX
Expert Trader
Posts: 7
Joined: Thu Nov 30, 2023 9:44 am

Re: How to stop the line.new() extend conditionally?

Post by SamiFX »

Do you want the lines only to extent till a cnadle breaks it? Or do you want to only draw the latest and remove the rest?
jackbravo
New Trader
Posts: 2
Joined: Mon Dec 23, 2024 3:35 am

Re: How to stop the line.new() extend conditionally?

Post by jackbravo »

Thanks for the reply.
I'd like to keep the current as well as the old ones, unless it's going to cause slowness. I don't need maybe more than 10 or 20.
Post Reply