OPEN-SOURCE SCRIPT

TASC 2024.09 Precision Trend Analysis

█ OVERVIEW

This script introduces an approach for detecting and confirming trends in price series based on digital signal processing principles, as presented by John Ehlers in the "Precision Trend Analysis" article from the September 2024 edition of TASC's Traders' Tips.



█ CONCEPTS

Traditional trend-following indicators, such as moving averages, are lowpass filters that pass low-frequency components in a series and remove high-frequency components. Because lowpass filters preserve lengthy cycles in the data while attenuating shorter cycles, such filters have unavoidable lag that impacts the timeliness of trading signals.

In his article, John Ehlers presents an alternative approach that combines two highpass filters with different lengths to remove undesired high-frequency content via cancellation. Highpass filters have nearly zero lag. As such, the resulting trend indicator from this approach is very responsive to changes in the price series, with peaks and valleys that closely align with those of the price data. The indicator signifies an uptrend when its value is positive (i.e., above the balance point) and a downtrend when it is negative.

Subsequently, John Ehlers demonstrates that one can use the trend indicator's rate of change (ROC) to determine the onset of new trend movements. The ROC is zero at peaks and valleys in the trend indicator. Therefore, when the ROC crosses above zero, it signifies the onset or continuation of an uptrend. Likewise, the ROC crossing below zero indicates the onset or continuation of a downtrend. Note, however, that because the ROC does not preserve lower-frequency information, it can produce whipsaw trading signals in sideways or continuously trending price series.

This script implements both the trend indicator and its ROC along with the following on-chart signals:
• Green and red arrows that indicate the possible onset or continuation of an uptrend and downtrend, respectively
• Bar and plot colors that signify the sign (direction) of the trend indicator


█ CALCULATIONS

The math behind the trend indicator comes from digital filter design principles. The first step applies a digital highpass filter that attenuates long cycles with periods above the user-specified critical period. The default value is 250 bars, representing roughly one year for instruments such as stocks on the daily timeframe. The next step applies a highpass filter with a shorter period (40 bars by default). The difference between these filters determines the trend indicator, which preserves cyclic components between 40 and 250 bars by default while attenuating and eliminating others. The ROC represents the scaled one-bar difference in the trend indicator.
ehlersTrend Analysis
PineCodersTASC
Tools and ideas for all Pine coders: tradingview.com/u/PineCoders/
Pine news broadcasts: t.me/PineCodersSquawkBox or twitter.com/PineCoders
TASC: traders.com/

סקריפט קוד פתוח

ברוח TradingView אמיתית, מחבר הסקריפט הזה פרסם אותו בקוד פתוח, כך שסוחרים יוכלו להבין ולאמת אותו. כל הכבוד למחבר! אתה יכול להשתמש בו בחינם, אבל השימוש החוזר בקוד זה בפרסום כפוף לכללי הבית. אתה יכול להכניס אותו למועדפים כדי להשתמש בו בגרף.

רוצה להשתמש בסקריפ זה בגרף?

כתב ויתור