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