Range by bar number

Trigun1127

Member
Hey guys could someone edit this code so that at a desired bar the High and Low have horizontal lines plotted and that line terminates only when a bar Closes above one or the other?

input Reset_Time = 0930;
declare upper;
declare once_per_bar;
input Length = 10;
def fH =
if !isnan(close[-Length]) then Double.NaN
else if isnan(close[-Length]) and !isNaN(close[-(Length - 1)])
then GetValue(Highest(high,Length),-(Length - 1))
else fH[1]
;
plot H = fh;
H.setpaintingStrategy(paintingStrategy.HORIZONTAL);
H.setdefaultColor(color.red);
def fL =
if !isnan(close[-Length]) then Double.NaN
else if isnan(close[-Length]) and !isNaN(close[-(Length - 1)])
then GetValue(Lowest(Low,Length),-(Length - 1))
else fL[1]
;
plot L = fL;
L.setpaintingStrategy(paintingStrategy.HORIZONTAL);
L.setdefaultColor(color.green);
 
Solution
@Trigun1127 high and low will not plot until 'length' bars after 0930.
Ruby:
declare upper;
input Reset_Time = 0930;
input Length = 10;
input Consecutive_Closes_Above = 3;
input Consecutive_Closes_Below = 3;

def EOD = SecondsTillTime(1800) == 0;
def GB = close>open;
def RB = close<open;
def Doji = close == open;

def Reset = SecondsFromTime(Reset_Time) == 0;
def ResetBN = if Reset then BarNumber() else ResetBN[1];
def Highest_Lowest_TimeFrame = BarNumber() >= ResetBN + Length;

def fH = if GetValue(Reset, Length - 1) == 1 then Highest(high, Length) else fH[1];
def fL = if GetValue(Reset, Length - 1) == 1 then Lowest(low, Length) else fL[1];

def sH = if Reset then fH[-length+1] else double.nan;
def sL = if Reset then...
@Trigun1127 had to rewrite the definitions for fH and fL. the 'Highest' and 'Lowest' functions kept adjusting if close went above or below.

Ruby:
declare upper;
declare once_per_bar;
input Length = 10;

def fH = if isnan(close[-Length]) and !isNaN(close[-(Length - 1)]) then fold i = 0 to length-1 with hint = high do if getvalue(high,-i) > hint then getvalue(high,-i+1) else hint else fH[1];
def fL = if isnan(close[-Length]) and !isNaN(close[-(Length - 1)]) then fold j = 0 to length-1 with lint = low do if getvalue(low,-j) < lint then getvalue(low,-j) else lint else fL[1];

def Hf = if isnan(close) then Hf[1] else if close > fH then double.nan else if !fH[1] and fH then fH else Hf[1];
def Lf = if isnan(close) then Lf[1] else if close < fL then double.nan else if !fL[1] and fL then fL else Lf[1];

plot H = if Hf>0 then Hf else double.nan;
H.setpaintingStrategy(paintingStrategy.HORIZONTAL);
H.setdefaultColor(color.red);
plot L = if Lf>0 then Lf else double.nan;
L.setpaintingStrategy(paintingStrategy.HORIZONTAL);
L.setdefaultColor(color.green);

plot BreakOut = isnan(H);
BreakOut.Hide();
plot BreakDn = isnan(L);
BreakDn.Hide();
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Looking Great! just a couple of things since I completely forgot to mention (had it in mind the whole time but forgot to actually write it)!
I need it only for the first set of bars. So if its set to 5 it will only display the H or L for the first 5 bars
and only for RTH 9:30. So no need for it to keep counting the highest HH or LL for a rolling 5 bars.
Also what would I need to change so I can have X consecutive green/red bars closing above either H or L for break up/breakdown trigger.
 
Last edited:
@Trigun1127 high and low will not plot until 'length' bars after 0930.
Ruby:
declare upper;
input Reset_Time = 0930;
input Length = 10;
input Consecutive_Closes_Above = 3;
input Consecutive_Closes_Below = 3;

def EOD = SecondsTillTime(1800) == 0;
def GB = close>open;
def RB = close<open;
def Doji = close == open;

def Reset = SecondsFromTime(Reset_Time) == 0;
def ResetBN = if Reset then BarNumber() else ResetBN[1];
def Highest_Lowest_TimeFrame = BarNumber() >= ResetBN + Length;

def fH = if GetValue(Reset, Length - 1) == 1 then Highest(high, Length) else fH[1];
def fL = if GetValue(Reset, Length - 1) == 1 then Lowest(low, Length) else fL[1];

def sH = if Reset then fH[-length+1] else double.nan;
def sL = if Reset then fL[-length+1] else double.nan;

def Hf = if EOD then double.nan else if !isnan(sH) then sH else Hf[1];
def Lf = if EOD then double.nan else if !isnan(sL) then sL else Lf[1];

def BOC = if Reset then 0 else if !isnan(BOC[1]) and BOC[1] == Consecutive_Closes_Above then double.nan else if RB or EOD or !Highest_Lowest_TimeFrame then 0 else if close > Hf and !Doji and !RB then BOC[1]+1 else BOC[1];

def BDC = if Reset then 0 else if !isnan(BOC[1]) and BDC[1] == Consecutive_Closes_below then double.nan else if GB or EOD or !Highest_Lowest_TimeFrame then 0 else if close < Lf and !Doji and !GB then BDC[1]+1 else BDC[1];

plot H = if isnan(BDC) or isnan(BOC) then double.nan else Hf;
H.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
H.SetDefaultColor(Color.RED);
plot L = if isnan(BDC) or isnan(BOC) then double.nan else Lf;
L.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
L.SetDefaultColor(Color.GREEN);

plot BreakOut = BOC == Consecutive_Closes_Above;
BreakOut.Hide();
plot BreakDn = BDC == Consecutive_Closes_Below;
BreakDn.Hide();

Addchartbubble(BreakOut or BreakDn, if BreakOut then high else low, if BreakOut then BOC else BDC,color.white,if BreakOut then yes else no);
 
Last edited:
Solution
Mine doesn't seem to be drawing across all the way. Here's an example, in this example it should keep drawn across because the conditions weren't met on conditional closes. The second did close below but the line doesn't terminate at the bar where it brokeout. I also like your break out/ break down color shift idea, you can keep that in if you'd like.

 
I only need the lines to terminate if the consecutive bar conditions are fuflilled. Otherwise the high/low of the bar length (5 or 10) continues throughout the day like in first picture. If it gets x consecutive green/red bars closes then the line terminates at that bar and doesn’t continue.
 
Its looking fantastic!!
2 questions?
1. Are the lines supposed to change color after the consecutive breakout/breakdown? its not doing so on my screen if its the case.
2. For some reason on some days (im using lines instead of dashes) im getting this effect where the lines continue on and bend between the close and open of days. I don't have extended hours on. maybe that's it?


First example is color not changing
second example is lines getting bended

 
@Trigun1127
1. Are the lines supposed to change color after the consecutive breakout/breakdown? its not doing so on my screen if its the case.
I never had any type of color change in any of the code I wrote for this.

2. For some reason on some days (im using lines instead of dashes) im getting this effect where the lines continue on and bend between the close and open of days. I don't have extended hours on. maybe that's it?
Yes extended hours being off is causing this, should just be able to change EOD time to 1630 to fix.
 
@Trigun1127

I never had any type of color change in any of the code I wrote for this.


Yes extended hours being off is causing this, should just be able to change EOD time to 1630 to fix.
Oh ok the Breakdown code in settings shows it as a yellow color and breakout was white. Wasn't sure if that indicated a shift.
Tried EOD time to 1630 but it didnt change.
Last thing I noticed can you make it so Dojis are not counted as a consecutive close?
 
@Trigun1127 it is going to depend upon the aggregation period you are using.
In order to use paintingstrategy.LINE, You will have to adjust the EOD time to the time of the last bar show for the day.

Or , use paintingstrategy.Horizontal and change the EOD time back to 1800.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
469 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top