input Show_Bubbles = no;
input Aggregation = AggregationPeriod.MIN;
def PivotHigh = high(period = Aggregation);
def PivotLow = low(period = Aggregation);
def PivotOpen = open(period = Aggregation);
def PivotClose = close(period = Aggregation);
#Current Close/Bar/Expansion Start#
def Bar = if !IsNaN(PivotClose) and BarNumber() > 0 then BarNumber() else Bar[1];
def VBar = HighestAll(Bar);
def FinalClose = fold c = 0 to VBar while !IsNaN(Bar) and !IsNaN(GetValue(PivotClose, -c)) do GetValue(PivotClose, -c);
def ExpansionStart = if IsNaN(PivotClose) and !IsNaN(PivotClose[1]) then FinalClose[1] else ExpansionStart[1];
#High/Low Pivot Initial Flags#
def HighPivotInitialFlag;
def LowPivotInitialFlag;
def HighClearCheck;
def LowClearCheck;
def InitialFlagCheck;
HighClearCheck = CompoundValue(1,if LowPivotInitialFlag[1] then PivotHigh[1] else if PivotLow > HighClearCheck[1] then 0 else HighClearCheck[1],0);
LowClearCheck = CompoundValue(1,if HighPivotInitialFlag[1] then PivotLow[1] else if PivotHigh < LowClearCheck[1] then 0 else LowClearCheck[1],0);
HighPivotInitialFlag = PivotHigh > PivotHigh[1] and PivotHigh >= PivotHigh[-1] and !HighClearCheck;
LowPivotInitialFlag = PivotLow < PivotLow[1] and PivotLow <= PivotLow[-1] and !LowClearCheck;
InitialFlagCheck = if HighPivotInitialFlag then 1 else if LowPivotInitialFlag then 0 else InitialFlagCheck[1];
#High/Low Pivot Secondary Flags#
def SecondaryHighPivotFlag;
def SecondaryLowPivotFlag;
def SecondayFlagCheck;
SecondaryHighPivotFlag = if HighPivotInitialFlag == 1 and !InitialFlagCheck[1] then fold hpf = 0 to VBar - Bar with inthpf = PivotHigh while GetValue(LowPivotInitialFlag, -hpf) == 0 or (GetValue(LowPivotInitialFlag, -hpf) == 1 and GetValue(HighPivotInitialFlag, -hpf) == 1) do if GetValue(PivotHigh, -hpf) > inthpf then GetValue(PivotHigh, -hpf) else inthpf else SecondaryHighPivotFlag[1];
SecondaryLowPivotFlag = if LowPivotInitialFlag == 1 and InitialFlagCheck[1] then fold lpf = 0 to VBar - Bar with intlpf = PivotLow while GetValue(HighPivotInitialFlag, -lpf) == 0 or (GetValue(LowPivotInitialFlag, -lpf) == 1 and GetValue(HighPivotInitialFlag, -lpf) == 1) do if GetValue(PivotLow, -lpf) < intlpf then GetValue(PivotLow, -lpf) else intlpf else SecondaryLowPivotFlag[1];
SecondayFlagCheck = if PivotHigh == SecondaryHighPivotFlag and HighPivotInitialFlag then 1 else if PivotLow == SecondaryLowPivotFlag and LowPivotInitialFlag then 0 else SecondayFlagCheck[1];
#High/Low Pivot Final Flag/Pivot Value#
def HighPivot;
def LowPivot;
def PivotValue;
HighPivot = PivotHigh == SecondaryHighPivotFlag and HighPivotInitialFlag and !SecondayFlagCheck[1];
LowPivot = PivotLow == SecondaryLowPivotFlag and LowPivotInitialFlag and SecondayFlagCheck[1];
PivotValue = if HighPivot then PivotHigh else if LowPivot then PivotLow else 0;
#Last Pivot Check#
def LastPivot = if HighPivot or LowPivot then fold lp = 0 to VBar - Bar with intlp while !IsNaN(GetValue(PivotClose, -lp)) do if GetValue(HighPivot, -lp) or GetValue(LowPivot, -lp) then intlp + 1 else intlp else 0;
#Count Forward and Backwards Bars to next opposite pivot#
def HighForwardCount = fold hfc = 0 to VBar - Bar with inthfc while GetValue(LowPivot, -hfc) == 0 do inthfc + 1;
def HighBackwardCount = fold hbc = 0 to Bar with inthbc while GetValue(LowPivot, hbc) == 0 do inthbc + 1;
def LowForwardCount = fold lfc = 0 to VBar - Bar with intlfc while GetValue(HighPivot, -lfc) == 0 do intlfc + 1;
def LowBackwardCount = fold lbc = 0 to Bar with intlbc while GetValue(HighPivot, lbc) == 0 do intlbc + 1;
#Initial PivotPoints Strength#
def HighPivotStrength = if HighForwardCount < 3 or HighBackwardCount < 3 then 0 else if HighForwardCount < HighBackwardCount then HighForwardCount else HighBackwardCount;
def LowPivotStrength = if LowForwardCount < 3 or LowBackwardCount < 3 then 0 else if LowForwardCount < LowBackwardCount then LowForwardCount else LowBackwardCount;
#Price Height#
def HighPivotForwardPriceHeight = if HighPivot then PivotHigh - fold hpfph = 0 to VBar - Bar with inthpfph = PivotLow while GetValue(PivotHigh, -hpfph) <= PivotValue do if GetValue(PivotLow, -hpfph) < inthpfph then GetValue(PivotLow, -hpfph) else inthpfph else 0;
def HighPivotBackwardPriceHeight = if HighPivot then PivotHigh - fold hpbph = 0 to Bar with inthpbph = PivotLow while GetValue(PivotHigh, hpbph) <= PivotValue do if GetValue(PivotLow, hpbph) < inthpbph then GetValue(PivotLow, hpbph) else inthpbph else 0;
def HighPivotPriceHeight = if HighPivotForwardPriceHeight < HighPivotBackwardPriceHeight then HighPivotForwardPriceHeight else HighPivotBackwardPriceHeight;
def LowPivotForwardPriceHeight = if LowPivot then (fold lpfph = 0 to VBar - Bar with intlpfph = PivotHigh while GetValue(PivotLow, -lpfph) >= PivotValue do if GetValue(PivotHigh, -lpfph) > intlpfph then GetValue(PivotHigh, -lpfph) else intlpfph) - PivotLow else 0;
def LowPivotBackwardPriceHeight = if LowPivot then (fold lpbph = 0 to Bar with intlpbph = PivotHigh while GetValue(PivotLow, lpbph) >= PivotValue do if GetValue(PivotHigh, lpbph) > intlpbph then GetValue(PivotHigh, lpbph) else intlpbph) - PivotLow else 0;
def LowPivotPriceHeight = if LowPivotForwardPriceHeight < LowPivotBackwardPriceHeight then LowPivotForwardPriceHeight else LowPivotBackwardPriceHeight;
#Count Bars until Crossed#
def HighPivotCross = if HighPivot then fold hpc = 0 to VBar - Bar with inthpc while GetValue(PivotHigh, -hpc - 1) <= PivotValue do if IsNaN(GetValue(PivotClose, -hpc - 2)) then 0 else inthpc + 1 else Double.NaN;
def LowPivotCross = if LowPivot then fold lpc = 0 to VBar - Bar with intlpc while GetValue(PivotLow, -lpc - 1) >= PivotValue do if IsNaN(GetValue(PivotClose, -lpc - 2)) then 0 else intlpc + 1 else Double.NaN;
#Decrease Pivot Strength After Cross#
def HighPivotResistanceStrength = if HighPivot then fold hprs = 0 to VBar - Bar with inthprs = HighPivotStrength / 2 while !IsNaN(GetValue(PivotClose, -hprs - 1)) and inthprs > 0 do if hprs > HighPivotCross and HighPivotCross and GetValue(PivotLow, -hprs) > PivotValue then inthprs - .5 else if GetValue(PivotHigh, -hprs) > PivotValue and GetValue(PivotLow, -hprs) < PivotValue then inthprs - (((GetValue(PivotHigh, -hprs) - PivotValue) / (GetValue(PivotHigh, -hprs) - GetValue(PivotLow, -hprs))) * .5) else inthprs else 0;
def FinalHighPivotResistanceStrength = if HighPivotResistanceStrength <= 0 then 0 else HighPivotResistanceStrength;
def HighPivotSupportStrength = if HighPivot then fold hpss = 0 to VBar - Bar with inthpss = HighPivotStrength / 2 while HighPivotResistanceStrength <= 0 and !IsNaN(GetValue(PivotClose, -hpss - 1)) and inthpss > 0 do if hpss > HighPivot and HighPivotCross and GetValue(PivotHigh, -hpss) < PivotValue then inthpss - .5 else if GetValue(PivotHigh, -hpss) > PivotValue and GetValue(PivotLow, -hpss) < PivotValue then inthpss - ((PivotValue - (GetValue(PivotLow, -hpss)) / (GetValue(PivotHigh, -hpss) - GetValue(PivotLow, -hpss))) * .5) else inthpss else 0;
def FinalHighPivotSupportStrength = if HighPivotSupportStrength <= 0 then 0 else HighPivotSupportStrength;
def LowPivotSupportStrength = if LowPivot then fold lpss = 0 to VBar - Bar with intlpss = LowPivotStrength / 2 while !IsNaN(GetValue(PivotClose, -lpss - 1)) and intlpss > 0 do if lpss > LowPivotCross and LowPivotCross and GetValue(PivotHigh, -lpss) < PivotValue then intlpss - .5 else if GetValue(PivotLow, -lpss) < PivotValue and GetValue(PivotHigh, -lpss) > PivotValue then intlpss - (((PivotValue - GetValue(PivotLow, -lpss)) / (GetValue(PivotHigh, -lpss) - GetValue(PivotLow, -lpss))) * .5) else intlpss else 0;
def FinalLowPivotSupportStrength = if LowPivotSupportStrength <= 0 then 0 else LowPivotSupportStrength;
def LowPivotResistanceStrength = if LowPivot then fold lprs = 0 to VBar - Bar with intlprs = LowPivotStrength / 2 while LowPivotSupportStrength <= 0 and !IsNaN(GetValue(PivotClose, -lprs - 1)) and intlprs > 0 do if lprs > LowPivotCross and LowPivotCross and GetValue(PivotLow, -lprs) > PivotValue then intlprs - .5 else if GetValue(PivotLow, -lprs) < PivotValue and GetValue(PivotHigh, -lprs) > PivotValue then intlprs - (((GetValue(PivotHigh, -lprs) - PivotValue) / (GetValue(PivotHigh, -lprs) - GetValue(PivotLow, -lprs))) * .5) else intlprs else 0;
def FinalLowPivotResistanceStrength = if LowPivotResistanceStrength <= 0 then 0 else LowPivotResistanceStrength;
def HighPivotTotalStrength = FinalHighPivotResistanceStrength + FinalHighPivotSupportStrength;
def LowPivotTotalStrength = FinalLowPivotResistanceStrength + FinalLowPivotSupportStrength;
plot Pivots = if HighPivot then PivotHigh else if LowPivot then PivotLow else Double.NaN;
Pivots.EnableApproximation();
AddChartBubble(HighPivot and Show_Bubbles, PivotHigh, "High Pivot\n" + PivotValue, Color.WHITE, yes);
AddChartBubble(LowPivot and Show_Bubbles, PivotLow, "Low Pivot\n" + PivotValue, Color.WHITE, no);
AddChartBubble(HighPivot and !HighPivotCross and Show_Bubbles, PivotHigh, "Not Crossed", Color.WHITE, yes);
AddChartBubble(LowPivot and !LowPivotCross and Show_Bubbles, PivotLow, "Not Crossed", Color.WHITE, no);
AddChartBubble(HighPivot and Show_Bubbles, PivotHigh, "Price Height " + HighPivotPriceHeight, Color.WHITE, yes);
AddChartBubble(LowPivot and Show_Bubbles, PivotLow, "Price Height " + LowPivotPriceHeight, Color.WHITE, no);
AddChartBubble(HighPivot and Show_Bubbles, PivotHigh, "Resistance Strength " + FinalHighPivotResistanceStrength, if HighPivotResistanceStrength <= 0 then Color.LIGHT_RED else if HighPivotResistanceStrength < HighPivotStrength / 2 then Color.YELLOW else Color.LIGHT_GREEN, yes);
AddChartBubble(HighPivot and Show_Bubbles, PivotHigh, "Support Strength " + FinalHighPivotSupportStrength, if HighPivotSupportStrength <= 0 then Color.LIGHT_RED else if HighPivotSupportStrength < HighPivotStrength / 2 then Color.YELLOW else Color.LIGHT_GREEN, yes);
AddChartBubble(LowPivot and Show_Bubbles, PivotLow, "Resistance Strength " + FinalLowPivotResistanceStrength, if LowPivotResistanceStrength <= 0 then Color.LIGHT_RED else if LowPivotResistanceStrength < LowPivotStrength / 2 then Color.YELLOW else Color.LIGHT_GREEN, no);
AddChartBubble(LowPivot and Show_Bubbles, PivotLow, "Support Strength " + FinalLowPivotSupportStrength, if LowPivotSupportStrength <= 0 then Color.LIGHT_RED else if LowPivotSupportStrength < LowPivotStrength / 2 then Color.YELLOW else Color.LIGHT_GREEN, no);