petergluis
Active member
ThinkorSwim provides FW_DPO_MoBo with the code. Here is my code for ThinkorSwim.
Ruby:
#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);
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));
AddCloud(Upper, Lower, Color.blue);