#// Indicator for TOS
#// © BigBeluga
# indicator("Fibonacci Bands [BigBeluga]", overlay=true, max_lines_count = 500)
# Converted by Sam4Cok@Samer800 - 12/2024
input displayOptions = {Default "Band Lines", "Regression Lines", "Band & Regression", "Don't Show"};
input linearRegressionLookback = 150;
input extendRegressionLines = yes;
input period = 20; # "Period"
input width = 1.0; # "Width"
input price = close; # "Source"
input fibRatio1 = 1.618; # "Fibonacci Ratio 1"
input fibRatio2 = 2.618; # "Fibonacci Ratio 2"
input fibRatio3 = 4.236; # "Fibonacci Ratio 3"
input ExtendBands = 30; # "Extend Bands"
input LiquiditySweep = yes; # "Liquidity Sweep"
input showLabel = yes;
def na = Double.NaN;
def last = IsNaN(close);
def extBand = Max(ExtendBands, 5);
def reg; def fibo;
Switch (displayOptions) {
Case "Don't Show" :
fibo = no;
reg = no;
Case "Band & Regression" :
fibo = yes;
reg = yes;
Case "Regression Lines" :
fibo = no;
reg = yes;
Default :
fibo = yes;
reg = no;
}
Script smma {
input src = close;
input length = 100;
def sma = Average(src, length);
def smma_value = CompoundValue(1, (smma_value[1] * (length - 1) + src) / length, sma);
plot out = smma_value;
}
def ATR = smma(atr(Length = 200), 100);
def smma = smma(price, period);
#// Calculate Fibonacci Levels
def r1 = ATR * fibRatio1 * width;
def r2 = ATR * fibRatio2 * width;
def r3 = ATR * fibRatio3 * width;
#// Calculate Fibonacci Bands
def fibTop1 = smma + r1;
def fibTop2 = smma + r2;
def fibTop3 = smma + r3;
def fibBott1 = smma - r1;
def fibBott2 = smma - r2;
def fibBott3 = smma - r3;
Script extendLine {
input src = close;
input extend = 30;
def slop_i = Round(extend/10, 0);
def slope = (src - src[slop_i]) / slop_i;
def ext; def y1; def slp;
if !isNaN(close) {
ext = 0;
y1 = src;
slp = slope;
} else {
ext = if ext[1] >= extend then Double.NaN else ext[1] + 1;
y1 = y1[1];
slp = slp[1];
}
plot line = y1 +(ext * slp);
}
def l_t3 = extendLine(fibTop3, extBand);
def l_t2 = extendLine(fibTop2, extBand);
def l_t1 = extendLine(fibTop1, extBand);
def l_m = extendLine(smma, extBand);
def l_b3 = extendLine(fibBott3, extBand);
def l_b2 = extendLine(fibBott2, extBand);
def l_b1 = extendLine(fibBott1, extBand);
def mid_trend = l_m > l_m[1];
plot linT3 = if !reg then na else InertiaAll(l_t3[-extBand], linearRegressionLookback, extendToRight = extendRegressionLines);
plot linT2 = if !reg then na else InertiaAll(l_t2[-extBand], linearRegressionLookback, extendToRight = extendRegressionLines);
plot linT1 = if !reg then na else InertiaAll(l_t1[-extBand], linearRegressionLookback, extendToRight = extendRegressionLines);
plot linM = if !reg then na else InertiaAll(l_m[-extBand], linearRegressionLookback, extendToRight = extendRegressionLines);
plot linB1 = if !reg then na else InertiaAll(l_b1[-extBand], linearRegressionLookback, extendToRight = extendRegressionLines);
plot linB2 = if !reg then na else InertiaAll(l_b2[-extBand], linearRegressionLookback, extendToRight = extendRegressionLines);
plot linB3 = if !reg then na else InertiaAll(l_b3[-extBand], linearRegressionLookback, extendToRight = extendRegressionLines);
plot top3 = if fibo and l_t3 then l_t3 else na;
plot top2 = if fibo and l_t2 then l_t2 else na;
plot top1 = if fibo and l_t1 then l_t1 else na;
plot midSm = if mid_trend and l_m then l_m else na;
plot midSm1 = if mid_trend then na else if l_m then l_m else na;
plot btm3 = if fibo and l_b3 then l_b3 else na;
plot btm2 = if fibo and l_b2 then l_b2 else na;
plot btm1 = if fibo and l_b1 then l_b1 else na;
linT3.SetDefaultColor(Color.MAGENTA);
linT2.SetDefaultColor(Color.MAGENTA);
linT1.SetDefaultColor(Color.MAGENTA);
linM.SetDefaultColor(Color.WHITE);
linB1.SetDefaultColor(Color.CYAN);
linB2.SetDefaultColor(Color.CYAN);
linB3.SetDefaultColor(Color.CYAN);
midSm1.SetStyle(Curve.MEDIUM_DASH);
top3.SetDefaultColor(Color.RED);
top2.SetDefaultColor(Color.DARK_RED);
top1.SetDefaultColor(Color.DARK_RED);
midSm.SetDefaultColor(Color.GRAY);
midSm1.SetDefaultColor(Color.GRAY);
btm3.SetDefaultColor(Color.GREEN);
btm2.SetDefaultColor(Color.DARK_GREEN);
btm1.SetDefaultColor(Color.DARK_GREEN);
AddCloud(l_t3, l_t2, Color.DARK_RED);
AddCloud(l_b2, l_b3, Color.DARK_GREEN);
#---
Script Pivot {
input series = close;
input leftBars = 10;
input rightBars = 10;
input isHigh = yes;
def na = Double.NaN;
def HH = series == Highest(series, leftBars + 1);
def LL = series == Lowest(series, leftBars + 1);
def pivotRange = (leftBars + rightBars + 1);
def leftEdgeValue = if series[pivotRange] ==0 then na else series[pivotRange];
def pvtCond = !isNaN(series) and leftBars > 0 and rightBars > 0 and !isNaN(leftEdgeValue);
def barIndexH = if pvtCond then
fold i = 1 to rightBars + 1 with p=1 while p do
series > GetValue(series, - i) else na;
def barIndexL = if pvtCond then
fold j = 1 to rightBars + 1 with q=1 while q do
series < GetValue(series, - j) else na;
def PivotPoint;
if isHigh {
PivotPoint = if HH and barIndexH then series else na;
} else {
PivotPoint = if LL and barIndexL then series else na;
}
plot pvt = PivotPoint;
}
def pvtHi = pivot(high[1], 4, 1, yes);
def pvtLo = pivot(low[1], 4, 1, no);
def sig_dn = mid_trend and !isNaN(pvtHi) and high > fibTop3 and (high Crosses Below high[1]);
def sig_up = !mid_trend and !isNaN(pvtLo) and low < fibBott3 and (low Crosses Above low[1]);
plot sigDn = if sig_dn then high else na;
plot sigUp = if sig_up then low else na;
sigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
sigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
sigDn.SetDefaultColor(Color.CYAN);
sigUp.SetDefaultColor(Color.MAGENTA);
def l1;
def l1P = CompoundValue(1, l1[1], high);
def cntDn = if sig_dn then 0 else cntDn[1] + 1;
l1 = if sig_dn then high[1] else
if (if close < open then high > l1p and open < l1p else high > l1p and close < l1p) then na else
if ((close > l1p) and (close[1] <= l1p)) then na else if cntDn > 80 then na else l1p;
def l2;
def l2P = CompoundValue(1, l2[1], low);
def cntUp = if sig_up then 0 else cntUp[1] + 1;
l2 = if sig_up then low[1] else
if (if close < open then low < l2p and close > l2p else low < l2p and open > l2p) then na else
if ((close < l2p) and (close[1] >= l2p)) then na else if cntUp > 80 then na else l2p;
plot liqDn = if LiquiditySweep and l1 then l1 else na;
plot liqUp = if LiquiditySweep and l2 then l2 else na;
liqDn.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
liqDn.SetDefaultColor(Color.CYAN);
liqUp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
liqUp.SetDefaultColor(Color.MAGENTA);
AddLabel(showLabel, "100% (" + AsDollars(l_t3[-extBand]) + ")", Color.DARK_RED);
AddLabel(showLabel, "61.8% (" + AsDollars(l_t2[-extBand]) + ")", Color.RED);
AddLabel(showLabel, "38.2% (" + AsDollars(l_t1[-extBand]) + ")", Color.LIGHT_RED);
AddLabel(showLabel, "0.0% (" + AsDollars(l_m[-extBand]) + ")", Color.LIGHT_GRAY);
AddLabel(showLabel, "38.2% (" + AsDollars(l_b1[-extBand]) + ")", Color.LIGHT_GREEN);
AddLabel(showLabel, "61.8% (" + AsDollars(l_b2[-extBand]) + ")", Color.GREEN);
AddLabel(showLabel, "100% (" + AsDollars(l_b3[-extBand]) + ")", Color.DARK_GREEN);
#-- END of CODE