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