![09M77oT.png](/proxy.php?image=https%3A%2F%2Fi.imgur.com%2F09M77oT.png&hash=b3076dae03189bd4034ab00d84d40b8d)
Author Message:
The Trend Filter (2-pole) is an advanced trend-following indicator based on a two-pole filter, which smooths out market noise while effectively highlighting trends and their strength. It incorporates color gradients and support/resistance dots to enhance trend visualization and decision-making for traders.
MOre Details: https://www.tradingview.com/v/lOm2gFyk/
CODE:
CSS:
#// Indicator for TOS
#// © BigBeluga
#indicator("Trend Filter (2-pole) [BigBeluga]", overlay = true)
# Converted by Sam4Cok@Samer800 - 01/2025
input timeframe = AggregationPeriod.MIN;
input source = FundamentalType.CLOSE;
input length = 20; # "Length"
input damping = 0.9; # "Damping", minval = 0.1, maxval = 1.0, step = 0.01)
input RisingAndFallingThreshold = 5; #, "Rising and Falling")
input colorBars = no; # "BarColor", inline = "Features")
input showSignals = no; # "Signals",
def na = Double.NaN;
def last = isNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def src = Fundamental(source, Period = tf);
#/ CALCULATIONS
def atr = atr(Length = 200);
##// ~~ Gradient Coloring {
Script gradient_color {
input src = 0;
input minVal = 10;
input maxVal = 400;
input loR = 173;
input loG = 216;
input loB = 230;
input hiR = 41;
input hiG = 98;
input hiB = 255;
def value = if isNaN(src) then 0 else src;
def clamped_value = max(min(value, maxVal), minVal);
def normalized_value = (clamped_value - minVal) / (maxVal - minVal);
def re = floor(loR + (hiR - loR) * normalized_value);
def gr = floor(loG + (hiG - loG) * normalized_value);
def bl = floor(loB + (hiB - loB) * normalized_value);
plot r = re;
plot g = gr;
plot b = bl;
}
#//@function Two-pole filter
Script two_pole_filter {
input src = close;
input length = 20;
input factor = 0.9;
def pi = Double.Pi;
def damping = Min(Max(factor, 0.1), 1);
def omega = 2.0 * pi / length;
def alpha = damping * omega;
def beta = Sqr(omega);
def f1 = if isNaN(f1[1]) then src else
if !f1[1] then src else f1[1] + alpha * (src - f1[1]);
def f2 = if isNaN(f2[1]) then src else
if !f2[1] then src else f2[1] + beta * (f1 - f2[1]);
plot out = f2;
}
def tp_f = two_pole_filter(src, length, damping);
def rising;
def falling;
def up = tp_f > tp_f[2];
def dn = tp_f < tp_f[2];
if up {
rising = rising[1] + 1;
falling = 0;
} else if dn {
rising = 0;
falling = falling[1] + 1;
} else {
rising = rising[1];
falling = falling[1];
}
def rUp = gradient_color(rising, 0, 15, 255, 235, 59, 0, 255, 0).r;
def gUp = gradient_color(rising, 0, 15, 255, 235, 59, 0, 255, 0).g;
def bUp = gradient_color(rising, 0, 15, 255, 235, 59, 0, 255, 0).b;
def rDn = gradient_color(falling, 0, 15, 255, 235, 59, 255, 0, 0).r;
def gDn = gradient_color(falling, 0, 15, 255, 235, 59, 255, 0, 0).g;
def bDn = gradient_color(falling, 0, 15, 255, 235, 59, 255, 0, 0).b;
plot ploeFilter = if !last then tp_f else na;
plot fall = if !last and falling >= RisingAndFallingThreshold then tp_f + atr else na; # "Falling"
plot rise = if !last and rising >= RisingAndFallingThreshold then tp_f - atr else na; # "Rising"
ploeFilter.SetLineWeight(2);
fall.SetStyle(Curve.POINTS);
rise.SetStyle(Curve.POINTS);
ploeFilter.AssignValueColor(if up then CreateColor(rUp, gUp, bUp) else
if dn then CreateColor(rDn, gDn, bDn) else CreateColor(255, 235, 59));
fall.AssignValueColor(CreateColor(rDn, gDn, bDn));
rise.AssignValueColor(CreateColor(rUp, gUp, bUp));
# Signals
def sig_up = (rising > RisingAndFallingThreshold) and (rising[1] <= RisingAndFallingThreshold) and !last[-1] and showSignals;
def sig_dn = (falling > RisingAndFallingThreshold) and (falling[1] <= RisingAndFallingThreshold) and !last[-1] and showSignals;
plot arrUp = if !sig_up and sig_up[-1] then low else na;
plot arrDn = if !sig_dn and sig_dn[-1] then high else na;
arrUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
arrDn.AssignValueColor(CreateColor(rDn, gDn, bDn));
arrUp.AssignValueColor(CreateColor(rUp, gUp, bUp));
# bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
if up then CreateColor(rUp, gUp, bUp) else
if dn then CreateColor(rDn, gDn, bDn) else CreateColor(255, 235, 59));
#-- END of CODE