Mandelbrot->6 Variations

# ─────────────────────────────────────────────
# │ MarketFragments.com | DNA & Market │
# │ info@marketfragments.com │
# │ www.marketfragments.com │
# ─────────────────────────────────────────────
# Time →
# │
# █ █ █│ █
# █ █ █ │ █ █
# █ █ █ │ █ █ █ ╭─╮
# █ █ █ │ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ ╰─────╯
# ──────┴──────────────────────────────────────────────────────────────
# T1 T2 T3 T4 T5 T6
#
# Mandelbrot->6 Variations
declare lower;
# Mandelbrot Logic
# /ES 1 minute chart cluster4 .0276 max:-.0003 min: -0.0006
# /ES 5 min chart cluster2 .0209 Max: -.0006 min: -.00026
input MaxIter =100; # Number of Mandelbrot iterations
input Threshold = 4.0; # Value at which we consider the iteration to be unbounded
def o = open;
def c = close;
def h = high;
def l = low;
def xHL2 = Hl2;
def v = volume;
def tcnt = tick_count;
def isvwap = vwap;
def nan = Double.NaN;
def bn = BarNumber();
def tickSize = if !IsNaN(TickSize()) then TickSize() else .01;
def tickValue = if !IsNaN(TickValue()) then TickValue() else .01;
def re = hl2 / hl2[1] - 1; # Using daily return as a simplification
# Define the iterative calculation within the fold function for Mandelbrot set
def n = fold i = 1 to MaxIter with z = 0.0 while (z * z < Threshold) do z * z + re;
def isn = if n * n < Threshold then n else MaxIter;
addChartBubble(isn>=-0.0006 && isn<=-0.0026,isn,"yes",color.cyan);
# Newton's Method
def newtonn = fold i2 = 1 to MaxIter with z2 = 0.0 while (z2 * z2 < Threshold) do z2 - ((z2 * z2 - 1) / (2 * z2));
def isn_newton = if newtonn * newtonn < Threshold then newtonn else MaxIter;
# Distance Estimation
def distEst = Absvalue(n) / Absvalue(re);
def isn_dist = if n * n < Threshold then 0 else distEst;
# Continuous Potential Method
def potential = Absvalue(re / n);
def isn_potential = if n * n < Threshold then 0 else potential;
# Escape Time Algorithm with Different Functions (using z^3 + c function)
def zCube = fold i3 = 1 to MaxIter with z3 = 0.0 while (z3 * z3 * z3 < Threshold) do z3 * z3 * z3 + re;
def isn_zCube = if zCube * zCube * zCube < Threshold then zCube else MaxIter;
# Exponential Smoothing
def expSmooth = ExpAverage(isn, 10);
def isn_expSmooth = if isn * isn < Threshold then expSmooth else MaxIter;
def f_mal;
input choose = { In_set,Newton, Distance, Continuous_Potential, default Escape_Time,ExpSmoothing};
switch (choose ) {
case In_set:
f_mal = isn;
case Newton:
f_mal = isn_newton;
case Distance:
f_mal = isn_dist ;
case Continuous_Potential:
f_mal = isn_potential ;
case Escape_Time:
f_mal = isn_zCube ;
case ExpSmoothing:
f_mal = isn_expSmooth ;
}
addlabel(1,choose,color.white);
# Anomaly Detection Logic (based on your previous script)
input length = 15; # Example length
def mean = Average(f_mal , length);
def stdDev = StDev(f_mal, length);
def upperBand1 = mean + stdDev;
def lowerBand1 = mean - stdDev;
def upperBand2 = mean + (3* stdDev);
def lowerBand2 = mean - (3 * stdDev);
def isAnomaly = f_mal > upperBand2 or f_mal < lowerBand2;
def mRoc = (f_mal - isn[1]) / isn[1] * 100;
def sig = f_mal * 100;
def issignal = if sig == 0 then 1 else 0;
def cnt = if f_mal< upperBand1 && f_mal>lowerBand1 then cnt[1]+1 else 0;
# Plotting
plot Mandelbrot = f_mal;
# Anomaly Detection Plots
plot UpperBand1a = upperBand1;
plot LowerBand1a = lowerBand1;
plot UpperBand2a = upperBand2;
plot LowerBand2a = lowerBand2;
plot breakout = if cnt==0 && cnt[1]>=6 then f_mal else double.nan;
breakout.SetLineWeight(4);
breakout.SetPaintingStrategy(paintingStrategy = PaintingStrategy.POINTS);
breakout.SetDefaultColor(color.cyan);
# Coloring Plots
Mandelbrot.SetDefaultColor(GetColor(1));
UpperBand1a.assignValueColor( if upperBand1< upperBand1[1] then color.red else color.green) ;
LowerBand1a.assignValueColor( if lowerBand1> lowerBand1[1] then color.red else color.green) ;
UpperBand2a.assignValueColor( if upperBand2< upperBand2[1] then color.red else color.green) ;
LowerBand2a.assignValueColor( if lowerBand2> lowerBand2[1] then color.red else color.green) ;

