#https://www.tradingview.com/script/s2yv2eub-Mobo-Bands/
declare lower;
def price = hl2;
def colorNormLength = 3;
def dpoLength = 13;
def moboDisplace = 0;
def moboLength = 10;
input aggregationPeriod = AggregationPeriod.DAY;
def numDevDn = -0.8;
def numDevUp = 0.8;
script nz {
input data = 0;
def ret_val = if IsNaN(data) then 0 else data;
plot return = ret_val;
}
def coloredMobo = yes;
def coloredFill = yes;
def breakArrows = yes;
def moboShowMid = yes;
#def DPO = price - Average(price[DPO_length / 2 + 1], DPO_length);
def xsma = Average(price[dpoLength / 2 + 1], dpoLength);
#def xsma = sma(price, dpoLength);
def DPO = price - xsma;
def Midline = Average(DPO, moboLength);
def sDev = StDev(DPO, moboLength);
def LowerBand = Midline + numDevDn * sDev;
def UpperBand = Midline + numDevUp * sDev;
plot line_dpo = DPO;
line_dpo.AssignValueColor(if line_dpo> line_dpo[1] then Color.cyan else Color.red);
line_dpo.SetLineWeight(5);
plot Line_mid = Midline;
Line_mid.AssignValueColor(if MidLine < dpo then Color.green else Color.magenta);
Line_mid.SetLineWeight(3);
def nan = Double.NaN;
input fill = yes;
plot Upper = UpperBand;
Upper.SetDefaultColor(GetColor(7));
plot Lower = LowerBand;
lower.SetDefaultColor(GetColor(7));
plot line_0 = 0;
line_0.SetDefaultColor(GetColor(9));
def Signal1 = DPO > UpperBand and DPO[1] < UpperBand[1];
def Signal2 = DPO < LowerBand and DPO[1] > LowerBand[1];
def wasUp = if Signal1 then 1 else if Signal2 then 0 else nz(wasUp[1]);
def wasDn = if Signal2 then 1 else if Signal1 then 0 else nz(wasDn[1]);
plot plotshape1 = if Signal1 and wasDn[1] then UpperBand else Double.NaN;
plotshape1.SetDefaultColor(GetColor(1));
plot plotshape2 = if Signal2 and wasUp[1] then LowerBand else Double.NaN;
plotshape2.SetDefaultColor(GetColor(1));
def a = (UpperBand > UpperBand[1] and Midline > Midline[1]);
def b = (UpperBand < UpperBand[1] and LowerBand < LowerBand[1]);
def c = (UpperBand > UpperBand[1] and Midline > Midline[1]);
def d = (UpperBand < UpperBand[1] and midline < midline[1]);
def e = average(close)>midline;
def f = midline<average(close);
def Chg = If(a, 1, If(b, -1, 0));
def Hold = CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def Chg_a = If((c or e), 1, If((d or f), -1, 0));
def Hold_a = CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def LBUp = if fill and Hold[0] == 1 then lowerBand else nan;
def UBUp = if fill and Hold[0] == 1 then upperband else nan;
def LBDn = if fill and Hold[0] == -1 then lowerband else nan;
def UBDn = if fill and Hold[0] == -1 then upperband else nan;
def MBUP = if fill and Hold[0] == 1 then midline else nan;
def MBDn = if fill and Hold[0] == -1 then Midline else nan;
DefineGlobalColor("Cloud Up", Color.red);
DefineGlobalColor("Cloud_Up", Color.downtick);
DefineGlobalColor("Cloud_Dn", Color.light_green);
DefineGlobalColor("Cloud Dn", Color.cyan);
AddCloud(LBUp, UBUp, GlobalColor("Cloud Up"), GlobalColor("Cloud Dn"));
AddCloud(LBDn, UBDn, GlobalColor("Cloud Dn"), GlobalColor("Cloud Up"));
AddCloud(MBUp, UBUp, GlobalColor("Cloud_Up"), GlobalColor("Cloud_Dn"));
AddCloud(MBDn, UBDn, GlobalColor("Cloud_Dn"), GlobalColor("Cloud_Up"));