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