Trend-Step Filtering For ThinkOrSwim

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

https://www.tradingview.com/script/UoXQCBPA-Setting-Less-Trend-Step-Filtering/
I've been trying to convert this one to TOS for awhile now but always get tripped up with the multi-layer recursion that Alex coded in. I'd be forever indebted to someone who could convert this, or even offer advice on how I could get rid of the recursion
check it out below. I added option for mutli-time frame as well.

CSS:
#//@version=4
#study("Setting-Less Trend-Step Filtering",overlay=true)
# Converted by Sam4Cok@Samer800 - 11/2022 - request from useThinkScript.com member
input useChartTime = yes;
input Aggregation = AggregationPeriod.FIFTEEN_MIN;
input srcInput = close;
input robust = yes;
#//----
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if data!=0 then data else repl;
    plot return = ret_val;
}
def c = if useChartTime then srcInput else close(Period = Aggregation);
def b; def sc; def a; def src; def n;
sc = if robust then AbsValue(c - nz(b[1]))/(AbsValue(c - nz(b[1])) + nz(a[1])) else 1;
src = sc*c+(1-sc)*nz(b[1],c);
#//----
n = TotalSum(1)-1;
a = TotalSum(AbsValue(src - nz(b[1],src)))/n*(if(robust,1,0)+sc);
b = if src > b[1] + a then src else if src < b[1] - a then src else b[1];
#//----
plot line = b;
line.SetDefaultColor(Color.CYAN);
line.SetLineWeight(2);

#--- END Code
 
check it out below. I added option for mutli-time frame as well.

CSS:
#//@version=4
#study("Setting-Less Trend-Step Filtering",overlay=true)
# Converted by Sam4Cok@Samer800 - 11/2022 - request from useThinkScript.com member
input useChartTime = yes;
input Aggregation = AggregationPeriod.FIFTEEN_MIN;
input srcInput = close;
input robust = yes;
#//----
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if data!=0 then data else repl;
    plot return = ret_val;
}
def c = if useChartTime then srcInput else close(Period = Aggregation);
def b; def sc; def a; def src; def n;
sc = if robust then AbsValue(c - nz(b[1]))/(AbsValue(c - nz(b[1])) + nz(a[1])) else 1;
src = sc*c+(1-sc)*nz(b[1],c);
#//----
n = TotalSum(1)-1;
a = TotalSum(AbsValue(src - nz(b[1],src)))/n*(if(robust,1,0)+sc);
b = if src > b[1] + a then src else if src < b[1] - a then src else b[1];
#//----
plot line = b;
line.SetDefaultColor(Color.CYAN);
line.SetLineWeight(2);

#--- END Code

Is it possible to make this code work with the custom close("Symbol")?
Right now, for example, if i'm trying to use close("SPX") on /ES chart it doesn't plot anything
 
Is it possible to make this code work with the custom close("Symbol")?
Right now, for example, if i'm trying to use close("SPX") on /ES chart it doesn't plot anything
try this code

CSS:
#//@version=4
#study("Setting-Less Trend-Step Filtering",overlay=true)
# Converted by Sam4Cok@Samer800 - 11/2022 - request from useThinkScript.com member
input useChartTime = {Default "Yes", "No"};
input Symbol = "SPX";
input Aggregation = AggregationPeriod.FIFTEEN_MIN;
input srcInput = close;
input robust = yes;

def na = Double.NaN;
def bar = BarNumber();
#def rob = if robust,1,0)
def HTFSrc = Fundamental(FundamentalType.CLOSE, symbol = Symbol, period = Aggregation);
#//----

def price;
Switch (useChartTime) {
Case "Yes" : price = srcInput;
Case "No"  : price = HTFSrc;
}

def c = price;
def b; def a;
def cb = AbsValue(c - b[1]);
def sc = if robust then cb /(cb + a[1]) else 1;
def src = sc * c + (1 - sc) * if(b[1], b[1], c);
#//----
def n = bar -1;
def bSrc = AbsValue(src - if(b[1],b[1],src));
def TotSum = TotalSum(bSrc);
a = TotSum / n * (robust + sc);
b = if src > b[1] + a then src else
    if src < b[1] - a then src else b[1];
#//----
plot line = b;
line.SetDefaultColor(Color.CYAN);
line.SetLineWeight(2);

#-- END of CODE
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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