# Convert TrendFlex Oscillator - John Ehlers to thinkorswim?

#### borntostun

##### New member
Lifetime
Hi all.

So the latest S&C includes an intriguing, new indicator from John Ehlers -- the Trendflex. Code is provided in Trader's Tips, but I've also located a modified version over on Tradingview that has some options and tricks that are attractive.

https://www.tradingview.com/script/8ZSALctc-TrendFlex-Oscillator-Dr-John-Ehlers/
I'm curious if anyone could assist in translating the pinescript to thinkscript? Thanks all!

Code:
``````//@version=4
study("TrendFlex Oscillator - Dr. John Ehlers", "TFO", false, format.price, 2)

bgcolor(color.new(#000000,15), title="Dark Background")

trendflex(Series, PeriodSS, PeriodTrendFlex, PeriodEMA) =>
var SQRT2xPI = sqrt(8.0) * asin(1.0) // 4.44288293815 Constant
alpha = SQRT2xPI / PeriodSS
beta = exp(-alpha)
gamma = -beta * beta
delta = 2.0 * beta * cos(alpha)
float superSmooth = na, superSmooth := (1.0 - delta - gamma) * (Series + nz(Series[1])) * 0.5 + delta * nz(superSmooth[1]) + gamma * nz(superSmooth[2])
E = 0.0
for i=1 to PeriodTrendFlex
E := E + superSmooth - nz(superSmooth)
epsilon = E / PeriodTrendFlex
zeta = 2.0 / (PeriodEMA + 1.0)
float EMA = na, EMA := zeta * epsilon * epsilon + (1.0 - zeta) * nz(EMA[1])
return = EMA==0.0 ? 0.0 : epsilon / sqrt(EMA)

source                  = input( close,                              "Source", input.source)
periodTrendFlex         = input(    20,                    "TrendFlex Period", input.integer, minval=2)
useSuperSmootherOveride = input( false, "Apply SuperSmoother Override Below*", input.bool)
periodSuperSmoother     = input(   8.0,               "SuperSmoother Period*", input.float  , minval=4.0, step=0.5)
postSmooth              = input(  33.0,                "Post Smooth Period**", input.float  , minval=1.0, step=0.5)
lineThickness           = input(     2,            "---- Line Thickness ----", input.integer, options=[1,2,3])
showArea                = input("Show",                        "Display Area", input.string , options=["Show","Hide"])
upperLevel              = input(   1.0,                         "Upper Level", input.float  , minval= 0.1, maxval= 2.0, step=0.1)
lowerLevel              = input(  -1.0,                         "Lower Level", input.float  , minval=-2.0, maxval=-0.1, step=0.1)
var HIDE_AREA = not (showArea=="Show")
if(not useSuperSmootherOveride)
periodSuperSmoother := periodTrendFlex * 0.5

trendFlexOscillator = trendflex(source, periodSuperSmoother, periodTrendFlex, postSmooth)

plot(        0.0, color=#FFFFFF22, linewidth=7            , editable=false)
hline(       0.0, color=#FFFFFFff, title=           "Zero", editable=false)
hline(upperLevel, color=#FF0000ff, title="Upper Threshold")
hline(lowerLevel, color=#00FF00ff, title="Lower Threshold")

plot(HIDE_AREA ? na : trendFlexOscillator, color=#AA00FF  , transp=80, style=plot.style_area, title="TrendFlex Area")
plot(                 trendFlexOscillator, color=#AA00FFff, linewidth=lineThickness         , title="TrendFlex")``````

Last edited by a moderator:

#### tomsk

##### Well-known member
VIP
@borntostun TASC published 2 scripts in their Feb issue. They put together a pair of studies based on the article by John Ehlers, “Reflex: A New Zero-Lag Indicator.” The first script is called Reflex and the second script is called Trendflex. Here are the links that TASC published

Reflex

https://tos.mx/KuKGOLK
Trendflex

https://tos.mx/aE5oFkr

#### borntostun

##### New member
Lifetime
Ya, I got those . . . I just like how the TradingView scipt has some added features? .

#### borntostun

##### New member
Lifetime
Apologies, I'm no coder.

Any help/teaching as to how to make some simple changes to the code provided from S&C?

I'd love to place a dashed horizontal line at -1 (in green) and +1 (in red) on this indicator -- with alerts on cross ups of green and cross downs of red.

Thanks!

---------

declare lower;

input length = 20;

def filter = reference EhlersSuperSmootherFilter("cutoff length" = length / 2);
def average = (fold i = 1 to length + 1 with sum do sum + filter - filter) / length;
def rms = Sqrt(ExpAverage(Sqr(average), 50));

plot Trendflex = average / rms;
plot ZeroLine = 0;

Trendflex.SetDefaultColor(color = Color.DOWNTICK);
ZeroLine.SetDefaultColor(color = Color.GRAY);

#### tomsk

##### Well-known member
VIP
@borntostun Here are the modifications for lines and alerts to your study, as requested

Code:
``````declare lower;

input length = 20;

def filter = reference EhlersSuperSmootherFilter("cutoff length" = length / 2);
def average = (fold i = 1 to length + 1 with sum do sum + filter - filter[i]) / length;
def rms = Sqrt(ExpAverage(Sqr(average), 50));

plot Trendflex = average / rms;
plot ZeroLine = 0;
plot one = 1;
plot minusOne = -1;

Trendflex.SetDefaultColor(color = Color.DOWNTICK);
ZeroLine.SetDefaultColor(color = Color.GRAY);
one.SetDefaultColor(Color.Red);
minusOne.SetDefaultColor(Color.Green);

Alert(Trendflex crosses above minusOne, "Cross above -1", Alert.Bar, Sound.Ring);
Alert(Trendflex crosses below one, "Cross below +1", Alert.Bar, Sound.Bell);``````

#### borntostun

##### New member
Lifetime
Awesome, thanks!

And this will make it dashed?

one.SetStyle(Curve.short_DASH);

#### tomsk

##### Well-known member
VIP
You already have the right syntax, so why not go ahead and implement your change - that is always the best way to learn the effects of your changes!

#### john3

##### Active member
2019 Donor
It doesn't seem to be better than RSI.

#### dolomick

##### Member
It doesn't seem to be better than RSI.
I found the two combined better than RSI, giving clearer signals for entries and exits.

#### horserider

##### Well-known member
VIP
Please show us how you set it up to be better than the RSI.

#### dolomick

##### Member
Look here

Reflex is on top, then Trendflex, then RSI, shown in that order The red shows a divergence that could be used to short. The yellow shows the overall trend and a long entry. RSI not giving nearly that much information, no obvious entries shown.

Last edited by a moderator:

#### horserider

##### Well-known member
VIP
Thanks for the example. That should help those interested in this indicator. I will say the RSI can supply lots of information. Careful not to discount it too quickly.

#### dolomick

##### Member
Thanks for the example. That should help those interested in this indicator. I will say the RSI can supply lots of information. Careful not to discount it too quickly.
Oh yeah I like the RSI too, my problem is I like too many indicators