#https://www.tradingview.com/v/5cV2hGJk/
#//@Dreadblitz
#study(shorttitle="MACD_X", title="DOBLE MACD X", overlay=false)
# Converted by Sam4Cok@Samer800 - 08/2023
declare lower;
input ViewTrendEntrySignal = yes;
input ViewTrendReversalSignal = no;
input ViewAtlasZone = yes;
input ViewChoppinessZone = yes;
input ChoppinessLevel = 50;
input ViewDivergenceChannel = yes;
input ViewDivergenceLabels = yes;
input div_reg_Bull = yes;
input div_reg_Bear = yes;
input div_hid_Bull = no;
input div_hid_Bear = no;
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
#// MACD 2
def L1A = 5;
def L2A = 15;
def tpa = close;
def m0a = ExpAverage(tpa, L1A);
def m1a = ExpAverage(tpa, L2A);
def macd_2 = (m0a - m1a);
plot macd2 = macd_2;#, title='MacD 515'
plot macd2Hist = macd_2;#, title='MacD 515'
macd2.SetLineWeight(2);
macd2.SetDefaultColor(Color.YELLOW);
macd2Hist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
macd2Hist.SetDefaultColor(Color.YELLOW);
#// MACD 1
def L1 = 12;
def L2 = 26;
def tp = close;
def m0 = ExpAverage(tp, L1);
def m1 = ExpAverage(tp, L2);
def macd_1 = (m0 - m1);
plot macd1 = macd_1;#, title='Macd 1226', color=gris, linewidth=2, style=plot.style_line, transp=0)
plot macd1Hist = macd_1;#, title='Macd 1226', color=gris, linewidth=2, style=plot.style_histogram, transp=0)
macd1.SetLineWeight(2);
macd1.SetDefaultColor(Color.GRAY);
macd1Hist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
macd1Hist.SetDefaultColor(Color.GRAY);
#//Atlas Mini
def length = 20;
def src = close;
def mult = 2.0;
def basis = Average(src, length);
def dev = mult * StDev(src, length);
def upper = basis + dev;
def lower = basis - dev;
def uplo = (upper - lower) / upper;
def dbb = Sqrt(uplo) * length;
def dbbmed = ExpAverage(dbb, 120);
def factor = dbbmed * 4 / 5;
def atl = dbb - factor;
def all = if atl > 0 then 0 else 1;
AddCloud(if ViewAtlasZone and all then pos else na, neg, CreateColor(78,0,0)); # 'Zona Atlas'
#//Choppiness Index
def length1 = 14;
def tr = TrueRange(high, close , low);
def str = Sum(tr, length1);
def ll = if low <= close[1] then low else close[1];
def hh = if high >= close[1] then high else close[1];
def ltl = Lowest(ll, length1);
def hth = Highest(hh, length1);
def height = hth - ltl;
def chop = 100 * (lg(str / height) / lg(length1));
def all1 = if chop < ChoppinessLevel then 0 else 1;
AddCloud(if ViewChoppinessZone and all1 then pos else na, neg, CreateColor(78,0,0));# 'Zona Choppiness'
#//Divergencias
def max = highest(macd_2, 100) * 1.5;
def nsc = max;
def nsv = max * -1;
def midpoint = (nsc + nsv) / 2;
def ploff = (nsc - midpoint) / 8;
def _src = macd_2;
def top_fractal = _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0];
def bot_fractal = _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0];
def fractalize = if top_fractal then 1 else if bot_fractal then -1 else 0;
def fractaltop1 = if fractalize > 0 then macd_2[2] else na;
def fractalbot1 = if fractalize < 0 then macd_2[2] else na;
def fractal_top1 = !isNaN(fractaltop1);
def fractal_bot1 = !isNaN(fractalbot1);
def highprev1 = if fractal_top1 then macd_2[2] else highprev1[1];
def highprice1 = if fractal_top1 then high[2] else highprice1[1];
def lowprev1 = if fractal_bot1 then macd_2[2] else lowprev1[1];
def lowprice1 = if fractal_bot1 then low[2] else lowprice1[1];
def high_prev1 = if highprev1 !=highprev1[1] then highprev1[1] else high_prev1[1];
def high_price1 = if highprice1!=highprice1[1] then highprice1[1] else high_price1[1];
def low_prev1 = if lowprev1 !=lowprev1[1] then lowprev1[1] else low_prev1[1];
def low_price1 = if lowprice1 !=lowprice1[1] then lowprice1[1] else low_price1[1];
def regular_bearish_div1 = fractal_top1 and high[2] > high_price1 and macd_2[2] < high_prev1 and div_reg_Bear;
def hidden_bearish_div1 = fractal_top1 and high[2] < high_price1 and macd_2[2] > high_prev1 and div_hid_Bear;
def regular_bullish_div1 = fractal_bot1 and low[2] < low_price1 and macd_2[2] > low_prev1 and div_reg_Bull;
def hidden_bullish_div1 = fractal_bot1 and low[2] > low_price1 and macd_2[2] < low_prev1 and div_hid_Bull;
def col1 = if regular_bearish_div1 then 1 else if hidden_bearish_div1 then 1 else 0;
def col2 = if regular_bullish_div1 then 1 else if hidden_bullish_div1 then 1 else 0;
def beardivLine = if fractal_top1 then macd_2[2] else beardivLine[1];
def bulldivLine = if fractal_bot1 then macd_2[2] else bulldivLine[1];
def bear_divLine = if col1 then beardivLine else bear_divLine[1];
def bull_divLine = if col2 then bulldivLine else bull_divLine[1];
plot bearLine = if !ViewDivergenceChannel then na else bear_divLine[-2];
plot bullLine = if !ViewDivergenceChannel then na else bull_divLine[-2];
bearLine.AssignValueColor(Color.DARK_RED);
bullLine.AssignValueColor(Color.DARK_GREEN);
def regBear = regular_bearish_div1 and div_reg_Bear and ViewDivergenceLabels;
def hidBear = hidden_bearish_div1 and div_hid_Bear and ViewDivergenceLabels;
def regBull = regular_bullish_div1 and div_reg_Bull and ViewDivergenceLabels;
def hidBull = hidden_bullish_div1 and div_hid_Bull and ViewDivergenceLabels;
AddChartBubble(regBear[-2], macd_2[1] + ploff*0.5, "🐻 R", Color.RED);
AddChartBubble(hidBear[-2], macd_2[1] + ploff*0.5, "🐻 H", Color.DARK_RED);
AddChartBubble(regBull[-2], macd_2[1] - ploff*0.5, "🐂 R", Color.GREEN, no);
AddChartBubble(hidBull[-2], macd_2[1] - ploff*0.5, "🐂 H", Color.DARK_GREEN, no);
#//Estrategia
def largo = (macd_2 Crosses Above 0) and macd_1 < macd_2 and macd_1[1] < macd_1[0] and macd_1 < 0 and ViewTrendEntrySignal;
def largo1 = (macd_2 Crosses Above macd_1) and macd_2 < 0 and ViewTrendReversalSignal;
plot LargoDe = if largo then macd_2 - ploff*0.75 else na; # "Largo Entrada de Tendencia"
plot LargoContra = if largo1 then macd_2 - ploff*0.75 else na; # "Largo Entrada Contra Tendencia"
LargoDe.SetDefaultColor(Color.GREEN);
LargoContra.SetDefaultColor(Color.GREEN);
LargoDe.SetPaintingStrategy(PaintingStrategy.SQUARES);
LargoContra.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
def corto = (macd_2 Crosses below 0) and macd_1 > macd_2 and macd_1[1] > macd_1[0] and macd_1 > 0 and ViewTrendEntrySignal;
def corto1 = (macd_2 Crosses Below macd_1) and macd_2 > 0 and ViewTrendReversalSignal;
plot CortoDe = if corto then macd_2 + ploff*0.75 else na; # "Corto Entrada de Tendencia"
plot CortoContra = if corto1 then macd_2 + ploff*0.75 else na; # "Corto Entrada Contra Tendencia"
cortoDe.SetDefaultColor(Color.RED);
cortoContra.SetDefaultColor(Color.RED);
CortoDe.SetPaintingStrategy(PaintingStrategy.SQUARES);
CortoContra.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot hline = if isNaN(close) then na else 0;
hline.SetDefaultColor(Color.DARK_GRAY);
#-- END of CODE