Truncated Indicator

kiwi

New member
VIP
I'm trying to code John Ehlers Truncated Indicator that he published in Traders Tips July 2020. The code I have does not indicate any errors in the thinkScriptEditor, but does not produce a plot.

I would appreciate some assistance as to where I have gone wrong with coding and any guidance of where to go to get the indicator working.

declare lower;

input Period = 20;
input Bandwidth = .1;
input Length = 10; #must be less than 98 due to array size

def L1 = Cos(360 / 180 * Double.Pi / Period);
def G1 = Cos(360 / 180 * Double.Pi * Bandwidth / Period);
def S1 = 1 / G1 - Sqrt( 1 / (G1 * G1) - 1);
def BP = CompoundValue(3, .5 * (1 - S1) * (close - close[2]) + L1 * (1 + S1) * BP[1] - S1 * BP[2],0);

def Trunc = fold i = -length to 1 do .5 * (1 - S1)(GetValue(Close,i - 1) - GetValue(Close,i + 1)) + L1(1 +
S1)*GetValue(Trunc,(i + 1)) - S1*GetValue(Trunc,i + 2);

plot BPT = Trunc[1];

Thanks!
 
Solution
I'm trying to code John Ehlers Truncated Indicator that he published in Traders Tips July 2020. The code I have does not indicate any errors in the thinkScriptEditor, but does not produce a plot.

I would appreciate some assistance as to where I have gone wrong with coding and any guidance of where to go to get the indicator working.

declare lower;

input Period = 20;
input Bandwidth = .1;
input Length = 10; #must be less than 98 due to array size

def L1 = Cos(360 / 180 * Double.Pi / Period);
def G1 = Cos(360 / 180 * Double.Pi * Bandwidth / Period);
def S1 = 1 / G1 - Sqrt( 1 / (G1 * G1) - 1);
def BP = CompoundValue(3, .5 * (1 - S1) * (close - close[2]) + L1 * (1 + S1) * BP[1] - S1 * BP[2],0);

def Trunc = fold i = -length to 1 do...
This?
Code:
def Trunc = fold i = -length to 1 do .5 * (1 - S1)(GetValue(Close,i - 1) - GetValue(Close,i + 1)) + L1(1 +
S1)*GetValue(Trunc,(i + 1)) - S1*GetValue(Trunc,i + 2);
Is it missing an operation after L1 ? Looks like it's trying to call a function called L1()

-mashume
 

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

This?
Code:
def Trunc = fold i = -length to 1 do .5 * (1 - S1)(GetValue(Close,i - 1) - GetValue(Close,i + 1)) + L1(1 +
S1)*GetValue(Trunc,(i + 1)) - S1*GetValue(Trunc,i + 2);
Is it missing an operation after L1 ? Looks like it's trying to call a function called L1()

-mashume
mashume,

Thank you for your response. Your observation, at first glance, appears that would be the problem. In thinkScriptEditor the text wrapped that line. I corrected at your suggestion, but it did not correct the problem.

Here is the code from Ehlers "Truncated Indicator" pdf.

BandPass Filter and Truncated Bandpass Filter
(C) 2005-2020 John F. Ehlers
}
Inputs:
Period(20),
Bandwidth(.1),
Length(10); //must be less than 98 due to array size
Vars:
L1(0), G1(0), S1(0), count(0),
BP(0), BPT(0);
Arrays:
Trunc[100](0);
//Standard Bandpass
L1 = Cosine(360 / Period);
G1 = Cosine(Bandwidth*360 / Period);
S1 = 1 / G1 - SquareRoot( 1 / (G1*G1) - 1);
BP = .5*(1 - S1)(Close - Close[2]) + L1(1 + S1)*BP[1] - S1*BP[2];
If CurrentBar <= 3 Then BP = 0;
//Truncated Bandpass
Trunc[Length + 2] = 0;
Trunc[Length + 1] = 0;
For count = Length DownTo 1 Begin
Trunc[count] = .5*(1 - S1)(Close[count - 1] - Close[count + 1]) + L1(1 +
S1)*Trunc[count + 1] - S1*Trunc[count + 2];
End;
BPT = Trunc[1]; //convert to a variable
Plot1(BP);
Plot4(0);
Plot2(BPT);
 
mashume,

Thank you for your response. Your observation, at first glance, appears that would be the problem. In thinkScriptEditor the text wrapped that line. I corrected at your suggestion, but it did not correct the problem.

Here is the code from Ehlers "Truncated Indicator" pdf.

BandPass Filter and Truncated Bandpass Filter
(C) 2005-2020 John F. Ehlers
}
Inputs:
Period(20),
Bandwidth(.1),
Length(10); //must be less than 98 due to array size
Vars:
L1(0), G1(0), S1(0), count(0),
BP(0), BPT(0);
Arrays:
Trunc[100](0);
//Standard Bandpass
L1 = Cosine(360 / Period);
G1 = Cosine(Bandwidth*360 / Period);
S1 = 1 / G1 - SquareRoot( 1 / (G1*G1) - 1);
BP = .5*(1 - S1)(Close - Close[2]) + L1(1 + S1)*BP[1] - S1*BP[2];
If CurrentBar <= 3 Then BP = 0;
//Truncated Bandpass
Trunc[Length + 2] = 0;
Trunc[Length + 1] = 0;
For count = Length DownTo 1 Begin
Trunc[count] = .5*(1 - S1)(Close[count - 1] - Close[count + 1]) + L1(1 +
S1)*Trunc[count + 1] - S1*Trunc[count + 2];
End;
BPT = Trunc[1]; //convert to a variable
Plot1(BP);
Plot4(0);
Plot2(BPT);
I'll try to give it more than a glancing consideration tomorrow morning.

-mashume
 
I'm trying to code John Ehlers Truncated Indicator that he published in Traders Tips July 2020. The code I have does not indicate any errors in the thinkScriptEditor, but does not produce a plot.

I would appreciate some assistance as to where I have gone wrong with coding and any guidance of where to go to get the indicator working.

declare lower;

input Period = 20;
input Bandwidth = .1;
input Length = 10; #must be less than 98 due to array size

def L1 = Cos(360 / 180 * Double.Pi / Period);
def G1 = Cos(360 / 180 * Double.Pi * Bandwidth / Period);
def S1 = 1 / G1 - Sqrt( 1 / (G1 * G1) - 1);
def BP = CompoundValue(3, .5 * (1 - S1) * (close - close[2]) + L1 * (1 + S1) * BP[1] - S1 * BP[2],0);

def Trunc = fold i = -length to 1 do .5 * (1 - S1)(GetValue(Close,i - 1) - GetValue(Close,i + 1)) + L1(1 +
S1)*GetValue(Trunc,(i + 1)) - S1*GetValue(Trunc,i + 2);

plot BPT = Trunc[1];

Thanks!

i experimented with this and have a study that draws a line.
i don't know if it is what is intended.

the code in post #1 has errors for me. it is missing 2 math operators in the trunc = formula, next to a (.
. 1 - S1)(Get
. + L1(1 + S1)
i added a * in both places ( multiplication )
. 1 - S1) * (Get
. + L1 * (1 + S1)

the fold loop had an error, trying to self assign itself.
this happens when the resultant variable is in its formula with an offset of 0. ( maybe a negative offset also causes this ?)

i changed,
. the fold count, to be , 1 to length+1
. replace i in fold, with (length - i) , to do a reverse count

in the code are some comments on some things i tried with the trunc formula.

Ruby:
# JohnEhlersTruncated_00b

declare lower;

def bn = barnumber();
input Period = 20;
input Bandwidth = .1;
input Length = 10; #must be less than 98 due to array size

def L1 = Cos(360 / 180 * Double.Pi / Period);
def G1 = Cos(360 / 180 * Double.Pi * Bandwidth / Period);
def S1 = 1 / G1 - Sqrt( 1 / (G1 * G1) - 1);
def BP = CompoundValue(3, .5 * (1 - S1) * (close - close[2]) + L1 * (1 + S1) * BP[1] - S1 * BP[2], 0);

# original formula
#def Trunc = fold i = -length to 1  do .5 * (1 - S1)(GetValue(Close,i - 1) - GetValue(Close,i + 1)) + L1(1 + S1)*GetValue(Trunc,(i + 1)) - S1*GetValue(Trunc,i + 2);
# missing math operator in 2 places,  )(  and   L1(
#   1 - S1)(Get
#   + L1(1 + S1)


#  add * for missing operators
# loop negative # to 1  ??
# values , error , trying to self assign trunc
#def Trunc = fold i = -length to 1
#  do ( 0.5 * (1 - S1) * (GetValue(Close,i - 1)
#  - GetValue(Close,i + 1))
#  + L1 * (1 + S1) * GetValue(Trunc,(i + 1))
#  - S1*GetValue(Trunc, i + 2));


# chg i , 1 to length
# values , HUGE numbers
#def Trunc = fold i = 1 to (length + 1)
#  do ( 0.5 * (1 - S1) * (GetValue(Close,i - 1)
#  - GetValue(Close,i + 1))
#  + L1 * (1 + S1) * GetValue(Trunc,(i + 1))
#  - S1*GetValue(Trunc, i + 2));


# reverse i count
# values , approx  -1 to 1
def Trunc = fold i = 1 to (length + 1)
  do ( 0.5 * (1 - S1) * (GetValue(Close, (length-i) - 1)
  - GetValue(Close, (length-i) + 1))
  + L1 * (1 + S1) * GetValue(Trunc,( (length-i) + 1))
  - S1*GetValue(Trunc, (length-i) + 2));

plot BPT = Trunc[1];

plot z =0;

# -----------------------------------

#  test stuff
input test1 = no;
addchartbubble(test1, 0,
bn
+ "\nL" + l1
+ "\nG" + g1
+ "\nS" + s1
+ "\nB" + bp
+ "\nT" + trunc
, color.cyan, yes);
#
#


i think this is the source
http://traders.com/Documentation/FEEDbk_docs/2020/07/TradersTips.html

AAPL 90D 1H
53rGfIw.jpg
 
Last edited:
Solution
i experimented with this and have a study that draws a line.
i don't know if it is what is intended.

the code in post #1 has errors for me. it is missing 2 math operators in the trunc = formula.
. 1 - S1)(Get
. + L1(1 + S1)
i added a * in both places ( multiplication )

the fold loop had an error, trying to self assign itself.
this happens when the resultant variable is in its formula with an offset of 0. ( maybe a negative offset also causes this ?)

i changed,
. the fold count, to be , 1 to length+1
. replace i in fold, with (length - i) , to do a reverse count

in thd code are some comments on some things i tried with the trunc formula.

 

halcyonguy

Thank you for the response, however the plot is not correct. The changes you recommended create the same plot as the Standard Bandpass code from earlier in the script. The link you provided to Ehlers original post is correct.
 

halcyonguy

Thank you for the response, however the plot is not correct. The changes you recommended create the same plot as the Standard Bandpass code from earlier in the script. The link you provided to Ehlers original post is correct.

...also, why are some indicators coded to TOS and posted with Trading Tips and some are not, such as this one.
 

halcyonguy

Thank you for the response, however the plot is not correct. The changes you recommended create the same plot as the Standard Bandpass code from earlier in the script. The link you provided to Ehlers original post is correct.

sorry, then , i guess i don't understand the original well enough and what changes you want.
.
i don't know what you are referring to with this comment.
'why are some indicators coded to TOS and posted with Trading Tips and some are not, such as this one.'
 
i experimented with this and have a study that draws a line.
i don't know if it is what is intended.

the code in post #1 has errors for me. it is missing 2 math operators in the trunc = formula, next to a (.
. 1 - S1)(Get
. + L1(1 + S1)
i added a * in both places ( multiplication )
. 1 - S1) * (Get
. + L1 * (1 + S1)

the fold loop had an error, trying to self assign itself.
this happens when the resultant variable is in its formula with an offset of 0. ( maybe a negative offset also causes this ?)

i changed,
. the fold count, to be , 1 to length+1
. replace i in fold, with (length - i) , to do a reverse count

in the code are some comments on some things i tried with the trunc formula.
Ruby,

Thank you for your input. The changes you made to the Truncated Indicator, when plotted with the BandPass, are both the same plot. My original code did include the missing multiplyer you noted...not sure why it did not copy/paste into my post.
 
sorry, then , i guess i don't understand the original well enough and what changes you want.
.
i don't know what you are referring to with this comment.
'why are some indicators coded to TOS and posted with Trading Tips and some are not, such as this one.'
Halcyonguy,

"Why are some indicators coded to TOS and posted with Trading Tips and some are not, such as this one", sorry for the confusion. If you look at the Trading Tips posts going back over a year or more, there are some posts that do not feature TOS code link for the post, most however do include TOS scripts. The Truncated Indicator was one that did not have a TOS script associated with the post for that month.

Hope that helps clairify what I was asking about.
 
Halcyonguy,

"Why are some indicators coded to TOS and posted with Trading Tips and some are not, such as this one", sorry for the confusion. If you look at the Trading Tips posts going back over a year or more, there are some posts that do not feature TOS code link for the post, most however do include TOS scripts. The Truncated Indicator was one that did not have a TOS script associated with the post for that month.

Hope that helps clairify what I was asking about.
This is a public forum, whether the OP includes links, tips, charts, images is all up to the individual poster
 
Ruby,

Thank you for your input. The changes you made to the Truncated Indicator, when plotted with the BandPass, are both the same plot. My original code did include the missing multiplyer you noted...not sure why it did not copy/paste into my post.
you code may have been accidently ? altered when you pasted the code into a post , without putting it in a code window, sometimes character combinations can be interpreted as something else, like icons, or ...
when you write a post, at the top is a header with icons. </> will pop open a code window, for pasting in code
 
Halcyonguy,

"Why are some indicators coded to TOS and posted with Trading Tips and some are not, such as this one", sorry for the confusion. If you look at the Trading Tips posts going back over a year or more, there are some posts that do not feature TOS code link for the post, most however do include TOS scripts. The Truncated Indicator was one that did not have a TOS script associated with the post for that month.

Hope that helps clairify what I was asking about.

sorry, i have no idea of what this 'trading tips' is you are talking about.
if it is on another site , i don't know.
if it is on traders.com , i don't know. i've never been to that site before i looked up that link.
i hope this doesn't sound negative,
maybe someone else knows.
 
I recently ran across the same article at traders.com and wanted it as well. This was the best I could come up with given that Thinkscript doesn't allow you to access arrays directly and there are only so many variables you can declare before you run into memory issues. But on the plus side it is the truncated Bandpass indicator so long as you are fine with the constraint of having the length be between 5 and 24

Code:
script TruncatedBP {
    input Period = 20;
    input Bandwidth = 0.1;
    input Length = 10;
    input Price = Close;

    def L1 = Cos(2 * Double.Pi / (Period));
    def G1 = Cos(bandwidth * 2 * Double.Pi / (Period));
    def S1 = 1 / G1 - Sqrt(1 / Sqr(G1) - 1);

    def len = Length;
    def p = Price;

    def t24 = if len > 22 then .5*(1-S1)*(p[23]-p[25]) +L1*(1+S1)*0                           -S1*0                           else Double.NaN;
    def t23 = if len > 21 then .5*(1-S1)*(p[22]-p[24]) +L1*(1+S1)*(if len>23 then t24 else 0) -S1*0                           else Double.NaN;
    def t22 = if len > 20 then .5*(1-S1)*(p[21]-p[23]) +L1*(1+S1)*(if len>22 then t23 else 0) -S1*(if len>23 then t24 else 0) else Double.NaN;
    def t21 = if len > 19 then .5*(1-S1)*(p[20]-p[22]) +L1*(1+S1)*(if len>21 then t22 else 0) -S1*(if len>22 then t23 else 0) else Double.NaN;
    def t20 = if len > 18 then .5*(1-S1)*(p[19]-p[21]) +L1*(1+S1)*(if len>20 then t21 else 0) -S1*(if len>21 then t22 else 0) else Double.NaN;
    def t19 = if len > 17 then .5*(1-S1)*(p[18]-p[20]) +L1*(1+S1)*(if len>19 then t20 else 0) -S1*(if len>20 then t21 else 0) else Double.NaN;
    def t18 = if len > 16 then .5*(1-S1)*(p[17]-p[19]) +L1*(1+S1)*(if len>18 then t19 else 0) -S1*(if len>19 then t20 else 0) else Double.NaN;
    def t17 = if len > 15 then .5*(1-S1)*(p[16]-p[18]) +L1*(1+S1)*(if len>17 then t18 else 0) -S1*(if len>18 then t19 else 0) else Double.NaN;
    def t16 = if len > 14 then .5*(1-S1)*(p[15]-p[17]) +L1*(1+S1)*(if len>16 then t17 else 0) -S1*(if len>17 then t18 else 0) else Double.NaN;
    def t15 = if len > 13 then .5*(1-S1)*(p[14]-p[16]) +L1*(1+S1)*(if len>15 then t16 else 0) -S1*(if len>16 then t17 else 0) else Double.NaN;
    def t14 = if len > 12 then .5*(1-S1)*(p[13]-p[15]) +L1*(1+S1)*(if len>14 then t15 else 0) -S1*(if len>15 then t16 else 0) else Double.NaN;
    def t13 = if len > 11 then .5*(1-S1)*(p[12]-p[14]) +L1*(1+S1)*(if len>13 then t14 else 0) -S1*(if len>14 then t15 else 0) else Double.NaN;
    def t12 = if len > 10 then .5*(1-S1)*(p[11]-p[13]) +L1*(1+S1)*(if len>12 then t13 else 0) -S1*(if len>13 then t14 else 0) else Double.NaN;
    def t11 = if len >  9 then .5*(1-S1)*(p[10]-p[12]) +L1*(1+S1)*(if len>11 then t12 else 0) -S1*(if len>12 then t13 else 0) else Double.NaN;
    def t10 = if len >  8 then .5*(1-S1)*(p[9]-p[11])  +L1*(1+S1)*(if len>10 then t11 else 0) -S1*(if len>11 then t12 else 0) else Double.NaN;
    def t9 =  if len >  7 then .5*(1-S1)*(p[8]-p[10])  +L1*(1+S1)*(if len>9 then t10 else 0)  -S1*(if len>10 then t11 else 0) else Double.NaN;
    def t8 =  if len >  6 then .5*(1-S1)*(p[7]-p[9])   +L1*(1+S1)*(if len>8 then t9 else 0)   -S1*(if len>9 then t10 else 0)  else Double.NaN;
    def t7 =  if len >  5 then .5*(1-S1)*(p[6]-p[8])   +L1*(1+S1)*(if len>7 then t8 else 0)   -S1*(if len>8 then t9 else 0)   else Double.NaN;
    def t6 =                   .5*(1-S1)*(p[5]-p[7])   +L1*(1+S1)*(if len>6 then t7 else 0)   -S1*(if len>7 then t8 else 0);
    def t5 =                   .5*(1-S1)*(p[4]-p[6])   +L1*(1+S1)*(if len>5 then t6 else 0)   -S1*(if len>6 then t7 else 0);
    def t4 =                   .5*(1-S1)*(p[3]-p[5])   +L1*(1+S1)*t5                          -S1*(if len>5 then t6 else 0);
    def t3 =                   .5*(1-S1)*(p[2]-p[4])   +L1*(1+S1)*t4                          -S1*t5;
    def t2 =                   .5*(1-S1)*(p[1]-p[3])   +L1*(1+S1)*t3                          -S1*t4;
    def t1 =                   .5*(1-S1)*(p[0]-p[2])   +L1*(1+S1)*t2                          -S1*t3;

    plot BPT = t1;
}

declare lower;

input Period = 20;
input Bandwidth = 0.1;
input Length = 10;
input Price = Close;
input ShowBP = yes;
input ShowBPT = yes;

Assert(Length >= 5, "Length minimum is 5");
Assert(Length <= 24, "Length maximum is 24");
Assert(Period >= 10, "Period minimum is 10");
Assert(Period <= 48, "Period maximum is 48");

plot BP = if !IsNaN(close) and ShowBP then BandPassFilter(Period, Bandwidth, 1).BandPassFilter else Double.NaN;

plot BPT = if ShowBPT then TruncatedBP(Period, Bandwidth, Length, Price).BPT else Double.NaN;

plot Zero = 0;
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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