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: