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);



 
@greco26
Started working on this from your previous post.
Tested it a little, tried to see if it matched your screenshots, buy couldn't see the ticker symbols and time periods on all of them.
Let me know if it has any problems.
Grg702P.png

Ruby:
def bn = BarNumber();

def fronthigh = if high[1] > high[2] and high < high[1] then high[1] else fronthigh[1];
def fronthighBN = if high[1] == fronthigh then bn - 1 else fronthighBN[1];
def backhigh = if bn < HighestAll(fronthighBN) and high < fronthigh then Highest(high) else backhigh[1];
def backhighBN = if high == backhigh then bn else backhighBN[1];

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

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

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 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 DifferenceFromFrontLow = if bn < BackwardsSendFrontLowBarNumber and (low - BackwardsSendFrontLowValue) > 0 and bn >= BackwardsSendRangeStartBarNumber then low - BackwardsSendFrontLowValue else Double.NaN;

def BackLow = if (low - BackwardsSendFrontLowValue) == LowestAll(DifferenceFromFrontLow) then low else BackLow[1];
def BackLowBN = if low == BackLow then bn else BackLowBN[1];
def BackwardsSendBackLowBarNumber = fold d = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -d)) do GetValue(BackLowBN, -d);
def BackLowToFrontLowLine = if bn >= BackLowBN and BackLow > 0 then BackLow else BackLowToFrontLowLine[1];

plot BackLowPlot = if bn == BackwardsSendBackLowBarNumber then yes else Double.NaN;
BackLowPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#plot BackLowPlot = if bn >= BackwardsSendBackLowBarNumber then BackLowToFrontLowLine else Double.NaN;
#BackLowPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

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

def Line = if bn < BackwardsSendBackLowBarNumber then Double.NaN else if BarNumber()==BackLowBN then BackLow else Line[1]-Slope;

plot DrawLine = Line;
DrawLine.SetPaintingStrategy(PaintingStrategy.LINE);
DrawLine.SetLineWeight(2);
##############################################################################

AddLabel(1, BackLow, Color.WHITE);
 
Last edited:
Thanks for this. You are super close. On some charts it is finding the correct bar but on other charts its not. The idea is to find the oldest low bar just above the priceline of the frontlow and draw the plot from there so that the slop is more gradual. Think of it like this; if you click to start a trend line from the front low and, while keeping the line even with price, pull the line all the way to the left until you hit a bar, then start moving your mouse up to connect the end of the trend line to the very first low you hit. Below is QQQ on the 15m chart which shows what I mean. The second screenshot is AAPL on the 1 minute. Let m know if this makes sense. thank you!!!!




 
@greco26 Misunderstood your post in the other thread, thought you wanted the low closest to and still above the lowbarvalue.
Will have to work on 'first low hit' part, but this is closer.
Ruby:
def bn = BarNumber();

def fronthigh = if high[1] > high[2] and high < high[1] then high[1] else fronthigh[1];
def fronthighBN = if high[1] == fronthigh then bn - 1 else fronthighBN[1];
def backhigh = if bn < HighestAll(fronthighBN) and high < fronthigh then Highest(high) else backhigh[1];
def backhighBN = if high == backhigh then bn else backhighBN[1];

def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 FrontLowID then yes else Double.NaN;
FrontLowPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

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

def BackLow = if bn == BackwardsSendRangeStartBarNumber then low else BackLow[1];
def BackwardsSendBackLowBarNumber = fold d = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -d)) do GetValue(BackLowBN, -d);
def BackLowToFrontLowLine = if bn >= BackLowBN and BackLow > 0 then BackLow else BackLowToFrontLowLine[1];

plot BackLowPlot = if bn == BackwardsSendRangeStartBarNumber then yes else Double.NaN;
BackLowPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

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

def Line = if bn < BackwardsSendRangeStartBarNumber then Double.NaN else if BarNumber()==BackLowBN then BackLow else Line[1]-Slope;

plot DrawLine = Line;
DrawLine.SetPaintingStrategy(PaintingStrategy.LINE);
DrawLine.SetLineWeight(2);
##############################################################################

AddLabel(1, BackLow, Color.WHITE);
 
Think of it like this; if you click to start a trend line from the front low and, while keeping the line even with price, pull the line all the way to the left until you hit a bar, then start moving your mouse up to connect the end of the trend line to the very first low you hit. ...

" to the very first low you hit. "
this isn't clear. you aren't connecting the line the the low of the bar after the 'hit bar'

do you mean the next dip , defined by your formula ?
low[1] < low[2] and low > low[1]
 
" to the very first low you hit. "
this isn't clear. you aren't connecting the line the the low of the bar after the 'hit bar'

do you mean the next dip , defined by your formula ?
low[1] < low[2] and low > low[1]
I know, Im sorry. I can't define it by formula. Thats what I need help with. No, its not necessary the dip but it could be if thats the first low that the trend line hits (example in my video).




I've created a very short video and uploaded it to Imgur. Hopefully that helps clarify what I mean. Below is also the steps I take to draw the trend line for a downward slope.

Step 1 - I start a trend line or hover my crosshairs over the the front low and look left to see where the crosshair horizontal line meets a bar.
Step 2 - For the downward slope, from that bar from step 1, I slide my curser up until the trend line I started touches the low of any bar to the right of the bar identified in step 1.
 
@greco26
Sl8Mbsq.png

Ruby:
def bn = BarNumber();

def fronthigh = if high[1] > high[2] and high < high[1] then high[1] else fronthigh[1];
def fronthighBN = if high[1] == fronthigh then bn - 1 else fronthighBN[1];
def backhigh = if bn < HighestAll(fronthighBN) and high < fronthigh then Highest(high) else backhigh[1];
def backhighBN = if high == backhigh then bn else backhighBN[1];

def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 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 Double.NaN;

def LowestSlopeBN = if TestSlopes == LowestAll(TestSlopes) 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 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(2);
##############################################################################
 
@greco26
Sl8Mbsq.png

Ruby:
def bn = BarNumber();

def fronthigh = if high[1] > high[2] and high < high[1] then high[1] else fronthigh[1];
def fronthighBN = if high[1] == fronthigh then bn - 1 else fronthighBN[1];
def backhigh = if bn < HighestAll(fronthighBN) and high < fronthigh then Highest(high) else backhigh[1];
def backhighBN = if high == backhigh then bn else backhighBN[1];

def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 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 Double.NaN;

def LowestSlopeBN = if TestSlopes == LowestAll(TestSlopes) 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 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(2);
##############################################################################
You nailed it. Like...totally nailed it!! Im going to add the code for the high plots and add some line extensions.

I'm going to break apart your code to study it and learn. I do that will all the code you guys help me with. So much to learn but I can't ever thank you and the rest of the folks on here like @halcyonguy who always help me. I try to root through and answer some easier questions on the forum to payback back and take some burden off you guys but I'm totally not at your skillset. Thank you again. Great stuff!!
 
Well, I can't figure out how to extend the slope lines to the end of the right side of the chart. Because I haven't already asked enough from you guys, can you show me how to do that? :) I tried using some old code you guys wrote for me but I dont fully understand how it works so I dont know how to massage it. Sorry guys......... The good news I that I have the high plots working now too.

Code:
def bn = BarNumber();

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


def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 Double.NaN;

def HighestSlopeBN = if HighTestSlopes == HighestAll(HighTestSlopes) 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 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(2);
HighDrawLine.SetDefaultColor(color.violet);



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




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

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 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 Double.NaN;

def LowestSlopeBN = if TestSlopes == LowestAll(TestSlopes) 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 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(2);
DrawLine.SetDefaultColor(color.violet);
##############################################################################
 
Well, I can't figure out how to extend the slope lines to the end of the right side of the chart. Because I haven't already asked enough from you guys, can you show me how to do that? :) I tried using some old code you guys wrote for me but I dont fully understand how it works so I dont know how to massage it. Sorry guys......... The good news I that I have the high plots working now too.

Code:
def bn = BarNumber();

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


def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 Double.NaN;

def HighestSlopeBN = if HighTestSlopes == HighestAll(HighTestSlopes) 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 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(2);
HighDrawLine.SetDefaultColor(color.violet);



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




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

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 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 Double.NaN;

def LowestSlopeBN = if TestSlopes == LowestAll(TestSlopes) 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 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(2);
DrawLine.SetDefaultColor(color.violet);
##############################################################################

take a look at this. it draws 4 lines after the last bar.
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-89607
look at the code for the yellow line ,
test formula 3 section


another example of extending lines past the last bar
https://usethinkscript.com/threads/rsi-prediction.8631/#post-83898
 
Last edited:
take a look at this. it draws 4 lines after the last bar.
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-89607
look at the code for the yellow line ,
test formula 3 section


another example of extending lines past the last bar
https://usethinkscript.com/threads/rsi-prediction.8631/#post-83898
Line extensions are in but I'm having 2 issues with them.
1 - The slopes of the extension lines look correct however they are not properly aligned. Any idea what's wrong in my code?
2 - How do I close the gaps in the lines so that it looks like one continuous line?

The image below is an example on QQQ 1 minute chart.

Thanks, of course, as always! ~Tim


Code:
def bn = BarNumber();

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


def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 Double.NaN;

def HighestSlopeBN = if HighTestSlopes == HighestAll(HighTestSlopes) 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 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(2);
HighDrawLine.SetDefaultColor(color.violet);


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




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

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 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 Double.NaN;

def LowestSlopeBN = if TestSlopes == LowestAll(TestSlopes) 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 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(2);
DrawLine.SetDefaultColor(color.violet);

##############################################################################
#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 fronthigh
    else (FrontHighEXT[1] - HighSlope);

plot FHigh = FrontHighEXT;
FHigh.SetDefaultColor(Color.YELLOW);

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

plot FLow = FrontLowEXT;
FLow.SetDefaultColor(Color.YELLOW);
 
Last edited:
@greco26
Was an issue with the slope values not carrying forward.
Fixed the alignment too, it was a little off.
ZpyVn4i.png

Ruby:
def bn = BarNumber();

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


def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 HighestSlopeBN = if HighTestSlopes == HighestAll(HighTestSlopes) 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 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.violet);


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




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

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 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) 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 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.violet);

##############################################################################
#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.SetDefaultColor(Color.YELLOW);

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.SetDefaultColor(Color.YELLOW);
/CODE]
 
Last edited:
@greco26
Was an issue with the slope values not carrying forward.
Fixed the alignment too, it was a little off.
ZpyVn4i.png

Ruby:
def bn = BarNumber();

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


def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 HighestSlopeBN = if HighTestSlopes == HighestAll(HighTestSlopes) 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 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.violet);


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




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

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 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) 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 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.violet);

##############################################################################
#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.SetDefaultColor(Color.YELLOW);

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.SetDefaultColor(Color.YELLOW);
/CODE]
wow, you guys are AMAZING! I will put this code in. Thank you guys so much!!! I think The Strat community using TOS is going to really see value in this. Thank you guys!!
 
@Svanoy, FYI I noticed something wonky. Take a look at SPY on the 5 minute chart and also on the 30 min chart. Im looking into it but it appears that the backhigh and backlow code Is creating multiple signals. There can't be a backhigh or backlow on the last 2 bars. Actually there can't be a backhigh or backlow unless there is a fronthigh or frontlow signal in front of it. I'm looking into it.


 
Last edited:
@greco26
Fixed one problem caused another :rolleyes:.
Slope was sometimes being recalculated, instead of just copying previous value, on bars after FrontLow.
Bug fixed.
9wbV2bo.png

Ruby:
def bn = BarNumber();

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


def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 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.violet);


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




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

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 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 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.violet);

##############################################################################
#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.SetDefaultColor(Color.YELLOW);

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.SetDefaultColor(Color.YELLOW);
 
@greco26
Fixed one problem caused another :rolleyes:.
Slope was sometimes being recalculated, instead of just copying previous value, on bars after FrontLow.
Bug fixed.
9wbV2bo.png

Ruby:
def bn = BarNumber();

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


def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 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.violet);


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




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

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 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 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.violet);

##############################################################################
#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.SetDefaultColor(Color.YELLOW);

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.SetDefaultColor(Color.YELLOW);
I was actually working on the high test slops code when you sent this through so I'm happy that I was on the right path!
@greco26
Noticed a problem with how you are identifying FrontLow/FrontHigh.
If 2 bars have the same low/high then the wrong bar is identified and the line cannot be plotted.
F on 5 min chart
f9Ak5rq.png
Yea, noticed that yesterday as well. I started working on that. I was thinking through how I would assign a BN if there is a tie for the same price level. Haven't figure it out yet so if you an idea, let me know!!
 
@greco26
Fixed one problem caused another :rolleyes:.
Slope was sometimes being recalculated, instead of just copying previous value, on bars after FrontLow.
Bug fixed.
9wbV2bo.png

Ruby:
def bn = BarNumber();

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


def frontlow = if low[1] < low[2] and low > low[1] then low[1] 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 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 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.violet);


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




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

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 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 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.violet);

##############################################################################
#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.SetDefaultColor(Color.YELLOW);

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.SetDefaultColor(Color.YELLOW);
this works awesome!!
 
I was actually working on the high test slops code when you sent this through so I'm happy that I was on the right path!

Yea, noticed that yesterday as well. I started working on that. I was thinking through how I would assign a BN if there is a tie for the same price level. Haven't figure it out yet so if you an idea, let me know!!
What if I used a fold to find any lows within the rangestartbn that matches the frontlow then find the oldest BN and add an if statement to the fontlowbn variable? Folds are still new to me so I want to give it a try but do you think im on the right track?
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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