Trend Filter (2-pole) [BigBeluga] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
09M77oT.png


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
 

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
377 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