input Length = 50;
input Increase = 1.25;
def VolAvg = Average(volume, Length);
def AbvTarget = volume/VolAvg >= Increase;
def ADClose = CompoundValue(1, close, Double.NaN);
def LastBar = !IsNaN(close) && IsNaN(close[-1]);
def NHigh = CompoundValue(1, if AbvTarget && !LastBar then high else 0, Double.NaN);
def NLow = CompoundValue(1, if AbvTarget && !LastBar then low else 0, Double.NaN);
def NOpen = CompoundValue(1, if AbvTarget && !LastBar then open else 0, Double.NaN);
def NClose = CompoundValue(1, if AbvTarget && !LastBar then close else 0, Double.NaN);
#######################################################################################
script TargetsA {
input LastBar = 0;
input Bar = 0;
input Cross = 0;
input ADClose = 0;
def TA = if LastBar then
fold a = 0 to Bar
with inta = Double.POSITIVE_INFINITY
do if IsNaN(GetValue(Cross, a)) then inta
else if GetValue(Cross, a) < ADClose then inta
else if GetValue(Cross, a) > inta then inta
else if GetValue(Cross, a) < inta and GetValue(Cross, a) >= ADClose
then GetValue(Cross, a)
else inta
else TA[1];
plot Targets = if !TA then Double.POSITIVE_INFINITY else TA;}
#######################################################################################
script TargetsB {
input LastBar = 0;
input Bar = 0;
input Cross = 0;
input ADClose = 0;
def TA = if LastBar then
fold a = 0 to Bar
with inta = Double.NEGATIVE_INFINITY
do if IsNaN(GetValue(Cross, a)) then inta
else if GetValue(Cross, a) > ADClose then inta
else if GetValue(Cross, a) < inta then inta
else if GetValue(Cross, a) > inta and GetValue(Cross, a) <= ADClose
then GetValue(Cross, a)
else inta
else TA[1];
plot Targets = if !TA then Double.NEGATIVE_INFINITY else TA;}
##########################################
def CACH = TargetsA(1,Length, NHigh, ADClose);
def CACL = TargetsA(1,Length, NLow, ADClose);
def CACO = TargetsA(1,Length, NOpen, ADClose);
def CACC = TargetsA(1,Length, NClose, ADClose);
#########################################
def CBCH = TargetsB(1,Length, NHigh, ADClose);
def CBCL = TargetsB(1,Length, NLow, ADClose);
def CBCO = TargetsB(1,Length, NOpen, ADClose);
def CBCC = TargetsB(1,Length, NClose, ADClose);
###########################################
def PACH1 = TargetsA(LastBar,Length, CACH, ADClose);
def PBCH1 = TargetsB(LastBar,Length, CBCH, ADClose);
def PACL1 = TargetsA(LastBar,Length, CACL, ADClose);
def PBCL1 = TargetsB(LastBar,Length, CBCL, ADClose);
def PACO1 = TargetsA(LastBar,Length, CACO, ADClose);
def PBCO1 = TargetsB(LastBar,Length, CBCO, ADClose);
def PACC1 = TargetsA(LastBar,Length, CACC, ADClose);
def PBCC1 = TargetsB(LastBar,Length, CBCC, ADClose);
#############################################
def CH = if LastBar then if PACH1 == Double.POSITIVE_INFINITY then PBCH1 else if PBCH1 == Double.NEGATIVE_INFINITY then PACH1
else if Min(close-PACH1,close-PBCH1)>=0 then PBCH1 else PACH1 else CH[1];
def CL = if LastBar then if PACL1 == Double.POSITIVE_INFINITY then PBCL1 else if PBCL1 == Double.NEGATIVE_INFINITY then PACL1
else if Min(close-PACL1,close-PBCL1)>=0 then PBCL1 else PACL1 else CL[1];
def CO = if LastBar then if PACO1 == Double.POSITIVE_INFINITY then PBCO1 else if PBCO1 == Double.NEGATIVE_INFINITY then PACO1
else if Min(close-PACO1,close-PBCO1)>=0 then PBCO1 else PACO1 else CO[1];
def CC = if LastBar then if PACC1 == Double.POSITIVE_INFINITY then PBCC1 else if PBCC1 == Double.NEGATIVE_INFINITY then PACC1
else if Min(close-PACC1,close-PBCC1)>=0 then PBCC1 else PACC1 else CC[1];
###########################################
def ClosestHigh = fold H = 0 to 1000 while !IsNaN(GetValue(close, -h)) do GetValue(CH,-h);
def ClosestLow = fold l = 0 to 1000 while !IsNaN(GetValue(close, -l)) do GetValue(CL,-l);
def ClosestOpen = fold o = 0 to 1000 while !IsNaN(GetValue(close, -o)) do GetValue(CO,-o);
def ClosestClose = fold c = 0 to 1000 while !IsNaN(GetValue(close, -c)) do GetValue(CC,-c);
#############################################
def HighLine = if AbvTarget && high==ClosestHigh then ClosestHigh else HighLine[1];
def LowLine = if AbvTarget && low==ClosestLow then ClosestLow else LowLine[1];
def OpenLine = if AbvTarget && open==ClosestOpen then ClosestOpen else OpenLine[1];
def CloseLine = if AbvTarget && close==ClosestClose then ClosestClose else CloseLine[1];
##############################################
plot PCH = if CH then CH else if !IsNaN(close[-1]) && HighLine then HighLine else double.nan;
PCH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PCH.SetLineWeight(1);
plot PCL = if CL then CL else if !IsNaN(close[-1]) && LowLine then LowLine else Double.NaN;
PCL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PCL.SetLineWeight(1);
plot PCO = if CO then CO else if !IsNaN(close) && OpenLine then OpenLine else Double.NaN;
PCO.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PCO.SetLineWeight(1);
plot PCC = if CC then CC else if !IsNaN(close) && CloseLine then CloseLine else Double.NaN;
PCC.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PCC.SetLineWeight(1);
################################
AssignPriceColor(if AbvTarget then if close>open then color.CYAN else color.Magenta else color.Current);
AssignPriceColor(if AbvTarget && high==PCH then if close>open then color.VIOLET else color.PLUM else color.Current);
AssignPriceColor(if AbvTarget && low==PCL then if close>open then color.VIOLET else color.PLUM else color.Current);
AssignPriceColor(if AbvTarget && open==PCO then if close>open then color.VIOLET else color.PLUM else color.Current);
AssignPriceColor(if AbvTarget && close==PCC then if close>open then color.VIOLET else color.PLUM else color.Current);
##########################################