How do I identify this bar?

greco26

Active member
Hi All, I can't figure out the correct code to identify this bar noted in the screenshot examples below. In the code below, I've identified a low in the chart. I want to find the next closest historical low just above the low that I've identified. The historical low could be many bars back or only a few bars back. Any help would be awesome!


Code:
def bn = barnumber();
def lowbar = if low[1] < low[2] and low > low[1] then bn-1 else 0 ;
def lowbarvalue = if bn-1 == highestall(lowbar) then GetValue(low, bn - lowbar) else 0;
addchartbubble(lowbarvalue,lowbarvalue,lowbarvalue,color.yellow);



 
If you do that then it would result in a horizontal line,
Just selecting the higher barnumber of the two bars with the same low would produce the same line and is much easier.
 

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

Yea, makes sense. Here is what I came up with. should work fine I think but let me know your thoughts.

Code:
def frontlow = if low[1] < low[2] and low > low[1] then low[1] else if low [2] == low[1] then low [2] else  frontlow[1];
 
Also, is the below an acceptable way to extend the lines on the chart instead of the line extension code? I was playing with the code earlier today and found this alternative but not sure if you see a potential issue with that. Im not going to change anything but just wanted to know your thoughts.

FROM: def HighLine = if HighRun == 0 then Double.NaN else if bn == BackHighBN then BackHigh else HighLine[1] - HighSlope;

TO: def HighLine = if HighRun == 0 then Double.NaN else if bn == highestall(BackHighBN) then BackHigh else HighLine[1] - HighSlope;
 
Yes, both changes look good.
You can also remove 'if HighRun == 0 then Double.NaN' as the value of the slopes is already Double.NaN when HighRun == 0,
Same goes for the low line.
 
@Svanoy , FYI, the code is working awesome. I did have 1 question for you. If I want to exclude the current live bar from the fronthigh and frontlow conditions, how best would I do that? I tried several things like adding if bn < bn as well as if insane(close) and a few others and none are working. Any idea what im doing wrong? Anything you can point me to that might help me figure it out?

Code:
def frontlow = if low[1] < low[2] and low > low[1] then low[1] else if low [2] == low[1] then low [2] else  frontlow[1];
 
Yes, I did but it didn't work. No line plotted when I changed the offset.

it plots a line for me


Code:
def bn = barnumber();

input show_line1 = no;
input show_line2 = yes;

def frontlow1 = if bn <= 2 then 0 else if low[1] < low[2] and low > low[1] then low[1] else if low [2] == low[1] then low [2] else  frontlow1[1];

plot z1 = frontlow1;
z1.SetHiding( !show_line1 );

# ============================

#def frontlow2 = if bn <= 3 then 0 else if low[2] < low[3] and low[1] > low[2] then low[2] else if low [3] == low[2] then low [3] else  frontlow2[1];

def x = 3;
def frontlow2 = if bn <= x then 0 else if low[x-1] < low[x] and low[x-2] > low[x-1] then low[x-1] else if low [x] == low[x-1] then low[x] else frontlow2[1];

plot z2 = frontlow2;
z2.SetHiding( !show_line2 );

# --------------------------

addchartbubble(0, low,
 frontlow1 + "\n" +
 frontlow2
, color.yellow, no);
#
 
it plots a line for me


Code:
def bn = barnumber();

input show_line1 = no;
input show_line2 = yes;

def frontlow1 = if bn <= 2 then 0 else if low[1] < low[2] and low > low[1] then low[1] else if low [2] == low[1] then low [2] else  frontlow1[1];

plot z1 = frontlow1;
z1.SetHiding( !show_line1 );

# ============================

#def frontlow2 = if bn <= 3 then 0 else if low[2] < low[3] and low[1] > low[2] then low[2] else if low [3] == low[2] then low [3] else  frontlow2[1];

def x = 3;
def frontlow2 = if bn <= x then 0 else if low[x-1] < low[x] and low[x-2] > low[x-1] then low[x-1] else if low [x] == low[x-1] then low[x] else frontlow2[1];

plot z2 = frontlow2;
z2.SetHiding( !show_line2 );

# --------------------------

addchartbubble(0, low,
 frontlow1 + "\n" +
 frontlow2
, color.yellow, no);
#
Yes however in context with the study, the line doesn't plot (for me at least). For example, QQQ or SPY 1 min, the front low won't plot with the alternate code.


Code:
input HideArrows = yes;
def bn = BarNumber();

def fronthigh =  if high[1] > high[2] and high < high[1] then high[1]  else if high [2] == high[1] then high [2] else fronthigh[1];
def fronthighBN = if high[1] == fronthigh then bn - 1 else fronthighBN[1];


def x = 3;
def frontlow = if bn <= x then 0 else if low[x-1] < low[x] and low[x-2] > low[x-1] then low[x-1] else if low [x] == low[x-1] then low[x] else frontlow[1];

#def frontlow = if low[1] < low[2] and low > low[1] then low[1] else if low [2] == low[1] then low [2] else  frontlow[1];
def frontlowBN = if low[1] == frontlow then bn - 1  else frontlowBN[1];




#edits########################################################################
def BarNum = if !IsNaN(close) and BarNumber() > 0 then bn else BarNum[1];
def VBar = HighestAll(BarNum);

def BackwardsSendFrontHighValue = fold aa = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -aa)) do GetValue(fronthigh, -aa);
def BackwardsSendFrontHighBarNumber = fold bb = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -bb)) do GetValue(fronthighBN, -bb);

def FrontHighID = bn == BackwardsSendFrontHighBarNumber;
plot FrontHighPlot = if !HideArrows and FrontHighID then yes else Double.NaN;
FrontHighPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

def HighRangeStartBN = if high[1] > BackwardsSendFrontHighValue and high <= BackwardsSendFrontHighValue then bn else HighRangeStartBN[1];
def HighBackwardsSendRangeStartBarNumber = fold cc = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -cc)) do GetValue(HighRangeStartBN, -cc);



#****
def HighTestSlopes = if bn >= HighBackwardsSendRangeStartBarNumber and bn < BackwardsSendFrontHighBarNumber then (high - BackwardsSendFrontHighValue) / (BackwardsSendFrontHighBarNumber - bn) else if bn >= BackwardsSendFrontHighBarNumber and !IsNaN(close) then HighTestSlopes[1] else Double.NaN;
#def LowestSlopeBN = if !IsNaN(TestSlopes) and  TestSlopes == LowestAll(TestSlopes) and bn < BackwardsSendFrontLowBarNumber then bn else LowestSlopeBN[1];
def HighestSlopeBN = if !IsNaN(HighTestSlopes) and HighTestSlopes == HighestAll(HighTestSlopes) and bn < BackwardsSendFrontHighBarNumber then bn else HighestSlopeBN[1];
#****

def BackHighBN = if bn == HighestSlopeBN then bn else BackHighBN[1];
def BackHigh = if bn == HighestSlopeBN then high else BackHigh[1];

plot BackHighPlot = if !HideArrows and bn == BackHighBN then yes else Double.NaN;
BackHighPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);


#Slope = Rise/Run
def HighRise = if !IsNaN(BackHigh - BackwardsSendFrontHighValue) then BackHigh - BackwardsSendFrontHighValue else HighRise[1];
def HighRun = if !IsNaN(BackwardsSendFrontHighBarNumber - BackHighBN) then BackwardsSendFrontHighBarNumber - BackHighBN else HighRun[1];
def HighIndeterminateSlope = if HighRun == 0 then yes else no;
def HighSlope = if !HighIndeterminateSlope then HighRise / HighRun else Double.NaN;

def HighLine = if HighRun == 0 then Double.NaN else if bn == BackHighBN then BackHigh else HighLine[1] - HighSlope;

plot HighDrawLine = HighLine;
HighDrawLine.SetPaintingStrategy(PaintingStrategy.LINE);
HighDrawLine.SetLineWeight(1);
HighDrawLine.SetDefaultColor(color.white);



##############################################################################




#########################################################################

def BackwardsSendFrontLowValue = fold a = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -a)) do GetValue(frontlow, -a);
def BackwardsSendFrontLowBarNumber = fold b = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -b)) do GetValue(frontlowBN, -b);

def FrontLowID = bn == BackwardsSendFrontLowBarNumber;

plot FrontLowPlot = if !HideArrows and FrontLowID then yes else Double.NaN;
FrontLowPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def RangeStartBN = if low[1] < BackwardsSendFrontLowValue and low >= BackwardsSendFrontLowValue then bn else RangeStartBN[1];
def BackwardsSendRangeStartBarNumber = fold c = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -c)) do GetValue(RangeStartBN, -c);

def TestSlopes = if bn >= BackwardsSendRangeStartBarNumber and bn < BackwardsSendFrontLowBarNumber then (low - BackwardsSendFrontLowValue) / (BackwardsSendFrontLowBarNumber - bn) else if bn >= BackwardsSendFrontLowBarNumber and !IsNaN(close) then TestSlopes[1] else Double.NaN;

def LowestSlopeBN = if !IsNaN(TestSlopes) and  TestSlopes == LowestAll(TestSlopes) and bn < BackwardsSendFrontLowBarNumber then bn else LowestSlopeBN[1];

def BackLowBN = if bn == LowestSlopeBN then bn else BackLowBN[1];
def BackLow = if bn == LowestSlopeBN then low else BackLow[1];

plot BackLowPlot = if !HideArrows and bn == BackLowBN then yes else Double.NaN;
BackLowPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

#Slope = Rise/Run
def Rise = if !IsNaN(BackLow - BackwardsSendFrontLowValue) then BackLow - BackwardsSendFrontLowValue else Rise[1];
def Run = if !IsNaN(BackwardsSendFrontLowBarNumber - BackLowBN) then BackwardsSendFrontLowBarNumber - BackLowBN else Run[1];
def IndeterminateSlope = if Run == 0 then yes else no;
def Slope = if !IndeterminateSlope then Rise / Run else Double.NaN;

def Line = if Run == 0 then Double.NaN else if bn == BackLowBN then BackLow else Line[1] - Slope;

plot DrawLine = Line;
DrawLine.SetPaintingStrategy(PaintingStrategy.LINE);
DrawLine.SetLineWeight(1);
DrawLine.SetDefaultColor(color.white);

##############################################################################
#LINE EXTENTIONS

def na = Double.NaN;
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn[1];

input formula_bars = 50;

def FrontHighEXT = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
  else if  bn == (lastbarbn) then HighLine[1] - highslope
    else (FrontHighEXT[1] - HighSlope);

plot FHigh = FrontHighEXT;
FHigh.SetLineWeight(1);
FHigh.SetDefaultColor(Color.white);

def FrontLowEXT = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
  else if  bn == (lastbarbn) then Line[1] - slope
    else (FrontLowEXT[1] - Slope);

plot FLow = FrontLowEXT;
Flow.SetLineWeight(1);
FLow.SetDefaultColor(Color.white);
 
@greco26 You have to change the definition of FrontLowBN as well to match FrontLow.

Ruby:
def frontlow = if bn <= x then 0 else if low[x-1] < low[x] and low[x-2] > low[x-1] then low[x-1] else if low [x] == low[x-1] then low[x] else frontlow[1];
def frontlowBN = if low[x-1] == frontlow then bn - (x-1)  else frontlowBN[1];
 
Yea, thats what I thought but the lines are not plotting properly still. I have the offset to 3. qqq on the 3 min for example. qqq on the 1 minute has one of the lines cut off on the backhigh.


Code:
def x =3;
input HideArrows = yes;
def bn = BarNumber();

def fronthigh =  if bn <= x then 0 else if high[1] > high[2] and high < high[1] then high[1]  else if high [2] == high[1] then high [2] else fronthigh[1];
def fronthighBN = if high[x-1] == fronthigh then bn - 1 else fronthighBN[1];



def frontlow = if bn <= x then 0 else if low[x-1] < low[x] and low[x-2] > low[x-1] then low[x-1] else if low [x] == low[x-1] then low[x] else frontlow[1];

#def frontlow = if low[1] < low[2] and low > low[1] then low[1] else if low [2] == low[1] then low [2] else  frontlow[1];
def frontlowBN = if low[x-1] == frontlow then bn - (x-1)  else frontlowBN[1];




#edits########################################################################
def BarNum = if !IsNaN(close) and BarNumber() > 0 then bn else BarNum[1];
def VBar = HighestAll(BarNum);

def BackwardsSendFrontHighValue = fold aa = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -aa)) do GetValue(fronthigh, -aa);
def BackwardsSendFrontHighBarNumber = fold bb = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -bb)) do GetValue(fronthighBN, -bb);

def FrontHighID = bn == BackwardsSendFrontHighBarNumber;
plot FrontHighPlot = if !HideArrows and FrontHighID then yes else Double.NaN;
FrontHighPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

def HighRangeStartBN = if high[1] > BackwardsSendFrontHighValue and high <= BackwardsSendFrontHighValue then bn else HighRangeStartBN[1];
def HighBackwardsSendRangeStartBarNumber = fold cc = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -cc)) do GetValue(HighRangeStartBN, -cc);



#****
def HighTestSlopes = if bn >= HighBackwardsSendRangeStartBarNumber and bn < BackwardsSendFrontHighBarNumber then (high - BackwardsSendFrontHighValue) / (BackwardsSendFrontHighBarNumber - bn) else if bn >= BackwardsSendFrontHighBarNumber and !IsNaN(close) then HighTestSlopes[1] else Double.NaN;
#def LowestSlopeBN = if !IsNaN(TestSlopes) and  TestSlopes == LowestAll(TestSlopes) and bn < BackwardsSendFrontLowBarNumber then bn else LowestSlopeBN[1];
def HighestSlopeBN = if !IsNaN(HighTestSlopes) and HighTestSlopes == HighestAll(HighTestSlopes) and bn < BackwardsSendFrontHighBarNumber then bn else HighestSlopeBN[1];
#****

def BackHighBN = if bn == HighestSlopeBN then bn else BackHighBN[1];
def BackHigh = if bn == HighestSlopeBN then high else BackHigh[1];

plot BackHighPlot = if !HideArrows and bn == BackHighBN then yes else Double.NaN;
BackHighPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);


#Slope = Rise/Run
def HighRise = if !IsNaN(BackHigh - BackwardsSendFrontHighValue) then BackHigh - BackwardsSendFrontHighValue else HighRise[1];
def HighRun = if !IsNaN(BackwardsSendFrontHighBarNumber - BackHighBN) then BackwardsSendFrontHighBarNumber - BackHighBN else HighRun[1];
def HighIndeterminateSlope = if HighRun == 0 then yes else no;
def HighSlope = if !HighIndeterminateSlope then HighRise / HighRun else Double.NaN;

def HighLine = if HighRun == 0 then Double.NaN else if bn == BackHighBN then BackHigh else HighLine[1] - HighSlope;

plot HighDrawLine = HighLine;
HighDrawLine.SetPaintingStrategy(PaintingStrategy.LINE);
HighDrawLine.SetLineWeight(1);
HighDrawLine.SetDefaultColor(color.white);



##############################################################################




#########################################################################

def BackwardsSendFrontLowValue = fold a = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -a)) do GetValue(frontlow, -a);
def BackwardsSendFrontLowBarNumber = fold b = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -b)) do GetValue(frontlowBN, -b);

def FrontLowID = bn == BackwardsSendFrontLowBarNumber;

plot FrontLowPlot = if !HideArrows and FrontLowID then yes else Double.NaN;
FrontLowPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def RangeStartBN = if low[1] < BackwardsSendFrontLowValue and low >= BackwardsSendFrontLowValue then bn else RangeStartBN[1];
def BackwardsSendRangeStartBarNumber = fold c = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -c)) do GetValue(RangeStartBN, -c);

def TestSlopes = if bn >= BackwardsSendRangeStartBarNumber and bn < BackwardsSendFrontLowBarNumber then (low - BackwardsSendFrontLowValue) / (BackwardsSendFrontLowBarNumber - bn) else if bn >= BackwardsSendFrontLowBarNumber and !IsNaN(close) then TestSlopes[1] else Double.NaN;

def LowestSlopeBN = if !IsNaN(TestSlopes) and  TestSlopes == LowestAll(TestSlopes) and bn < BackwardsSendFrontLowBarNumber then bn else LowestSlopeBN[1];

def BackLowBN = if bn == LowestSlopeBN then bn else BackLowBN[1];
def BackLow = if bn == LowestSlopeBN then low else BackLow[1];

plot BackLowPlot = if !HideArrows and bn == BackLowBN then yes else Double.NaN;
BackLowPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

#Slope = Rise/Run
def Rise = if !IsNaN(BackLow - BackwardsSendFrontLowValue) then BackLow - BackwardsSendFrontLowValue else Rise[1];
def Run = if !IsNaN(BackwardsSendFrontLowBarNumber - BackLowBN) then BackwardsSendFrontLowBarNumber - BackLowBN else Run[1];
def IndeterminateSlope = if Run == 0 then yes else no;
def Slope = if !IndeterminateSlope then Rise / Run else Double.NaN;

def Line = if Run == 0 then Double.NaN else if bn == BackLowBN then BackLow else Line[1] - Slope;

plot DrawLine = Line;
DrawLine.SetPaintingStrategy(PaintingStrategy.LINE);
DrawLine.SetLineWeight(1);
DrawLine.SetDefaultColor(color.white);

##############################################################################
#LINE EXTENTIONS

def na = Double.NaN;
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn[1];

input formula_bars = 50;

def FrontHighEXT = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
  else if  bn == (lastbarbn) then HighLine[1] - highslope
    else (FrontHighEXT[1] - HighSlope);

plot FHigh = FrontHighEXT;
FHigh.SetLineWeight(1);
FHigh.SetDefaultColor(Color.white);

def FrontLowEXT = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
  else if  bn == (lastbarbn) then Line[1] - slope
    else (FrontLowEXT[1] - Slope);

plot FLow = FrontLowEXT;
Flow.SetLineWeight(1);
FLow.SetDefaultColor(Color.white);
 
@greco26 You didn't adjust FrontHigh and FrontHighBN properly.
Try this:
Ruby:
def fronthigh =  if bn <= x then 0 else if high[x-1] > high[x-2] and high[x] < high[x-1] then high[x-1] else if high[x-2] == high[x-1] then high[x-2] else fronthigh[1];
def fronthighBN = if high[x-1] == fronthigh then bn - (x-1) else fronthighBN[1];

def frontlow = if bn <= x then 0 else if low[x-1] < low[x] and low[x-2] > low[x-1] then low[x-1] else if low[x-2] == low[x-1] then low[x-2] else frontlow[1];
def frontlowBN = if low[x-1] == frontlow then bn - (x-1)  else frontlowBN[1];

Also, you may be better of using addition instead of subtraction, It would be easier to visualize the offset and you wont end up with negative offsets.

Ruby:
def fronthigh =  if bn <= x then 0 else if high[x+1] > high[x+2] and high[x] < high[x+1] then high[x+1] else if high[x+2] == high[x+1] then high[x+2] else fronthigh[1];
def fronthighBN = if high[x+1] == fronthigh then bn - (x+1) else fronthighBN[1];

def frontlow = if bn <= x then 0 else if low[x+1] < low[x] and low[x+2] > low[x+1] then low[x+1] else if low[x+2] == low[x+1] then low[x+2] else frontlow[1];
def frontlowBN = if low[x+1] == frontlow then bn - (x+1)  else frontlowBN[1];
 
Last edited:
Ah, I see now. Im ashamed to say, but I dont I fully understand how think script processes things. Its my understanding that the current bar number is the highest so I dont understand what the code is actually doing. bn < x then 0 means ignore the first 3 oldest bars (from the left) and then do start the action (which would be 3 bars older than the current bar number). I was thinking that everything I write is based on the very current bar so everything is processed from right to left. I'm going to re-read the Manual again to clear this up. I can't thank the both of you guys enough for everything you're done and all the help you've given me in the past. I'm a non-technical sales guy and think script is the very first code I've ever touched so its trial by fire for the last year or so that I've been messing around. Anyway, thank you guys very much!
 
@greco26 this looks promising, thank you very much for this one, can you post the final script in the first post, so it is always there and you can update that with version numbers, so anyone coming in can take it from there as the latest
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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