# Plot line for previous "x" bars only

#### Sabs

##### New member
Hi,
I am trying to plot a line at the high and low values of the previous three 15m bars. Rather than continuous lines, I'd like the line to extend back only to the first of the three lookback bars (as shown in the image below).

I've been trying to modify the "count" code in this post for a 15m period but I've been unable to extend it longer than one bar. I've also tried implementing "BarNumber()" but haven't wrapped my head around it. Any help would be appreciated.

Here is the code so far, which creates continuous lines:

Code:
``````input NumberOfBarsPast = 3;
input AggPeriod = AggregationPeriod.FIFTEEN_MIN;
def l = low(period = AggPeriod);
def h = high(period = AggPeriod);
def LowestBar = Lowest(l, NumberOfBarsPast);
def HighestBar = Highest(h, NumberOfBarsPast);

plot LowLine = LowestBar;
LowLine.SetDefaultColor(Color.DARK_RED);
plot HighLine = HighestBar;
HighLine.SetDefaultColor(Color.DARK_GREEN);``````

Last edited:
Solution
Hi,
I am trying to plot a line at the high and low values of the previous three 15m bars. Rather than continuous lines, I'd like the line to extend back only to the first of the three lookback bars (as shown in the image below).

I've been trying to modify the "count" code in this post for a 15m period but I've been unable to extend it longer than one bar. I've also tried implementing "BarNumber()" but haven't wrapped my head around it. Any help would be appreciated.

View attachment 21114

Here is the code so far, which creates continuous lines:

Code:
``````input NumberOfBarsPast = 3;
input AggPeriod = AggregationPeriod.FIFTEEN_MIN;
def l = low(period = AggPeriod);
def h = high(period = AggPeriod);
def LowestBar = Lowest(l, NumberOfBarsPast)...``````
Hi,
I am trying to plot a line at the high and low values of the previous three 15m bars. Rather than continuous lines, I'd like the line to extend back only to the first of the three lookback bars (as shown in the image below).

I've been trying to modify the "count" code in this post for a 15m period but I've been unable to extend it longer than one bar. I've also tried implementing "BarNumber()" but haven't wrapped my head around it. Any help would be appreciated.

View attachment 21114

Here is the code so far, which creates continuous lines:

Code:
``````input NumberOfBarsPast = 3;
input AggPeriod = AggregationPeriod.FIFTEEN_MIN;
def l = low(period = AggPeriod);
def h = high(period = AggPeriod);
def LowestBar = Lowest(l, NumberOfBarsPast);
def HighestBar = Highest(h, NumberOfBarsPast);

plot LowLine = LowestBar;
LowLine.SetDefaultColor(Color.DARK_RED);
plot HighLine = HighestBar;
HighLine.SetDefaultColor(Color.DARK_GREEN);``````

See if this does what you were requestin

1a. It identifies the high/low=highesthigh/lowestlows of the last 3,
1b. Counts these with the sum(), starting at 1 until the sum() is no longer is true, which returns a zero
1c. Then another sum() will start at 1 until all the bars are read.
2. The counts = 1 will identify the respective high/low and extend it until the next count == 1.

The image shows the results of the same script on both a 3m and 15m charts.

Code:
``````#HigherHighs_LowerLows_Plot_from_first_Occurance_of_String_of_These

input NumberOfBarsPast = 3;
input AggPeriod = AggregationPeriod.FIFTEEN_MIN;
def l = low(period = AggPeriod);
def h = high(period = AggPeriod);

##########

def Hcount =
if Sum(h == Highest(h, NumberOfBarsPast), 1) >= 1
then Hcount[1] + 1
else if
Sum(h == Highest(h, NumberOfBarsPast), 1) < 1
then 0
else Hcount[1];
def HH = if Hcount == 1 then high else HH[1];

plot HighLine = HH;
HighLine.SetDefaultColor(Color.DARK_GREEN);
HighLine.SetPaintingStrategy(PaintingStrategy.DASHES);

##########

def Lcount =
if Sum(l == Lowest(l, NumberOfBarsPast), 1) >= 1
then Lcount[1] + 1
else if
Sum(l == Lowest(l, NumberOfBarsPast), 1) < 1
then 0
else Lcount[1];
def LL = if Lcount == 1 then low else LL[1];

plot LowLine = LL;
LowLine.SetDefaultColor(Color.DARK_RED);
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#########``````

Hi,
I am trying to plot a line at the high and low values of the previous three 15m bars. Rather than continuous lines, I'd like the line to extend back only to the first of the three lookback bars (as shown in the image below).

I've been trying to modify the "count" code in this post for a 15m period but I've been unable to extend it longer than one bar. I've also tried implementing "BarNumber()" but haven't wrapped my head around it. Any help would be appreciated.

Here is the code so far, which creates continuous lines:

Code:
``````input NumberOfBarsPast = 3;
input AggPeriod = AggregationPeriod.FIFTEEN_MIN;
def l = low(period = AggPeriod);
def h = high(period = AggPeriod);
def LowestBar = Lowest(l, NumberOfBarsPast);
def HighestBar = Highest(h, NumberOfBarsPast);

plot LowLine = LowestBar;
LowLine.SetDefaultColor(Color.DARK_RED);
plot HighLine = HighestBar;
HighLine.SetDefaultColor(Color.DARK_GREEN);``````

if sleepyz code isn't quite what you are looking for, here is another version.
it draws high , low, lines , x bars back from the last bar.
default is 6 bars back

Code:
``````#hilo_lines_xbars_back

#Plot line for previous "x" bars only

def na = double.nan;
def bn = barnumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);

def c = close;

# how many bars back
input bars = 6;

# find bar, x bars back from last bar
def first = !isNaN(close[-bars]) and isNaN(close[-(bars+1)]);

def hi = if bn == 1 or isnan(close) then 0
else if first then highest(high[-(bars+0)], (bars+1))
else hi[1];

def lo = if bn == 1 or isnan(close) then 0
else if first then lowest(low[-(bars+0)], bars+1)
else lo[1];

plot zhi = if hi > 0 then hi else na;
plot zlo = if lo > 0 then lo else na;
zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

input test_line = no;
#``````

#### Attachments

• img1.JPG
46.8 KB · Views: 41
Thank you SleepyZ and halcyonguy - both are great and closer than what I could get.

@halcyonguy - one small request - can you think of a way to have your code disregard the high or low value of the current bar?
What I have in mind is the current bar breaking through the hi/lo plot as shown in the screenshot below. Thanks again, you guys are pros!

#### Attachments

• tos 3 bar.PNG
4.2 KB · Views: 20
Thank you SleepyZ and halcyonguy - both are great and closer than what I could get.

@halcyonguy - one small request - can you think of a way to have your code disregard the high or low value of the current bar?
What I have in mind is the current bar breaking through the hi/lo plot as shown in the screenshot below. Thanks again, you guys are pros!

This excludes the last bar in the lookback

Code:
``````#HigherHighs_LowerLows_Plot_from_first_Occurance_of_String_of_These

input NumberOfBarsPast = 3;

def l = low;
def h = high;
def c = close;
def bn = BarNumber() ;
def na = Double.NaN;
def last = HighestAll(if IsNaN(c[-1]) and !IsNaN(c) then bn else na);

##########
def hh = if bn==1 then 0 else if IsNaN(c) then hh[1] else Highest(h[1], NumberOfBarsPast);
plot HighLine = if bn < last - NumberOfBarsPast or IsNaN(c) then na else hh[-NumberOfBarsPast];
HighLine.SetDefaultColor(Color.GREEN);
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highline.setlineWeight(3);

##########
def LL = if IsNaN(c) then LL[1] else Lowest(l[1], NumberOfBarsPast);
plot LowLine =  if bn < last - NumberOfBarsPast or IsNaN(c) then na else LL[-NumberOfBarsPast];
LowLine.SetDefaultColor(Color.RED);
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowline.setlineWeight(3);

#########``````

Thanks @SleepyZ - perfect! Using HighestAll was too complex for me to figure out, but you nailed it!

87k+ Posts
416 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?