Regularized-Moving-Average Oscillator Suite for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
Fe7xXsf.png

Author Message:
The Regularized MA Oscillator Suite calculates the moving average (MA) based on user-defined parameters such as length, moving average type, and custom smoothing factors. It then derives the mean and standard deviation of the MA using a normalized period. Finally, it computes the Z-Score by subtracting the mean from the MA and dividing it by the standard deviation.

CODE:

CSS:
#//https://www.tradingview.com/v/LAFDETZX/
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International
#// © EliCobra
#indicator("Regularized-MA Oscillator Suite", "{?} - MA Osc. Suite", false)
# Converted by Sam4Cok@Samer800    - 12/2023
declare lower;

input matype = {"SMA", default "EMA", "DEMA", "TEMA", "HMA", "EHMA", "THMA", "RMA", "WMA", "VWMA", "T3", "KAMA", "ALMA", "LSMA"};
input Source = close;           # "Source"
input Length = 14;              # "Length"
input KaufmanFast = 0.666;      # "Kaufman Fast"
input KaufmanSlow = 0.0645;     # "Kaufman Slow"
input almaOffset = 0.85;        # "ALMA Offset"
input almaSigma = 6;            # "ALMA Sigma"
input RegularizeLength = 30;    # "Regularize Length"
input ReversionThreshold = {"1", "2", default "3"};    # "Reversion Threshold"
input ShowReversalSignals = yes; # "Show Reversal Signals"
input BarColoring = {default "None", "Trend", "Extremities", "Reversions", "Slope"}; # "Bar Coloring"

def na = Double.NaN;
def last = isNaN(Close);

DefineGlobalColor("up", CreateColor(0,179,80));
DefineGlobalColor("dn", CreateColor(187,0,16));
def revt;
Switch (ReversionThreshold) {
Case "3" : revt = 3;
Case "2" : revt = 2;
Default  : revt = 1;
}
#f_kama(src, len, kamaf, kamas) =>
script f_kama {
    input src = close;
    input len = 14;
    input kamaf = 0.666;
    input kamas = 0.0645;
    def white = AbsValue(src - src[1]);
    def nsignal = AbsValue(src - src[len]);
    def nnoise = Sum(white, len);
    def nefratio = if nnoise != 0 then nsignal / nnoise else 0;
    def nsmooth = Power(nefratio * (kamaf - kamas) + kamas, 2) ;
    def ama = CompoundValue(1, ama[1] + nsmooth * (src - ama[1]), 0);
    plot out = ama;
}
#f_t3(src, len) =>
script f_t3 {
    input src = close;
    input len = 14;
    def x1 = ExpAverage(src, len);
    def x2 = ExpAverage(x1, len);
    def x3 = ExpAverage(x2, len);
    def x4 = ExpAverage(x3, len);
    def x5 = ExpAverage(x4, len);
    def x6 = ExpAverage(x5, len);
    def b = 0.7;
    def c1 = - Power(b, 3);
    def c2 = 3 * Power(b, 2) + 3 * Power(b, 3);
    def c3 = -6 * Power(b, 2) - 3 * b - 3 * Power(b, 3);
    def c4 = 1 + 3 * b + Power(b, 3) + 3 * Power(b, 2);
    def f_t3 = c1 * x6 + c2 * x5 + c3 * x4 + c4 * x3;
    plot out = f_t3;
}
#f_ehma(src, length) => 
script f_ehma {
    input src = close;
    input length = 14;
    def ema1 = ExpAverage(src, length);
    def ema2 = 2 * ExpAverage(src, length / 2);
    def SqrtLen = Round(Sqrt(length), 0);
    def f_ehma = ExpAverage(ema2 - ema1, SqrtLen);
    plot out = f_ehma;
}
#f_thma(src, length) => 
script f_thma {
    input src = close;
    input length = 14;
    def wma1 = WMA(src, length);
    def wma2 = WMA(src, length / 2);
    def wma3 = 3 * WMA(src, length / 3);
    def f_thma = WMA(wma3 - wma2 - wma1, length);
    plot out = f_thma;
}
script f_alma {
    input series = close;
    input windowsize = 9;
    input offset = 0.85;
    input sigma = 6;
    def m = offset * (windowsize - 1);
    def s = windowsize / sigma;
    def norm = fold i = 0 to windowsize with p do
               p + Exp(-1 * Sqr(i - m) / (2 * Sqr(s)));
    def sum = fold j = 0 to windowsize with q do
              q + GetValue(series, windowsize - j - 1) * Exp(-1 * Sqr(j - m) / (2 * Sqr(s)));
    def f_alma = sum / norm;
    plot out = f_alma;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 14;
    input vol = volume;
    def nom = Average(src * vol, len);
    def den = Average(vol, len);
    def VWMA = nom / den;
    plot result = VWMA;
}
#f_ma(src, len, type, kamaf, kamas, offset, sigma) =>
script f_ma {
    input src = close;
    input len = 14;
    input type = "EMA";
    input kamaf = 0.666;
    input kamas = 0.0645;
    input offset = 0.85;
    input sigma = 6;
    def x =
        if type == "SMA" then Average(src, len) else
        if type == "EMA" then ExpAverage(src, len) else
        if type == "HMA" then HullMovingAvg(src, len) else
        if type == "RMA" then WildersAverage(src, len) else
        if type == "WMA" then WMA(src, len) else
        if type == "VWMA" then vwma(src, len) else
        if type == "ALMA" then f_alma(src, len, offset, sigma) else
        if type == "DEMA" then DEMA(src, len) else
        if type == "TEMA" then TEMA(src, len) else
        if type == "EHMA" then f_ehma(src, len) else
        if type == "THMA" then f_thma(src, len) else
        if type == "T3" then f_t3(src, len) else
        if type == "KAMA" then f_kama(src, len, kamaf, kamas) else
        if type == "LSMA" then Inertia(src, len) else ExpAverage(src, len);
    def f_ma = x;
    plot out = f_ma;
}
def ma = f_ma(Source, Length, matype, KaufmanFast, KaufmanSlow, almaOffset, almaSigma);
def mean = Average(ma, RegularizeLength);
def dev = stdev(ma, RegularizeLength);
def zmean = (ma - mean) / dev;

def max = if last then na else 4;
def hh  = if last then na else 3;
def lh  = if last then na else 2;
plot mid = if last then na else 0;#, "Mid Line"
def min = if last then na else -4;
def ll  = if last then na else -3;
def hl  = if last then na else -2;
mid.SetStyle(Curve.SHORT_DASH);
mid.SetDefaultColor(Color.GRAY);

AddCloud(max, hh, CreateColor(88, 4, 48));
AddCloud(max, lh, CreateColor(88, 4, 48));
AddCloud(ll, min, CreateColor(2, 75, 48));
AddCloud(hl, min, CreateColor(2, 75, 48));

plot z = zmean; # "Z"
z.AssignValueColor(if zmean > 0 then GlobalColor("up") else GlobalColor("dn"));
z.SetLineWeight(2);

def zh = z / 2;
AddCloud(z, zh, Color.DARK_GREEN, Color.DARK_RED);

def col;
Switch (BarColoring) {
Case "Trend" : col = if zmean > 0 then 1 else -1;#? #00b350 : #bb0010
Case "Extremities" :
        col = if zmean > 2 then 1 else
              if zmean < -2 then -1 else 0;
Case "Reversions":
        col = if (zmean > revt and zmean < zmean[1]) and !(zmean[1] < zmean[2]) then -1 else
              if (zmean < -revt and zmean > zmean[1]) and !(zmean[1] > zmean[2]) then 1 else 0;
Case "Slope" :
        col  = if zmean > zmean[1] then 1 else -1;
Default : col = na;
}
def color = col;
AssignPriceColor(if isNaN(color) then Color.CURRENT else
                 if color > 0 then GlobalColor("up") else
                 if color < 0 then  GlobalColor("dn") else Color.GRAY);

def os = ShowReversalSignals and zmean < -revt and zmean > zmean[1] and !(zmean[1] > zmean[2]);
def ob = ShowReversalSignals and zmean >  revt and zmean < zmean[1] and !(zmean[1] < zmean[2]);

plot OverSold = if os then z - 0.5 else na;
plot OverBought = if ob then z + 0.5 else na;
OverSold.SetPaintingStrategy(PaintingStrategy.POINTS);
OverBought.SetPaintingStrategy(PaintingStrategy.POINTS);
OverSold.SetDefaultColor(Color.GREEN);
OverBought.SetDefaultColor(Color.RED);
OverSold.SetLineWeight(2);
OverBought.SetLineWeight(2);

#-- END OF CODE
 

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

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