Fold Function used with RSI Value

kiwi

New member
VIP
I am trying to sum the values, if RSI (close, x) > 0 then 1 else 0, across different lookback (x) RSI periods and different sum periods. The long hand coded version would look like the following:

input lb1 = 14;
input lb2 = 15;
input lb3 = 16;
input SumPeriod = 34;
def sum1 = sum(if rsi(close,lb1) > 0 then 1 else 0, SumPeriod);
def sum2 = sum(if rsi(close,lb2) > 0 then 1 else 0, SumPeriod);
def sum3 = sum(if rsi(close,lb3) > 0 then 1 else 0, SumPeriod);

The following fold works correctly, but is not what I am trying to arrive at. It sums the rsix values > 0 for the last number of bars in the SumPeriod (34).

input lb1 = 14;
input SumPeriod = 34;
def rsix = rsi(close, lb1);
def sum = fold i = 0 to SumPeriod with s do s + if rsix > 0 then 1 else 0;

I know the following fold function is not the complete code that is the equivalent of the long hand code above, but I cannot get past this part, TOS does not like this:

def sum = fold i = 0 to lb1 with s do s + rsi(close,i);

Is what I am asking of the fold function outside of it's capabilities, or I am missing something? Any assistance is appreciated!
 
The fold function evaluates "GetValue(rsix,i)" correctly, but that does not give the result I am looking for. The following is the code I am trying to resolve.

input rsiBegin = 14;
input rsiEnd = 21;
input rsiSum = 10;

def SumUp = fold i = 0 to rsiSum with s while i < rsiSum do s + fold j = rsiBegin to rsiEnd with t do t + if GetValue(RSI(close,t)) > 0 then 1 else 0;

GetValue(RSI(close,t)) in above creates an error "No default value for parameter 'dynamic offset' on getvalue at 182:110"

When an offset value is added, GetValue(RSI(close,t), rsiEnd) in above it creates an error
"Only constants expected here:"

This better describes the problem I am having.

Are there any solutions, or is this just not within fold's capability?

Thanks!
 
The fold function evaluates "GetValue(rsix,i)" correctly, but that does not give the result I am looking for. The following is the code I am trying to resolve.

input rsiBegin = 14;
input rsiEnd = 21;
input rsiSum = 10;

def SumUp = fold i = 0 to rsiSum with s while i < rsiSum do s + fold j = rsiBegin to rsiEnd with t do t + if GetValue(RSI(close,t)) > 0 then 1 else 0;

GetValue(RSI(close,t)) in above creates an error "No default value for parameter 'dynamic offset' on getvalue at 182:110"

When an offset value is added, GetValue(RSI(close,t), rsiEnd) in above it creates an error
"Only constants expected here:"

This better describes the problem I am having.

Are there any solutions, or is this just not within fold's capability?

Thanks!
No, fold cannot dynamically loop over indicator lengths or pass variables into GetValue()'s offset. But, you can hardcode the individual RSI periods and sum them manually — that’s the only real solution within ThinkScript’s static limitations.
 
The fold function evaluates "GetValue(rsix,i)" correctly, but that does not give the result I am looking for. The following is the code I am trying to resolve.

input rsiBegin = 14;
input rsiEnd = 21;
input rsiSum = 10;

def SumUp = fold i = 0 to rsiSum with s while i < rsiSum do s + fold j = rsiBegin to rsiEnd with t do t + if GetValue(RSI(close,t)) > 0 then 1 else 0;

GetValue(RSI(close,t)) in above creates an error "No default value for parameter 'dynamic offset' on getvalue at 182:110"

When an offset value is added, GetValue(RSI(close,t), rsiEnd) in above it creates an error
"Only constants expected here:"

This better describes the problem I am having.

Are there any solutions, or is this just not within fold's capability?

Thanks!

rsi is always > 0, so what is the point of the loops checking if rsi > 0 ?
rewrite your description / code.
 
Last edited:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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