#// © Zeiierman
#https://www.tradingview.com/v/vz0bVIJo/
#indicator("False Breakout (Expo)",overlay=true,max_bars_back = 2000)
# Converted and mod By Sam4Cok@Samer800 - 11/2022
#// ~~ inputs {
input Source = close;
input HighLowBand = no;
input highlightArea = no;
input FalseBreakoutLines = yes;
input autoExtendLines = yes;
input MaxLinesLength = 20; #Hint MaxLinesLength: Adjust max lengtth of lines if "Auto Extend Lines" disabled.
input signalStyle = {Arrows,Default Bubbles, None};
input smoothingType = {default "None", "WMA", "HMA"}; # Smoothing"
input lookbackPeriod = 20; # "False Breakout Period"
input minPeriod = 5; # "New Breakout within minimum X bars"
input maxPeriod = 5; # "Signal valid for X bars"
input smoothingLength = 10; # "Smoothing Length"
input aggressiveMode = no; # "This filter enables a more aggressive false breakout detection."
def na = Double.NaN;
def n = BarNumber();
def h = high;
def l = low;
def c = Source;
def style = if signalStyle==signalStyle.Arrows then 1 else
if signalStyle==signalStyle.Bubbles then -1 else 0;
#// ~~ var {
def val;
def falsebreakoutup;
def falsebreakoutdown;
def count;
def indx0;
def indx1;
def bar;
#// ~~ smoothing {
script smoothing {
input maType = "WMA";
input src = close;
input len = 14;
def ma = if maType == "WMA" then WMA(src, len) else
if maType == "HMA" then HullMovingAvg(src, len) else src;
plot return = ma;
}
#// ~~ new high/low {
def hSrc = If(aggressiveMode, l, h);
def lSrc = If(aggressiveMode, h, l);
def hh = Highest(hSrc, lookbackPeriod);
def ll = Lowest(lSrc, lookbackPeriod);
def hi = smoothing(smoothingType, hh, smoothingLength);
def lo = smoothing(smoothingType, ll, smoothingLength);
def condHi = hi > hi[1] and hi[1] <= hi[2];
def condLo = lo < lo[1] and lo[1] >= lo[2];
#// ~~ count {
if condHi {
count = If(count[1] > 0, 0, count[1]) - 1;
val = l;
indx0 = n;
indx1 = indx0[1];
bar = n;
} else {
if condLo {
count = If(count[1] < 0, 0, count[1]) + 1;
val = h;
indx0 = n;
indx1 = indx0[1];
bar = n;
} else {
count = If(falsebreakoutup[1] or falsebreakoutdown[1], 0, count[1]);
val = val[1];
indx0 = indx0[1];
indx1 = indx1[1];
bar = n;
}
}
#// ~~ cond {
def minbars = (indx1 + minperiod) < indx0;
def maxvalid = (n - maxperiod) <= indx0;
def breakD = if c < val then breakD[1] + 1 else 0;
def breakU = if c > val then breakU[1] + 1 else 0;
def breakdown = breakD == 1;
def breakup = breakU == 1;
falsebreakoutup = count < -1 and breakdown and maxvalid and minbars;
falsebreakoutdown = count > 1 and breakup and maxvalid and minbars;
#//~~~}
def ComUp = CompoundValue(1, if falsebreakoutup then val else ComUp[1], val);
def ComDn = CompoundValue(1, if falsebreakoutdown then val else ComDn[1], val);
def valUp = if ComUp==ComUp[1] then valUp[1] + 1 else 0;
def valDn = if ComDn==ComDn[1] then valDn[1] + 1 else 0;
plot UpLine = if isNaN(c) or if(autoExtendLines,no,valUp>MaxLinesLength) then na else ComUp[-2];
plot DnLine = if isNaN(c) or if(autoExtendLines,no,valDn>MaxLinesLength) then na else ComDn[-2];
UpLine.SetPaintingStrategy(PaintingStrategy.POINTS);
DnLine.SetPaintingStrategy(PaintingStrategy.POINTS);
UpLine.SetDefaultColor(Color.DARK_RED);
DnLine.SetDefaultColor(Color.DARK_GREEN);
UpLine.SetHiding(!FalseBreakoutLines);
DnLine.SetHiding(!FalseBreakoutLines);
#// ~~ plot {
plot hiBand = hi;
hiBand.SetDefaultColor(Color.DARK_RED);
hiBand.SetHiding(!HighLowBand);
plot loBand= lo;
loBand.SetDefaultColor(Color.DARK_GREEN);
loBand.SetHiding(!HighLowBand);
plot valLine = val;
valLine.SetHiding(yes);
#---- Signals
plot ArrowDn = if style>0 and falsebreakoutup then h else na;
plot ArrowUp = if style>0 and falsebreakoutdown then l else na;
ArrowDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowUp.SetPaintingStrategy(PaintingStrategy.ARROW_Up);
ArrowDn.SetDefaultColor(Color.MAGENTA);
ArrowUp.SetDefaultColor(Color.CYAN);
ArrowDn.SetLineWeight(2);
ArrowUp.SetLineWeight(2);
#-- Bubbles
AddChartBubble(style<0 and falsebreakoutup, h, "FalseBreakUp", Color.MAGENTA, yes);
AddChartBubble(style<0 and falsebreakoutdown, l, "FalseBreakDn", Color.CYAN, no);
#-- Cloud
AddCloud(if !highlightArea then na else valLine, UpLine, CreateColor(1, 25, 16), CreateColor(38, 0, 0));
AddCloud(if !highlightArea then na else valLine, DnLine, CreateColor(1, 25, 16), CreateColor(38, 0, 0));
#---- END