#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © LuxAlgo
#study(title=" Support and Resistance Levels with Breaks",shorttitle = " Support and Resistance Levels with Breaks [LuxAlgo]", overlay = true , max_bars_back=1000)
# Converted and mod by Sam4Cok@Samer800 - 01/2023
# Update Added Fibo LEvel - 09/2023
#input ShowBreaks = yes; # title = "Show Breaks" )
input ShowHhLlLabel = yes;
input src = hl2;
input breakout = {"Volume", "Price",Default "None"};
input RepaintingType = {default "On", "Off: Candle Confirmation", "Off: High & Low"};# 'Repainting'
input rightBars = 24;#, title = "Right Bars")
input leftBars = 20;#(15, title = "Left Bars ")
input volumeThreshold = 20;# title = "Volume Threshold")
#//
def na = Double.NaN;
def last = isNaN(close);
def VolBreak = breakout==breakout."Volume";
def PriceBreak = breakout==breakout."Price";
#def rTon = input_repType == input_repType."On";
#def rTcc = input_repType == input_repType."Off: Candle Confirmation";
#def rThv = input_repType == input_repType."Off: High & Low";
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red" , CreateColor(255, 82, 82));
script fixnan {
input source = close;
def fix = if !IsNaN(source) then source else fix[1];
plot result = fix;
}
#drawBox(condition, y1, y2, color) =>
script drawBox {
input condition = yes;
input y1 = high;
input y2 = low;
def boxHi = if condition then y1 else boxHi[1];
def boxLo = if condition then y2 else boxLo[1];
plot ph = if !IsNaN(close[5]) then if !boxHi then high else boxHi else Double.NaN;
plot pl = if !IsNaN(close[5]) then if !boxLo then low else boxLo else Double.NaN;
}
#repaint(c1, c2, c3) =>
script repaint {
input type = "On";
input c1 = no;
input c2 = no;
input c3 = no;
def rTon = type == "On";
def rTcc = type == "Off: Candle Confirmation";
def rThv = type == "Off: High & Low";
def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else Double.NaN;
plot out = repaint;
}
script FindPivots {
input dat = close; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input lbL = 5; # default Pivot Lookback Left
input lbR = 1; # default Pivot Lookback Right
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
fold a = 1 to lbR + 1 with b=1 while b do
if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
then dat else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
then dat else _nan;
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def ph = FindPivots(high, 1 , leftBars, rightBars);
def pl = FindPivots(low, -1, leftBars, rightBars);
def highUsePivot = fixnan(ph);
def lowUsePivot = fixnan(pl);
#// Box Height
def s_yLoc = if low[1] > low[-1] then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];
plot r1 = drawBox((highUsePivot - highUsePivot[1]), highUsePivot, r_yLoc).ph;
plot r2 = drawBox((highUsePivot - highUsePivot[1]), highUsePivot, r_yLoc).pl;
plot s1 = drawBox((lowUsePivot - lowUsePivot[1]), s_yLoc, lowUsePivot).ph;
plot s2 = drawBox((lowUsePivot - lowUsePivot[1]), s_yLoc, lowUsePivot).pl;
#plot r1 = if IsNaN(src) or highUsePivot == 0 then na else highUsePivot;
r1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
r1.SetDefaultColor(GlobalColor("green"));
r2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
r2.SetDefaultColor(GlobalColor("green"));
#plot s1 = if IsNaN(src) or lowUsePivot == 0 then na else lowUsePivot;
s1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1.SetDefaultColor(GlobalColor("red"));
s2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s2.SetDefaultColor(GlobalColor("red"));
#//Volume %
def short = ExpAverage(volume, 5);
def long = ExpAverage(volume, 10);
def osc = 100 * (short - long) / long;
#//For breaks with volume
def up = (open - close) < (high - open);
def Dn = (open - low) > (close - open);
def u11 = (VolBreak and close crosses below s2);
def u22 = (VolBreak and low crosses below s2);
def u33 = (VolBreak and close crosses below s2) and !isNaN(close[1]);
def o11 = (VolBreak and close crosses above r1);
def o22 = (VolBreak and high crosses above r1);
def o33 = (VolBreak and close crosses above r1) and !isNaN(close[1]);
def CrossUp = repaint(RepaintingType, u11, u22, u33);
def CrossDn = repaint(RepaintingType, o11, o22, o33);
#def CrossUp = VolBreak and src crosses above highUsePivot;
#def CrossDn = VolBreak and src crosses below lowUsePivot;
def VolThresh = osc > volumeThreshold;
def volDn = CrossDn and !up and VolThresh;
def volUp = CrossUp and !Dn and VolThresh;
#//For bull / bear wicks
def BearWicks = CrossDn and up and VolThresh;
def BullWicks = CrossUp and Dn and VolThresh;
# Support Result breaks
def sBreakDn = CrossDn and up and !VolThresh;
def rBreakUp = CrossUp and Dn and !VolThresh;
#------ Strength
def strgUp = volUp * 3 + BullWicks * 2 + rBreakUp;
def StrgDn = volDn * 3 + BearWicks * 2 + sBreakDn;
AddChartBubble(StrgDn > 2 , low, "Strong", Color.GREEN, no);
AddChartBubble(strgUp > 2 , high, "Strong", Color.RED, yes);
AddChartBubble(StrgDn > 1 and StrgDn <= 2 , low, "Normal", Color.DARK_GREEN, no);
AddChartBubble(strgUp > 1 and strgUp <= 2 , high, "Normal", Color.DARK_RED, yes);
AddChartBubble(StrgDn > 0 and StrgDn <= 1 , low, "Weak", Color.LIGHT_GREEN, no);
AddChartBubble(strgUp > 0 and strgUp <= 1 , high, "Weak", Color.PINK, yes);
#--- HH/LL label
def hh = if highUsePivot > highUsePivot[1] then highUsePivot else na;
def ll = if lowUsePivot < lowUsePivot[1] then lowUsePivot else na;
def hl = if lowUsePivot > lowUsePivot[1] then lowUsePivot else na;
def lh = if highUsePivot < highUsePivot[1] then highUsePivot else na;
AddChartBubble(ShowHhLlLabel and ll, ll, "LL", Color.RED, no);
AddChartBubble(ShowHhLlLabel and hh, hh, "HH", Color.GREEN, yes);
AddChartBubble(ShowHhLlLabel and hl, hl, "HL", Color.DARK_GREEN, no);
AddChartBubble(ShowHhLlLabel and lh, lh, "LH", Color.DARK_RED, yes);
#// Breakout Event
def sBreak;# = na
def rBreak;# = na
def sLabel;#
def rLabel;
def u1 = (close crosses below s2);
def u2 = (low crosses below s2);
def u3 = (close crosses below s2) and !isNaN(close);
def o1 = (close crosses above r1);
def o2 = (high crosses above r1);
def o3 = (close crosses above r1) and !isNaN(close);
def cu = repaint(RepaintingType, u1, u2, u3);
def co = repaint(RepaintingType, o1, o2, o3);
if cu and isNaN(sBreak[1]) {
sBreak = yes;
rBreak = na;
sLabel = if PriceBreak then s2 else na;
rLabel = na;
} else
if co and isNaN(rBreak[1]) {
sBreak = na;
rBreak = yes;
sLabel = na;
rLabel = if PriceBreak then r1 else na;
} else {
sBreak = na;
rBreak = na;
sLabel = na;
rLabel = na;
}
AddChartBubble(!isNaN(sLabel), sLabel, "break", Color.RED, yes);
AddChartBubble(!isNaN(rLabel), rLabel, "break", Color.GREEN, no);
#---- END
input showFiboLines = yes;
input showZigZagLine = yes;
DefineGlobalColor("786", CreateColor(244,67,54));
DefineGlobalColor("618", CreateColor(129,199,132));
DefineGlobalColor("500", CreateColor(76,175,80));
DefineGlobalColor("382", CreateColor(0,150,136));
DefineGlobalColor("236", CreateColor(100,181,246));
DefineGlobalColor("000", CreateColor(95,95,95));
def bar = AbsValue(BarNumber());
def naPh = !isNaN(ph);
def naPl = !isNaN(pl);
def phPvt = highUsePivot;
def plPvt = lowUsePivot;
def phBar = if naPh then bar else phBar[1];
def plBar = if naPl then bar else plBar[1];
def max = max(phBar, plBar);
def maxBar = highestAll(max);
def plotCond = bar < maxBar or last[5] or !showFiboLines;
def ZigCond = bar > maxBar - leftBars and showZigZagLine;
def upBar = phBar > plBar;
def fib1 = if plotCond then na else
if upBar then phPvt - (phPvt - plPvt) * 0.236 else
plPvt + (phPvt - plPvt) * 0.236;
def fib2 = if plotCond then na else
if upBar then phPvt - (phPvt - plPvt) * 0.382 else
plPvt + (phPvt - plPvt) * 0.382;
def fib3 = if plotCond then na else
if upBar then phPvt - (phPvt - plPvt) * 0.500 else
plPvt + (phPvt - plPvt) * 0.500;
def fib4 = if plotCond then na else
if upBar then phPvt - (phPvt - plPvt) * 0.618 else
plPvt + (phPvt - plPvt) * 0.618;
def fib5 = if plotCond then na else
if upBar then phPvt - (phPvt - plPvt) * 0.786 else
plPvt + (phPvt - plPvt) * 0.786;
def fib6 = if plotCond then na else
if upBar then phPvt - (phPvt - plPvt) * 1.000 else
plPvt + (phPvt - plPvt) * 1.000;
def zig = if naPh then high else
if naPl then low else na;
plot fibo1 = fib1;
plot fibo2 = fib2;
plot fibo3 = fib3;
plot fibo4 = fib4;
plot fibo5 = fib5;
plot fibo6 = fib6;
plot ZigZag = if ZigCond then zig else na;
ZigZag.EnableApproximation();
ZigZag.SetStyle(Curve.SHORT_DASH);
fibo1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ZigZag.SetDefaultColor(Color.YELLOW);
fibo1.SetDefaultColor(GlobalColor("000"));
fibo2.SetDefaultColor(GlobalColor("236"));
fibo3.SetDefaultColor(GlobalColor("382"));
fibo4.SetDefaultColor(GlobalColor("500"));
fibo5.SetDefaultColor(GlobalColor("618"));
fibo6.SetDefaultColor(GlobalColor("786"));
#-- END of CODe