Gaussian SWMA For Loop [By Coff3eG] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
mtZhxsp.png


Author Message:

he "Gaussian SWMA For Loop" is a sophisticated indicator designed to identify potential trading opportunities by combining a Gaussian-weighted moving average (WMA) with a simple moving average (SMA), enhanced by a loop-based scoring system. This indicator is tailored for traders looking to capture trends and reversals with a refined approach, making use of advanced filtering techniques and custom thresholds for signal generation.

CODE:

CSS:
# Indicator for TOS
#// © Coff3eG
#indicator("Gaussian SWMA For Loop", overlay = true)
# Converted by Sam4Cok@Samer800    - 11/2024
input colorBars = yes;
input showLabel = yes;
input timeframe = AggregationPeriod.MIN;
input Source    = FundamentalType.CLOSE; #, title = "Source", group = "Main")
input GaussianLength  = 4; #, title = "WMA Length", minval = 1, group = "Main")
input GaussianSigma  = 2.0; #, title = "Gaussian Sigma", minval = 0.1, step = 0.1,
input smoothingLength = 2; #, title = "SMA Length", minval = 1, group = "Main")
input loopFrom = 1; #, title = "From", group = "For Loop", minval = 1)
input loopTo   = 50; #, title = "To", group = "For Loop", maxval = 50)
input LongThreshold = 40; #, title = "Long Threshold", group = "Threshold")
input ShortThreshold = 0; #, title = "Short Threshold", group = "Threshold")


def na = Double.NaN;
def last = IsNaN(close);
def current = GetAggregationPeriod();
def tf = Max(current, timeframe);
def a = Max(loopFrom, 1);
def b = Max(loopTo, 1);
 
DefineGlobalColor("up", CreateColor(0, 255, 187));
DefineGlobalColor("dn", CreateColor(255, 0, 157));
#// Gaussian Filter Function
script gaussian_filter {
    input src = close;
    input length = 4;
    input sigma = 2;
    def gaussian_sum = fold i = 0 to length with p do
              p + (Exp(-0.5 * Power((i - (length - 1) / 2) / sigma, 2)));
    def gaussian_weighted_sum = fold j = 0 to length with q do
        q + src[j] * (Exp(-0.5 * Power((j - (length - 1) / 2) / sigma, 2)));
    def gaussian_filter = gaussian_weighted_sum / gaussian_sum;
    plot out = gaussian_filter;
}
#// Apply Gaussian Filter
def gaussian_smooth = gaussian_filter(Fundamental(Source, PEriod = tf), GaussianLength, GaussianSigma);
#// Calc SMA on Gaussian Filtered Data
def filtered_sma = Average(gaussian_smooth, smoothingLength);
def score = if a == b then (if filtered_sma > filtered_sma[a] then 1 else -1) else
            fold i  = a to b with p do
            p + (if filtered_sma > filtered_sma[i] then 1 else -1);
def L = score > LongThreshold;
def S = score < ShortThreshold;
def Coff = if L and !S then 1 else if S then -1 else 0;

plot a_plot = filtered_sma;
plot b_plot = if last then na else filtered_sma[1];
a_plot.AssignValueColor(if Coff>0 then GlobalColor("up") else
                        if Coff<0 then GlobalColor("dn") else Color.GRAY);
b_plot.AssignValueColor(if Coff>0 then GlobalColor("up") else
                        if Coff<0 then GlobalColor("dn") else Color.GRAY);

Addcloud(if (Coff>0 or Coff[-1]>0) then a_plot else na, b_plot, GlobalColor("up"), GlobalColor("up"));
Addcloud(if (Coff<0 or Coff[-1]<0) then a_plot else na, b_plot, GlobalColor("dn"), GlobalColor("dn"));
Addcloud(if (!Coff or !Coff[-1]) then a_plot else na, b_plot, Color.GRAY, Color.GRAY);

#-- Label

AddLabel(showLabel,"Score("+ score + ")", if Coff>0 then GlobalColor("up") else
                           if Coff<0 then GlobalColor("dn") else Color.GRAY);
#-- Bar Color

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if Coff>0 then GlobalColor("up") else
                 if Coff<0 then GlobalColor("dn") else Color.GRAY);

#-- 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
244 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