Deviation Scaled Moving Average w/ DSL For ThinkOrSwim

Zlotko

Active member
VIP
The author states:
Deviation Scaled Moving Average w/ DSL [Loxx] as described in the “The Deviation-Scaled Moving Average.” article of July 2018 TASC . This is an adaptive moving average average that has the ability to rapidly adapt to volatility in price movement. This version adds Discontinued Signal Lines create the buy/sell signals.

What are DSL Discontinued Signal Line?

A lot of indicators are using signal lines in order to determine the trend (or some desired state of the indicator) easier. The idea of the signal line is easy : comparing the value to it's smoothed (slightly lagging) state, the idea of current momentum/state is made.

Discontinued signal line is inheriting that simple signal line idea and it is extending it : instead of having one signal line, more lines depending on the current value of the indicator.

5AIePqu.png


Original Tradingview code
https://www.tradingview.com/script/MXTaXJdU-Deviation-Scaled-Moving-Average-w-DSL-Loxx/

For the new ThinkOrSwim code, you must scroll down to the next post
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Looking for DSL Oscillator like Deviation Scaled Moving Average w/ DSL
https://www.tradingview.com/script/MXTaXJdU-Deviation-Scaled-Moving-Average-w-DSL-Loxx/
check the below:

CSS:
#// 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
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
390 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top