# Extend High and lows to the right of the chart

#### nitrous

##### Member
I have the following code, but I would like the green/red horizontal lines extended all the way to the end of the chart for the whole day.

Played along with the plot condition, but it just messes up the chart. Thanks

Image example, see arrows:

Code:
``````# | define a peak and plot it
input magnitude = 5;

def peak = high > Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakValue = if peak then high else peakValue[1];
plot peakLine = peakValue;
peakLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
peakLine.SetDefaultColor(Color.green);

# | get the bar numbers for the most recent close
# | and the most recent peak
def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
def peakBar = if peak then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastPeakValue = GetValue(high, BarNumber() - HighestAll(peakBar));
def prevPeakValue = GetValue(peakValue[1], BarNumber() - HighestAll(peakBar));

# | find the value of the most recent close
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);

# | define what is considered to be "in range" of the previous peak
input percent = 0.5;

def inRange = mostRecentClose > (prevPeakValue * (1 - percent / 100)) and mostRecentClose < (prevPeakValue * (1 + percent / 100));

# | extend the most recent peak
plot lastPeakExtension = if BarNumber() >= HighestAll(peakBar) then lastPeakValue else Double.NaN;
lastPeakExtension.SetDefaultColor(Color.light_green);
# | extend the previous peak only if the most recent close value is "in range"
plot prevPeakExtension = if BarNumber() >= HighestAll(peakBar) - 1 then prevPeakValue else Double.NaN;
prevPeakExtension.SetDefaultColor(Color.Dark_Orange);
prevPeakExtension.SetHiding(!inRange);

# | define a valley and plot it

def valley = low < lowest(low[1], magnitude) and low <= lowest(low[-magnitude], magnitude);
def valleyValue = if valley then low else valleyValue[1];
plot valleyLine = valleyValue;
valleyLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
valleyLine.SetDefaultColor(Color.red);

# | get the bar numbers for the most recent close
# | and the most recent valley

def valleyBar = if valley then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastvalleyValue = GetValue(low, BarNumber() - lowestAll(valleyBar));
def prevvalleyValue = GetValue(valleyValue[1], BarNumber() - lowestAll(valleyBar));``````

Last edited:
I have the following code, but I would like the green/red horizontal lines extended all the way to the end of the chart for the whole day.

Played along with the plot condition, but it just messes up the chart. Thanks

Image example, see arrows:

Code:
``````# | define a peak and plot it
input magnitude = 5;

def peak = high > Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakValue = if peak then high else peakValue[1];
plot peakLine = peakValue;
peakLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
peakLine.SetDefaultColor(Color.green);

# | get the bar numbers for the most recent close
# | and the most recent peak
def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
def peakBar = if peak then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastPeakValue = GetValue(high, BarNumber() - HighestAll(peakBar));
def prevPeakValue = GetValue(peakValue[1], BarNumber() - HighestAll(peakBar));

# | find the value of the most recent close
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);

# | define what is considered to be "in range" of the previous peak
input percent = 0.5;

def inRange = mostRecentClose > (prevPeakValue * (1 - percent / 100)) and mostRecentClose < (prevPeakValue * (1 + percent / 100));

# | extend the most recent peak
plot lastPeakExtension = if BarNumber() >= HighestAll(peakBar) then lastPeakValue else Double.NaN;
lastPeakExtension.SetDefaultColor(Color.light_green);
# | extend the previous peak only if the most recent close value is "in range"
plot prevPeakExtension = if BarNumber() >= HighestAll(peakBar) - 1 then prevPeakValue else Double.NaN;
prevPeakExtension.SetDefaultColor(Color.Dark_Orange);
prevPeakExtension.SetHiding(!inRange);

# | define a valley and plot it

def valley = low < lowest(low[1], magnitude) and low <= lowest(low[-magnitude], magnitude);
def valleyValue = if valley then low else valleyValue[1];
plot valleyLine = valleyValue;
valleyLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
valleyLine.SetDefaultColor(Color.red);

# | get the bar numbers for the most recent close
# | and the most recent valley

def valleyBar = if valley then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastvalleyValue = GetValue(low, BarNumber() - lowestAll(valleyBar));
def prevvalleyValue = GetValue(valleyValue[1], BarNumber() - lowestAll(valleyBar));``````

i was going to say, but the image shows all the lines going to the end of the chart....
i think you manually drew the thicker red lines, but failed to mention it. (was confusing)

currently, the previous line stops when a new condition exits to start another line. 1 plot variable can have only 1 value. so if you want multiple lines occuring during the same bar, you will need more variables/formulas, a set for each additional line. if you want 4 peak lines at a time, then you need 4 sets of formulas.

here is an example to add 1 extension line. there is another post on that forum that has several lines. maybe searching for magnitude will find it? look for posts by robert use advanced search and search all dates. i haven't found the post i was thinking of yet...

Last edited:
i was going to say, but the image shows all the lines going to the end of the chart....
i think you manually drew the thicker red lines, but failed to mention it. (was confusing)

currently, the previous line stops when a new condition exits to start another line. 1 plot variable can have only 1 value. so if you want multiple lines occuring during the same bar, you will need more variables/formulas, a set for each additional line. if you want 4 peak lines at a time, then you need 4 sets of formules.
Thanks, yes I drew the lines myself , sorry forgot to mention it.
Sounds like it will be just a lot more work to modify the code to achieve what I want.

mall

I have the following code, but I would like the green/red horizontal lines extended all the way to the end of the chart for the whole day.

Played along with the plot condition, but it just messes up the chart. Thanks

Image example, see arrows:

Code:
``````# | define a peak and plot it
input magnitude = 5;

def peak = high > Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakValue = if peak then high else peakValue[1];
plot peakLine = peakValue;
peakLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
peakLine.SetDefaultColor(Color.green);

# | get the bar numbers for the most recent close
# | and the most recent peak
def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
def peakBar = if peak then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastPeakValue = GetValue(high, BarNumber() - HighestAll(peakBar));
def prevPeakValue = GetValue(peakValue[1], BarNumber() - HighestAll(peakBar));

# | find the value of the most recent close
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);

# | define what is considered to be "in range" of the previous peak
input percent = 0.5;

def inRange = mostRecentClose > (prevPeakValue * (1 - percent / 100)) and mostRecentClose < (prevPeakValue * (1 + percent / 100));

# | extend the most recent peak
plot lastPeakExtension = if BarNumber() >= HighestAll(peakBar) then lastPeakValue else Double.NaN;
lastPeakExtension.SetDefaultColor(Color.light_green);
# | extend the previous peak only if the most recent close value is "in range"
plot prevPeakExtension = if BarNumber() >= HighestAll(peakBar) - 1 then prevPeakValue else Double.NaN;
prevPeakExtension.SetDefaultColor(Color.Dark_Orange);
prevPeakExtension.SetHiding(!inRange);

# | define a valley and plot it

def valley = low < lowest(low[1], magnitude) and low <= lowest(low[-magnitude], magnitude);
def valleyValue = if valley then low else valleyValue[1];
plot valleyLine = valleyValue;
valleyLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
valleyLine.SetDefaultColor(Color.red);

# | get the bar numbers for the most recent close
# | and the most recent valley

def valleyBar = if valley then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastvalleyValue = GetValue(low, BarNumber() - lowestAll(valleyBar));
def prevvalleyValue = GetValue(valleyValue[1], BarNumber() - lowestAll(valleyBar));``````

See if this helps. It is an adaption of swing high/low code that allowed an input number of extended swings to show. You can add more extended lines by the same method shown in the code, but regrettably it is a 'brute force' method.

Code:
``````input extlines_to_show = 5;

# | define a peak and plot it
input magnitude = 5;

def peak = high > Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakValue = if peak then high else peakValue[1];
plot peakLine = peakValue;
peakLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
peakLine.SetDefaultColor(Color.GREEN);

# | get the bar numbers for the most recent close
# | and the most recent peak
def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
def peakBar = if peak then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastPeakValue = GetValue(high, BarNumber() - HighestAll(peakBar));
def prevPeakValue = GetValue(peakValue[1], BarNumber() - HighestAll(peakBar));

# | find the value of the most recent close
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);

# | define what is considered to be "in range" of the previous peak
input percent = 0.5;

def inRange = mostRecentClose > (prevPeakValue * (1 - percent / 100)) and mostRecentClose < (prevPeakValue * (1 + percent / 100));

# | extend the most recent peak
plot lastPeakExtension = if BarNumber() >= HighestAll(peakBar) then lastPeakValue else Double.NaN;
lastPeakExtension.SetDefaultColor(Color.LIGHT_GREEN);
# | extend the previous peak only if the most recent close value is "in range"
plot prevPeakExtension = if BarNumber() >= HighestAll(peakBar) - 1 then prevPeakValue else Double.NaN;
prevPeakExtension.SetDefaultColor(Color.DARK_ORANGE);
prevPeakExtension.SetHiding(!inRange);

# | define a valley and plot it

def valley = low < Lowest(low[1], magnitude) and low <= Lowest(low[-magnitude], magnitude);
def valleyValue = if valley then low else valleyValue[1];
plot valleyLine = valleyValue;
valleyLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
valleyLine.SetDefaultColor(Color.RED);

# | get the bar numbers for the most recent close
# | and the most recent valley

def valleyBar = if valley then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastvalleyValue = GetValue(low, BarNumber() - LowestAll(valleyBar));
def prevvalleyValue = GetValue(valleyValue[1], BarNumber() - LowestAll(valleyBar));

####################
#Modification using swing high/low code to extend the peak/valley code lines

def lsh       = if IsNaN(peakbar)
then lsh[1]
else if peakline
then high
else lsh[1];
def lsl       = if IsNaN(valleybar)
then lsl[1]
else if valleyLine
then low
else lsl[1];

#Counts Number of Peaks/Valleys - Used to limit plot of extended lines to input number
def hcount    = if peakLine   then 1 else hcount[1] + 1;
def lcount    = if valleyLine then 1 else lcount[1] + 1;

#First Plots
def lslext    = if IsNaN(lsl) then lslext[1] else lsl;
plot lslextp  = lslext;
lslextp.SetDefaultColor(Color.RED);
lslextp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def lshext    = if IsNaN(lsh) then lshext[1] else lsh;
plot lshextp  = lshext;
lshextp.SetDefaultColor(Color.GREEN);
lshextp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

def lowlsl    = if lsl > 0 then lsl else Double.NaN;
def highlsh   = if lsh > 0 then lsh else Double.NaN;

#Second Plots
def lslext1   = if lowlsl[1] != lowlsl   then lowlsl[1] else lslext1[1];
plot lslextp1 = if extlines_to_show >= 2 then lslext1 else Double.NaN;
lslextp1.SetDefaultColor(Color.RED);
lslextp1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def lshext1   = if highlsh[1] != highlsh then highlsh[1] else lshext1[1];
plot lshextp1 = if extlines_to_show >= 2 then lshext1 else Double.NaN;
lshextp1.SetDefaultColor(Color.GREEN);
lshextp1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#Third Plots
def   lslext2 = if lslext1 != lslext1[1] then lslext1[1] else lslext2[1];
plot lslextp2 = if extlines_to_show >= 3 then lslext2 else Double.NaN;
lslextp2.SetDefaultColor(Color.RED);
lslextp2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def lshext2   = if lshext1 != lshext1[1] then lshext1[1] else lshext2[1];
plot lshextp2 = if extlines_to_show >= 3 then lshext2 else Double.NaN;
lshextp2.SetDefaultColor(Color.GREEN);
lshextp2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#Fourth Plots
def lslext3   = if lslext2 != lslext2[1] then lslext2[1] else lslext3[1];
plot lslextp3 = if extlines_to_show >= 4 then lslext3 else Double.NaN;
lslextp3.SetDefaultColor(Color.RED);
lslextp3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def lshext3 = if lshext2 != lshext2[1]   then lshext2[1] else lshext3[1];
plot lshextp3 = if extlines_to_show >= 4 then lshext3 else Double.NaN;
lshextp3.SetDefaultColor(Color.GREEN);
lshextp3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#To create more extended lines then copy the below and change based upon how the Fourth Plots was changed to create the Fifth Plots. Regrettably only this 'brute force' method is needed
#Fifth Plots
def lslext4   = if lslext3 != lslext3[1] then lslext3[1] else lslext4[1];
plot lslextp4 = if extlines_to_show >= 5 then lslext4 else Double.NaN;
lslextp4.SetDefaultColor(Color.RED);
lslextp4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def lshext4 = if lshext3 != lshext3[1]   then lshext3[1] else lshext4[1];
plot lshextp4 = if extlines_to_show >= 5 then lshext4 else Double.NaN;
lshextp4.SetDefaultColor(Color.GREEN);
lshextp4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);``````

See if this helps. It is an adaption of swing high/low code that allowed an input number of extended swings to show. You can add more extended lines by the same method shown in the code, but regrettably it is a 'brute force' method.
This is awesome, I will play around with it.

Thanks

87k+ Posts
172 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?