#// Indicator for TOS
#// © loxx
#indicator("Deviation Scaled Moving Average w/ DSL [Loxx]", shorttitle='DSMAWDSL [Loxx]',
# Converted by Sam4Cok@Samer800 - 11/2024
input timeframe = AggregationPeriod.MIN;
input BetterSmoothingType = {"AMA", "T3", "Kaufman",default "No Smoothing"}; # "Heiken-Ashi Better Smoothing"
input useBetterHeikenAshiSource = {"Yes", Default "No"};
input Source = FundamentalType.CLOSE; # "Source"
input Period = 25; # "Period"
input SignalPeriod = 9; # "Signal Period"
input SignalSmoothingType = {default "EMA", "FEMA", "HULL"}; # "Signal/DSL Smoothing"
input SignalType = {"Slope", default "Levels Crosses"}; # "Signal type"
input colorBars = yes; # "Color bars?"
input showSignals = yes; # "Show signals?"
input KaufmanFastEnd = 0.666; # "* Kaufman's Adaptive MA (KAMA) Only - Fast End"
input KaufmanSlowEnd = 0.0645; # "* Kaufman's Adaptive MA (KAMA) Only - Slow End"
input amaFastLength = 2; # "* Adaptive Moving Average (AMA) Only - Fast"
input amaSlowLength = 30; # "* Adaptive Moving Average (AMA) Only - Slow"
def na = Double.NaN;
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def habingest = (open(Period = tf) + close(Period = tf))/2 + (((close(Period = tf) - open(Period = tf))/(high(Period = tf) - low(Period = tf))) * AbsValue((close(Period = tf) - open(Period = tf))/2));
def srcoption;
Switch (useBetterHeikenAshiSource) {
Case "Yes" : srcoption = if (isNaN(habingest) or !habingest) then Fundamental(Source, Period = tf) else habingest;;
Default : srcoption = Fundamental(Source, Period = tf);
}
script _ama {
input src = close;
input len = 2;
input fl = 2;
input sl = 30;
def flout = 2 / (fl + 1);
def slout = 2 / (sl + 1);
def hh = Highest(len + 1);
def ll = Lowest(len + 1);
def mltp = if hh - ll != 0 then AbsValue(2 * src - ll - hh) / (hh - ll) else 0;
def ssc = mltp * (flout - slout) + slout;
def ama = ama[1] + Sqr(ssc) * (src - ama[1]);
plot out = ama;
}
script _t3 {
input src = close;
input len = 3;
def xe1_1 = ExpAverage(src, len);
def xe2_1 = ExpAverage(xe1_1, len);
def xe3_1 = ExpAverage(xe2_1, len);
def xe4_1 = ExpAverage(xe3_1, len);
def xe5_1 = ExpAverage(xe4_1, len);
def xe6_1 = ExpAverage(xe5_1, len);
def b_1 = 0.7;
def c1_1 = -b_1 * b_1 * b_1;
def c2_1 = 3 * b_1 * b_1 + 3 * b_1 * b_1 * b_1;
def c3_1 = -6 * b_1 * b_1 - 3 * b_1 - 3 * b_1 * b_1 * b_1;
def c4_1 = 1 + 3 * b_1 + b_1 * b_1 * b_1 + 3 * b_1 * b_1;
def nT3Average = c1_1 * xe6_1 + c2_1 * xe5_1 + c3_1 * xe4_1 + c4_1 * xe3_1;
plot out = nT3Average;
}
script _kama {
input src = close;
input len = 2;
input kama_fastend = 0.666;
input kama_slowend = 0.0645;
def xvnoise = AbsValue(src - src[1]);
def nfastend = kama_fastend;
def nslowend = kama_slowend;
def nsignal = AbsValue(src - src[len]);
def nnoise = Sum(xvnoise, len);
def nefratio = if nnoise != 0 then nsignal / nnoise else 0;
def nsmooth = Power(nefratio * (nfastend - nslowend) + nslowend, 2);
def nAMA = nAMA[1] + nsmooth * (src - nAMA[1]);
plot out = nAMA;
}
script fema {
input src = close;
input len = 9;
def ema = ExpAverage(src, len);
def alpha = (2.0 / (2.0 + (len - 1.0) / 2.0));
def fema = CompoundValue(1, fema[1] + alpha * (src - fema[1]), ema);
plot out = fema;
}
Script super {
input src = close;
input len = 25;
def pi = Double.Pi;
def f = (Sqrt(2) * pi) / len;
def a = exp(-f);
def coeff2 = 2 * a * cos(f);
def coeff3 = - Sqr(a);
def coeff1 = 1 - coeff2 - coeff3;
def addSrc = src + src[1];
def smooth = CompoundValue(1, coeff1 * addSrc * 0.5 + coeff2 * smooth[1] + coeff3 * smooth[2],
coeff1 * addSrc * 0.5 + coeff2 * close[1] + coeff3 * close[2]);
plot out = if !IsNaN(src) then smooth else Double.NaN;;
}
def src;
switch (BetterSmoothingType) {
case "AMA" :
src = _ama(srcoption, 2, amaFastLength, amaSlowLength);
case "T3" :
src = _t3(srcoption, 3);
Case"Kaufman" :
src = _kama(srcoption, 2, KaufmanFastEnd, KaufmanSlowEnd);
Default :
src = srcoption;
}
def filt = super((close(Period = tf) - close(Period = tf)[2]), Period);
def sQrms = Sum(Sqr(filt), Period);
def rms = Sqrt(sQrms / Period);
def scaledfit = filt / rms;
def alpha = AbsValue(scaledfit) * 5 / Period;
def dsEMA;
def dsEMA1 = if dsEMA[1] then dsEMA[1] else ExpAverage(src, Period);
dsEMA = dsEMA1 + alpha * (src - dsEMA1);
def sig = if dsEMA then dsEMA1 else sig[1];
def levelu; def leveld;
switch (SignalSmoothingType) {
case "FEMA" :
levelu = if (DSEMA > sig) then fema(Max(dsEMA, sig), SignalPeriod) else levelu[1];
leveld = if (DSEMA < sig) then fema(Min(dsEMA, sig) , SignalPeriod) else leveld[1];
case "HULL" :
levelu = if (DSEMA > sig) then HullMovingAvg(Max(dsEMA, sig), SignalPeriod) else levelu[1];
leveld = if (DSEMA < sig) then HullMovingAvg(Min(dsEMA, sig), SignalPeriod) else leveld[1];
default :
levelu = if (dsEMA > sig) then ExpAverage(Max(dsEMA, sig), SignalPeriod) else levelu[1];
leveld = if (dsEMA < sig) then ExpAverage(Min(dsEMA, sig), SignalPeriod) else leveld[1];
}
def state;
def goLong_pre; def goShort_pre;
Switch (SignalType) {
Case "Slope" :
state = if (dsEMA < sig) then -1 else if (dsEMA > sig) then 1 else 0;
goLong_pre = (dsEMA > sig) and (dsEMA[1] <= sig[1]);
goShort_pre = (dsEMA < sig) and (dsEMA[1] >= sig[1]);
Default :
state = if (dsEMA < leveld) then -1 else if (dsEMA > levelu) then 1 else 0;
goLong_pre = (dsEMA > levelu) and (dsEMA[1] <= levelu[1]);
goShort_pre = (dsEMA < leveld) and (dsEMA[1] >= leveld[1]);
}
def contSwitch = if goLong_pre then 1 else if goShort_pre then -1 else contSwitch[1];
def goLong = goLong_pre and (contSwitch - contSwitch[1]);
def goShort = goShort_pre and (contSwitch - contSwitch[1]);
def colorout;
Switch (SignalType) {
Case "Slope" :
colorout = if contSwitch == -1 then -1 else 1;
Default :
colorout = if state == 1 then 1 else if state == -1 then -1 else 0;
}
#-- Plots
plot Stepped = if dsEMA then dsEMA else na; # "Pips-Stepped, Adaptive-ER DSEMA"
plot LevelUp = if levelu then levelu else na; # "Level Up"
plot LevelDn = if leveld then leveld else na; # "Level Down"
Stepped.SetLineWeight(2);
Stepped.AssignValueColor(if colorout>0 then Color.CYAN else
if colorout<0 then Color.MAGENTA else Color.GRAY);
LevelUp.SetDefaultColor(Color.DARK_GREEN);
LevelDn.SetDefaultColor(Color.DARK_RED);
#-- Signals
plot SigUp = if showSignals and goLong then low else na;
plot SigDn = if showSignals and goShort then high else na;
SigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SigUp.SetDefaultColor(Color.CYAN);
SigDn.SetDefaultColor(Color.MAGENTA);
#-- Bar Color
AssignPriceColor(if !colorbars then Color.CURRENT else
if colorout>0 then Color.GREEN else
if colorout<0 then Color.RED else Color.GRAY);
#-- END of CODE