Ehlers Precision Trend For ThinkOrSwim

BlackCurtain

New member
VIP
Hi, everyone, I've benefited greatly from many posts here, so wanted to share this that I created from the TASC article by John Ehlers. He calls it the Precision Trend Indicator.

John Ehlers introduces this method that uses two filters to remove unwanted "noise" from price data. These filters react very quickly to price changes, meaning the trend indicator they create closely follows the ups and downs of the market. When the indicator is above zero, it signals an uptrend, and when it's below zero, it indicates a downtrend. This makes it a helpful tool for spotting changes in market direction.

I have always been fascinated by Ehlers' work, especially the decycler and supersmoother. If anyone has ideas on how to use this indicator, please share. I'm not sure how useful it will be but it wasn't coded up in the TASC so I figured I'd take a try at it. [NOTE EDITED AS I MISPASTED THE FIRST VERSION!!!]

Screenshot 2024-09-01 200724.png


Ruby:
# TASC 2024.09 Precision Trend Analysis
# Converted to ThinkScript for thinkorswim
# Original by John F. Ehlers

declare lower;

input length01 = 250;
input length02 = 40;

def PI = 3.14159265359;

# High-pass filter calculations
def a1 = Exp(-1.414 * PI / length01);
def b1 = 2 * a1 * Cos(1.414 * PI / length01);
def c2 = b1;
def c3 = -a1 * a1;
def c1 = (1.0 + c2 - c3) * 0.25;

rec HP1 = if BarNumber() >= 4 then
c1 * (close - 2 * close[1] + close[2]) + c2 * HP1[1] + c3 * HP1[2]
else
0;

def a2 = Exp(-1.414 * PI / length02);
def b2 = 2 * a2 * Cos(1.414 * PI / length02);
def c22 = b2;
def c33 = -a2 * a2;
def c11 = (1.0 + c22 - c33) * 0.25;

rec HP2 = if BarNumber() >= 4 then
c11 * (close - 2 * close[1] + close[2]) + c22 * HP2[1] + c33 * HP2[2]
else
0;

def Trend = HP1 - HP2;
def ROC = (length02 / 6.28) * (Trend - Trend[1]);

plot TrendLine = Trend;
plot ROCLine = ROC;
plot ZeroLine = 0;

TrendLine.SetDefaultColor(GetColor(1));
TrendLine.SetLineWeight(2);
ROCLine.SetDefaultColor(GetColor(5));
ROCLine.SetLineWeight(2);
ZeroLine.SetDefaultColor(GetColor(0));

def Peak = if ROC crosses below 0 and Trend < 0 then ROC else Double.NaN;
def Valley = if ROC crosses above 0 and Trend > 0 then ROC else Double.NaN;

plot PeakMarker = Peak;
plot ValleyMarker = Valley;

PeakMarker.SetPaintingStrategy(PaintingStrategy.POINTS);
PeakMarker.SetLineWeight(3);
PeakMarker.SetDefaultColor(GetColor(1));
ValleyMarker.SetPaintingStrategy(PaintingStrategy.POINTS);
ValleyMarker.SetLineWeight(3);
ValleyMarker.SetDefaultColor(GetColor(2));

# Dynamic coloring
TrendLine.AssignValueColor(if Trend > 0 then Color.GREEN else Color.RED);
#AssignPriceColor(if Trend > 0 then Color.GREEN else Color.RED);
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Hi, everyone, I've benefited greatly from many posts here, so wanted to share this that I created from the TASC article by John Ehlers. He calls it the Precision Trend Indicator.

John Ehlers introduces this method that uses two filters to remove unwanted "noise" from price data. These filters react very quickly to price changes, meaning the trend indicator they create closely follows the ups and downs of the market. When the indicator is above zero, it signals an uptrend, and when it's below zero, it indicates a downtrend. This makes it a helpful tool for spotting changes in market direction.

I have always been fascinated by Ehlers' work, especially the decycler and supersmoother. If anyone has ideas on how to use this indicator, please share. I'm not sure how useful it will be but it wasn't coded up in the TASC so I figured I'd take a try at it. [NOTE EDITED AS I MISPASTED THE FIRST VERSION!!!]

View attachment 22799

Ruby:
# TASC 2024.09 Precision Trend Analysis
# Converted to ThinkScript for thinkorswim
# Original by John F. Ehlers

declare lower;

input length01 = 250;
input length02 = 40;

def PI = 3.14159265359;

# High-pass filter calculations
def a1 = Exp(-1.414 * PI / length01);
def b1 = 2 * a1 * Cos(1.414 * PI / length01);
def c2 = b1;
def c3 = -a1 * a1;
def c1 = (1.0 + c2 - c3) * 0.25;

rec HP1 = if BarNumber() >= 4 then
c1 * (close - 2 * close[1] + close[2]) + c2 * HP1[1] + c3 * HP1[2]
else
0;

def a2 = Exp(-1.414 * PI / length02);
def b2 = 2 * a2 * Cos(1.414 * PI / length02);
def c22 = b2;
def c33 = -a2 * a2;
def c11 = (1.0 + c22 - c33) * 0.25;

rec HP2 = if BarNumber() >= 4 then
c11 * (close - 2 * close[1] + close[2]) + c22 * HP2[1] + c33 * HP2[2]
else
0;

def Trend = HP1 - HP2;
def ROC = (length02 / 6.28) * (Trend - Trend[1]);

plot TrendLine = Trend;
plot ROCLine = ROC;
plot ZeroLine = 0;

TrendLine.SetDefaultColor(GetColor(1));
TrendLine.SetLineWeight(2);
ROCLine.SetDefaultColor(GetColor(5));
ROCLine.SetLineWeight(2);
ZeroLine.SetDefaultColor(GetColor(0));

def Peak = if ROC crosses below 0 and Trend < 0 then ROC else Double.NaN;
def Valley = if ROC crosses above 0 and Trend > 0 then ROC else Double.NaN;

plot PeakMarker = Peak;
plot ValleyMarker = Valley;

PeakMarker.SetPaintingStrategy(PaintingStrategy.POINTS);
PeakMarker.SetLineWeight(3);
PeakMarker.SetDefaultColor(GetColor(1));
ValleyMarker.SetPaintingStrategy(PaintingStrategy.POINTS);
ValleyMarker.SetLineWeight(3);
ValleyMarker.SetDefaultColor(GetColor(2));

# Dynamic coloring
TrendLine.AssignValueColor(if Trend > 0 then Color.GREEN else Color.RED);
#AssignPriceColor(if Trend > 0 then Color.GREEN else Color.RED);
Thanks @BlackCurtain This looks interesting but I am not sure how to interpret the two lines in the indicator. I assume when the faster moving line is above zero the trend is bullish? Not sure how to read the slower moving line. Hopefully others can chime in.
 
From the article in the September 2024 issue of TASC magazine, John Ehlers says that you should be long when the trend indicator is above the zero line, and short when the trend indicator is below the zero line.

Better timing can be achieved by adding the ROC indicator as it crosses the zero line; since that is when the trend indicator will be switching, although there are of course false whipsaw signals.

But I think his point is that WHEN the ROC line crosses above/below zero, that is ALWAYS at a turning point, but sometimes it signals a false turning point, too. Would welcome anyone else's interpretation, if you have seen the article!
 
Last edited by a moderator:
I was going to ask someone to convert from the original pine script ( https://www.tradingview.com/script/XxSVTg0v-TASC-2024-09-Precision-Trend-Analysis/ ) and I just saw your thread and that is great. It gave me the idea to use a tool ( github copilot ) to also make the convers ion, , which I have used for similar use, and in this case with some minor corrections.
I do prefer to use different lengths (shorter) but of course each one has a different preference.
================================================
Ruby:
# TASC Issue: September 2024
# Article: Precision Trend Analysis.
# A New Way To Pinpoint Trend.
# Article By: John F. Ehlers
# Language: ThinkScript for ThinkOrSwim
# https://usethinkscript.com/threads/ehlers-precision-trend-for-thinkorswim.19567/
# with minor corrections, converted from pine script by github copilot

declare lower;

# --- Inputs ---
input length01 = 250;
input length02 = 40;

# --- Functions ---
script HighPass {
input Price = close;
input Period = 250;
def a1 = Exp(-1.414 * Double.Pi / Period);
def b1 = 2 * a1 * Cos(1.414 * Double.Pi / Period);
def c2 = b1;
def c3 = -a1 * a1;
def c1 = (1 + c2 - c3) / 4;
def hp = if (BarNumber() >= 4) then
c1 * (Price - 2 * Price[1] + Price[2]) +
c2 * hp[1] + c3 * hp[2]
else 0;
plot HighPass = hp;
}

# --- Calculations ---
def HP1 = HighPass(Price = close, Period = length01);
def HP2 = HighPass(Price = close, Period = length02);
def Trend = HP1 - HP2;
def ROC = (length02 / 6.28) * (Trend - Trend[1]);

def upROC = ROC crosses below 0 and Trend < 0;
def dwROC = ROC crosses above 0 and Trend > 0;

# --- Visuals ---
def up = Trend > 0;
plot TrendIndicator = Trend;
TrendIndicator.AssignValueColor(if up then Color.GREEN else Color.RED);
TrendIndicator.SetLineWeight(2);

plot ROCPlot = ROC;
ROCPlot.SetDefaultColor(Color.BLUE);
ROCPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
ROCPlot.SetLineWeight(2);

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.GRAY);

AddChartBubble(upROC, ROC, "Peak", Color.RED, yes);
AddChartBubble(dwROC, ROC, "Valley", Color.GREEN, no);

AssignPriceColor(if Trend > 0 then Color.GREEN else Color.RED);
 
Last edited by a moderator:
Hoping to convert this tradeview indicator for think or swim. It generates a buy signal when the indicator crosses over the zero line. Also, is it possible to create a scan if the formula is not too busy. Thanks.

// TASC Issue: September 2024
// Article: Precision Trend Analysis.
// A New Way To Pinpoint Trend.
// Article By: John F. Ehlers
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
443 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