Price Filter [AstrideUnicorn] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
XTWhryJ.png

Author Message:
The indicator calculates a fast price filter based on the closing price of the underlying asset. Overall, it is intended to provide a fast, reliable way to detect trend direction and confirm trend strength, using statistical measures of price movements.
The algorithm was adapted from Marcus Schmidberger's (2018) article "High Frequency Trading with the MSCI World ETF". It demeans the price time series using the long-term average and then normalizes it with the long-term standard deviation. The resulting time series is then compared to specified thresholds to determine the trend direction.

HOW TO USE
The indicator surface is colored green if the price is trending upwards and red if the price is trending downwards. If the indicator outline is the opposite color of the indicator surface, it indicates that the price is moving against the trend and the current trend may be losing strength.
If the 'Use threshold' setting is enabled, the indicator will be colored blue if its value is within the range defined by the upper and lower thresholds. This indicates that the price is trending sideways, or that the current trend is losing strength.


SETTNGS

Length
- the length of the long-term average used to calculate the price filter. Recommended range 20 - 200. The sensitivity of the indicator increases as the value becomes smaller, allowing it to detect smaller price moves and swings earlier.

Threshold- the threshold value used to detect trend direction.

Use threshold - a boolean (true/false) input that determines whether to use the threshold value for confirmation.

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © AstrideUnicorn
#indicator("Price Filter [AstrideUnicorn]")
#// Adapted from "High-Frequency Trading with MSCI World ETF" by Markus Schmidberger (2018)
# Converted by Sam4Cok@Samer800 - 01/2023
declare lower;
#// Define inputs
input src = close;
input length = 200;        # "Length for long-term average"
input MovAvgType = AverageType.SiMPLE;
input threshold = 0.5;     # "Treshold value to detect trend direction"
input use_treshold = no;   # "Use treshold"
input Smoothing = yes;
input SmoothingType = AverageType.EXPONENTIAL;
input SmoothingLength = 3;

def na = Double.NaN;
#// Calculate price filter
def pf1 = (close - MovingAverage(MovAvgType, src, length)) / stdev(src, length);
def pf = if Smoothing then MovingAverage(SmoothingType, pf1, SmoothingLength) else pf1;
def pf_color = pf > pf[1];

#// Initialize plot
plot f_zero = if isNaN(close) then na else 0;
f_zero.SetDefaultColor(Color.GRAY);
plot f_filter = if isNaN(close) then na else pf;
f_filter.AssignValueColor(if pf_color then Color.GREEN else Color.RED);
f_filter.SetLineWeight(2);

plot thUp = if isNaN(close) then na else threshold;
thUp.SetStyle(Curve.SHORT_DASH);
thUp.SetDefaultColor(Color.GRAY);
plot thDn = if isNaN(close) then na else -threshold;
thDn.SetStyle(Curve.SHORT_DASH);
thDn.SetDefaultColor(Color.GRAY);

def color_fill;# = pf > 0;
if use_treshold {
    color_fill = if pf > threshold then 1 else if pf < -threshold then -1 else 0;
    } else {
    color_fill = if pf > 0 then 1 else -1;
}
plot Hist_filter = if isNaN(close) or !use_treshold then na else pf;
Hist_filter.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Hist_filter.AssignValueColor(if color_fill > 0 then Color.GREEN else
                             if color_fill < 0 then Color.RED else color.WHITE);

AddCloud(if color_fill > 0 then f_filter else na, f_zero, Color.GREEN);
AddCloud(if color_fill < 0 then f_zero else na, f_filter, Color.RED);


#--- END CODE
 
Last edited by a moderator:

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