#// © Zeiierman
#https://www.tradingview.com/script/Qt7fqntR-Breakout-Probability-Expo/
#indicator("Breakout Probability (Expo)"
# Converted and mod by Sam4Cok@Samer800 "Not typical Conv" - 11/2022
# Update - Added 5 brakout Probability lines
#/ ~~ Inputs {
input ShowLabel = yes;
input ShowBubbles = yes;
input NumberOfLines = 5; # max 5 lines
input NumberOfLabels = 5; # max 5 Labels
input PercentageStep = 0.1; # "Percentage Step"
def na = Double.NaN;
#--- Color---
DefineGlobalColor("Green1" , CreateColor(0,255,0));
DefineGlobalColor("Green2" , CreateColor(48,255,48));
DefineGlobalColor("Green3" , CreateColor(96,255,96));
DefineGlobalColor("Green4" , CreateColor(143,255,143));
DefineGlobalColor("Green5" , CreateColor(191,255,191));
DefineGlobalColor("Red1" , CreateColor(255,0,0));
DefineGlobalColor("Red2" , CreateColor(255,48,48));
DefineGlobalColor("Red3" , CreateColor(255,96,96));
DefineGlobalColor("Red4" , CreateColor(255,143,143));
DefineGlobalColor("Red5" , CreateColor(255,191,191));
#-------------
script nz {
input data = close;
input repl = 0;
def ret_val = if IsNaN(data) then repl else data;
plot return = ret_val;
}
def bar = BarNumber();
def lines = if NumberOfLines < 2 then na else
if NumberOfLines > 5 then 5 else NumberOfLines;
def labels = if NumberOfLabels < 2 then na else
if NumberOfLabels > 5 then 5 else NumberOfLabels;
def o; def c; def h; def l;
o = open;
h = high;
l = low;
c = close;
#--------------------------------------------------------
#// ~~ Variables {
def b = BarNumber();
def perStep = if PercentageStep >= 0 then PercentageStep else 0;
def step = c * (perStep / 100);
#/ ~~ Save Number Of Green & Red Candles {
def gr = if IsNaN(c[1] > o[1]) then c[1] > c[2] else (c[1] > o[1]);
def re = if IsNaN(c[1] < o[1]) then c[1] < c[2] else (c[1] < o[1]);
def green = if gr != yes then na else gr;
def red = if re != yes then na else re;
def totalG;
def totalR;
if green {
totalG = nz(totalG[1] + 1, 1);
totalR = nz(totalR[1]);
} else {
if red {
totalG = nz(totalG[1]);
totalR = nz(totalR[1] + 1, 1);
} else {
totalG = nz(totalG[1]);
totalR = nz(totalR[1]);
}
}
script score {
input x1 = 0;
input l = low;
input h = high;
input green1 = no;
input red1 = no;
input totalG1 = 0;
input totalR1 = 0;
def x = x1;
def green = green1;
def red = red1;
def totalG = totalG1;
def totalR = totalR1;
def ghh;
def a11;
def gll;
def b11;
def rhh;
def a22;
def rll;
def b22;
def hh;
def ll;
hh = if IsNaN(hh[1]) then 0 else h >= (h[1] + x);
ll = if IsNaN(ll[1]) then 0 else l <= (l[1] - x);
if green and hh {
ghh = ghh[1] + 1;
gll = gll[1];
rhh = rhh[1];
rll = rll[1];
a11 = Round((ghh + 1) / totalG * 100, 2);
b11 = b11[1];
a22 = a22[1];
b22 = b22[1];
} else
if green and ll {
ghh = ghh[1];
gll = gll[1] + 1;
rhh = rhh[1];
rll = rll[1];
a11 = a11[1];
b11 = Round((gll + 1) / totalG * 100, 2);
a22 = a22[1];
b22 = b22[1];
} else
if red and hh {
ghh = ghh[1];
gll = gll[1];
rhh = rhh[1] + 1;
rll = rll[1];
a11 = a11[1];
b11 = b11[1];
a22 = Round((rhh + 1) / totalR * 100, 2);
b22 = b22[1];
} else
if red and ll {
ghh = ghh[1];
gll = gll[1];
rhh = rhh[1];
rll = rll[1] + 1;
a11 = a11[1];
b11 = b11[1];
a22 = a22[1];
b22 = Round((rll + 1) / totalR * 100, 2);
} else {
ghh = ghh[1];
gll = gll[1];
rhh = rhh[1];
rll = rll[1];
a11 = a11[1];
b11 = b11[1];
a22 = a22[1];
b22 = b22[1];
}
plot a1 = a11;
plot b1 = b11;
plot a2 = a22;
plot b2 = b22;
}
#-----
def a0 = Score(0, l, h, green, red, totalG, totalR).a1;
def b0 = Score(0, l, h, green, red, totalG, totalR).b1;
def a00 = Score(0, l, h, green, red, totalG, totalR).a2;
def b00 = Score(0, l, h, green, red, totalG, totalR).b2;
#-----
def a1 = Score(step, l, h, green, red, totalG, totalR).a1;
def b1 = Score(step, l, h, green, red, totalG, totalR).b1;
def a11 = Score(step, l, h, green, red, totalG, totalR).a2;
def b11 = Score(step, l, h, green, red, totalG, totalR).b2;
#------
def a2 = Score(step * 2, l, h, green, red, totalG, totalR).a1;
def b2 = Score(step * 2, l, h, green, red, totalG, totalR).b1;
def a22 = Score(step * 2, l, h, green, red, totalG, totalR).a2;
def b22 = Score(step * 2, l, h, green, red, totalG, totalR).b2;
#-----
def a3 = Score(step * 3, l, h, green, red, totalG, totalR).a1;
def b3 = Score(step * 3, l, h, green, red, totalG, totalR).b1;
def a33 = Score(step * 3, l, h, green, red, totalG, totalR).a2;
def b33 = Score(step * 3, l, h, green, red, totalG, totalR).b2;
#-----
def a4 = Score(step * 4, l, h, green, red, totalG, totalR).a1;
def b4 = Score(step * 4, l, h, green, red, totalG, totalR).b1;
def a44 = Score(step * 4, l, h, green, red, totalG, totalR).a2;
def b44 = Score(step * 4, l, h, green, red, totalG, totalR).b2;
#--------Labels-----------------
AddLabel(ShowLabel and green[1], a0 + "%" + " - " + b0 + "%", GlobalColor("Green1"));
AddLabel(ShowLabel and red[1], a00 + "%" + " - " + b00 + "%", GlobalColor("red1"));
AddLabel(ShowLabel and labels>1 and green[1], a1 + "%" + " - " + b1 + "%", GlobalColor("Green2"));
AddLabel(ShowLabel and labels>1 and red[1], a11 + "%" + " - " + b11 + "%", GlobalColor("red2"));
AddLabel(ShowLabel and labels>2 and green[1], a2 + "%" + " - " + b2 + "%", GlobalColor("Green3"));
AddLabel(ShowLabel and labels>2 and red[1], a22 + "%" + " - " + b22 + "%", GlobalColor("red3"));
AddLabel(ShowLabel and labels>3 and green[1], a3 + "%" + " - " + b3 + "%", GlobalColor("Green4"));
AddLabel(ShowLabel and labels>3 and red[1], a33 + "%" + " - " + b33 + "%", GlobalColor("red4"));
AddLabel(ShowLabel and labels>4 and green[1], a4 + "%" + " - " + b4 + "%", GlobalColor("Green5"));
AddLabel(ShowLabel and labels>4 and red[1], a44 + "%" + " - " + b44 + "%", GlobalColor("red5"));
AddLabel(ShowLabel and !green[1] and !red[1], "No Data" , Color.GRAY);
#---- Line 00
def countHi = if b < 1 then na else if IsNaN(c) and !IsNaN(h[1]) then 1 else countHi[1] + 1;
def hi = GetValue(h, countHi + 1);
def countLo = if b < 1 then na else if IsNaN(c) and !IsNaN(l[1]) then 1 else countLo[1] + 1;
def lo = GetValue(l, countLo + 1);
def upLine = if IsNaN(c) then na else hi[-1];
def DnLine = if IsNaN(c) then na else lo[-1];
def PrevH = CompoundValue(1, if IsNaN(upLine) then PrevH[1] else h[1], na);
def PrevL = CompoundValue(1, if IsNaN(DnLine) then PrevL[1] else l[1], na);
plot LineH0 = if !IsNaN(c[1]) then PrevH[-1] else na;
LineH0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineH0.SetDefaultColor(GlobalColor("Green1"));
LineH0.SetHiding(!ShowBubbles);
plot LineL0 = if !IsNaN(c[1]) then PrevL[-1] else na;
LineL0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineL0.SetDefaultColor(GlobalColor("Red1"));
LineL0.SetHiding(!ShowBubbles);
AddChartBubble(ShowBubbles and green[1], upLine[1], a0 + "%", GlobalColor("Green1"), yes);
AddChartBubble(ShowBubbles and green[1], DnLine[1], b0 + "%" , GlobalColor("Red1"), no);
AddChartBubble(ShowBubbles and red[1], upLine[1], a00 + "%", GlobalColor("Green1"), yes);
AddChartBubble(ShowBubbles and red[1], DnLine[1], b00 + "%", GlobalColor("Red1"), no);
#--- Line 11
def stepH1 = if !IsNaN(LineH0) then (LineH0 + step) else na;
def stepL1 = if !IsNaN(LineL0) then (LineL0 - step) else na;
plot LineH1 = if lines > 1 then stepH1[1] else na;
LineH1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineH1.SetDefaultColor(GlobalColor("Green2"));
plot LineL1 = if lines > 1 then stepL1[1] else na;
LineL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineL1.SetDefaultColor(Color.PINK);
AddChartBubble(ShowBubbles and green[1] and IsNaN(LineH1[-1]), LineH1, a1 + "%", GlobalColor("Green2"), yes);
AddChartBubble(ShowBubbles and green[1] and IsNaN(LineL1[-1]), LineL1, b1 + "%" , GlobalColor("Red2"), no);
AddChartBubble(ShowBubbles and red[1] and IsNaN(LineH1[-1]), LineH1, a11 + "%", GlobalColor("Green2"), yes);
AddChartBubble(ShowBubbles and red[1] and IsNaN(LineL1[-1]), LineL1, b11 + "%", GlobalColor("Red2"), no);
# ---Line 22
def stepH2 = if !IsNaN(LineH0) then (LineH0 + step * 2) else na;
def stepL2 = if !IsNaN(LineL0) then (LineL0 - step * 2) else na;
plot LineH2 = if lines > 2 then stepH2[1] else na;
LineH2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineH2.SetDefaultColor(GlobalColor("Green3"));
plot LineL2 = if lines > 2 then stepL2[1] else na;
LineL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineL2.SetDefaultColor(Color.PINK);
AddChartBubble(ShowBubbles and green[1] and IsNaN(LineH2[-1]), LineH2, a2 + "%", GlobalColor("Green3"), yes);
AddChartBubble(ShowBubbles and green[1] and IsNaN(LineH2[-1]), LineL2, b2 + "%" , GlobalColor("Red3"), no);
AddChartBubble(ShowBubbles and red[1] and IsNaN(LineH2[-1]), LineH2, a22 + "%", GlobalColor("Green3"), yes);
AddChartBubble(ShowBubbles and red[1] and IsNaN(LineH2[-1]), LineL2, b22 + "%", GlobalColor("Red3"), no);
# ----- Line 33
def stepH3 = if !IsNaN(LineH0) then (LineH0 + step * 3) else na;
def stepL3 = if !IsNaN(LineL0) then (LineL0 - step * 3) else na;
plot LineH3 = if lines > 3 then stepH3[1] else na;
LineH3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineH3.SetDefaultColor(GlobalColor("Green4"));
plot LineL3 = if lines > 3 then stepL3[1] else na;
LineL3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineL3.SetDefaultColor(Color.PINK);
AddChartBubble(ShowBubbles and green[1] and IsNaN(LineH3[-1]), LineH3, a3 + "%", GlobalColor("Green4"), yes);
AddChartBubble(ShowBubbles and green[1] and IsNaN(LineH3[-1]), LineL3, b3 + "%" , GlobalColor("Red4"), no);
AddChartBubble(ShowBubbles and red[1] and IsNaN(LineH3[-1]), LineH3, a33 + "%", GlobalColor("Green4"), yes);
AddChartBubble(ShowBubbles and red[1] and IsNaN(LineH3[-1]), LineL3, b33 + "%", GlobalColor("Red4"), no);
# ---- Line 44
def stepH4 = if !IsNaN(LineH0) then (LineH0 + step * 4) else na;
def stepL4 = if !IsNaN(LineL0) then (LineL0 - step * 4) else na;
plot LineH4 = if lines > 4 then stepH4[1] else na;
LineH4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineH4.SetDefaultColor(GlobalColor("Green5"));
plot LineL4 = if lines > 4 then stepL4[1] else na;
LineL4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LineL4.SetDefaultColor(Color.PINK);
AddChartBubble(ShowBubbles and green[1] and IsNaN(LineH4[-1]), LineH4, a4 + "%", GlobalColor("Green5"), yes);
AddChartBubble(ShowBubbles and green[1] and IsNaN(LineH4[-1]), LineL4, b4 + "%" , GlobalColor("Red5"), no);
AddChartBubble(ShowBubbles and red[1] and IsNaN(LineH4[-1]), LineH4, a44 + "%", GlobalColor("Green5"), yes);
AddChartBubble(ShowBubbles and red[1] and IsNaN(LineH4[-1]), LineL4, b44 + "%", GlobalColor("Red5"), no);
#--- Backtest
script Backtest {
input v = close;
def h = high;
def h1 = high[1];
def l = low;
def p1;
def p2;
if v == h1 {
if h >= v {
p1 = p1[1] + 1;
p2 = p2[1];
} else {
p1 = p1[1];
p2 = p2[1] + 1;
}
} else
if l <= v {
p1 = p1[1] + 1;
p2 = p2[1];
} else {
p1 = p1[1];
p2 = p2[1] + 1;
}
plot p11 = p1;
plot p22 = p2;
}
#//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
#//Run Backtest Function
def p1 = Backtest(if green then (if a0 >= b0 then h[1] else l[1])
else (if a00 >= b00 then h[1] else l[1])).p11;
def p2 = Backtest(if green then (if a0 >= b0 then h[1] else l[1])
else (if a00 >= b00 then h[1] else l[1])).p22;
#//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
# //Calulate WinRatio
def Win = p1;
def Lose = p2;
def total = Win + Lose;
def WR = Round(Win / total * 100, 2);
AddLabel(ShowLabel, "Win(" + WR + "%)", Color.WHITE);
AddLabel(ShowLabel, "Size(" + total + ")", Color.WHITE);
#---- END code