Can not pass a fold index to a function

Sesqui

New member
VIP
Consider the following script which works fine:

def sy1 = fold i1 = 0 to length with s1 do (s1 + Cos(2 * Double.Pi * i1 / length));

Next consider replacing the COS part with function to allow length to be a changing variable:

Code:
script test_adapt {
input index = 0;
plot test_adapt = index;
}

def sy1 = fold i1 = 0 to length with s1 do (s1 + test_adapt(i1));

The above line does not work. The error is, "No such variable: i1". It appears I cannot pass the fold index i1 to a scripted function.

Has anyone sorted out a work around for this or is this not possible?

Warm regards,

Sesqui
 
Consider the following script which works fine:



Next consider replacing the COS part with function to allow length to be a changing variable:

Code:
script test_adapt {
input index = 0;
plot test_adapt = index;
}

def sy1 = fold i1 = 0 to length with s1 do (s1 + test_adapt(i1));

The above line does not work. The error is, "No such variable: i1". It appears I cannot pass the fold index i1 to a scripted function.

Has anyone sorted out a work around for this or is this not possible?

Warm regards,

Sesqui

it is hard to guess at what to do when partial code is posted.
your words are confusing, length is the same in each, not changing.

i don't think a fold loop can call a script.
will have to build all the formulas within the fold
 
Last edited:

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

Roger that. Thanks for your timely response. Good idea, will try building the formulas into the script. Again was totally lost. Don't know what would do without your valuable input.
 
SUMMING UP THE ISSUE:
The coding issue I encountered was I could not pass the product of a variable and the fold index counter to function COS() inside a fold loop. I tried creating a script COS_adapatible() that would take as input parameters the Periodogram (rounded down) and the fold loop index counter. But thinkscript would not accept passing the fold loop counter to the script. As a result, a nested fold loop was implemented that computed the COS() value summation, which resolved the issue.

The code below provides a copy paste of the TOS AutoCorrelationPeriodogram followed my a modification of the TOS CorrelationCylinderIndicator that uses nested fold loops to make it adapatble to changes of dominant phase period. The functions that the nested loops are implementing are shown next to them as comments. Unclear that making this indicator adaptive adds any value. However, the nested fold loops may be useful as examples.

CODE:
The nested fold loops are in the second section, the one titled, "Makes the CorrelationCylinderIndicator Adaptable"

The first nested fold loop starts with, "def sy1 = fold i1 = 0"...

HTML:
#===============================================================================
# AutoCorrelationPeriodogram for Dominant Cycle Period
#
# Charles Schwab & Co. (c) 2008-2025
#

input lag = 48;

def x = EhlersRoofingFilter("cutoff length" = 8, "roof cutoff length" = 48);
def cosinePart = fold i = 3 to 48 with cosPart do cosPart + (3 * (x * GetValue(x, i) + GetValue(x, 1) * GetValue(x, i + 1) + GetValue(x, 2) * GetValue(x, i + 2)) - (x + GetValue(x, 1) + GetValue(x, 2)) * (GetValue(x, i) + GetValue(x, i + 1) + GetValue(x, i + 2))) / Sqrt((3 * (x * x + GetValue(x, 1) * GetValue(x, 1) + GetValue(x, 2) * GetValue(x, 2)) - Sqr(x + GetValue(x, 1) + GetValue(x, 2))) * (3 * (GetValue(x, i) * GetValue(x, i) +  GetValue(x, i + 1) * GetValue(x, i + 1) + GetValue(x, i + 2) * GetValue(x, i + 2)) - Sqr(GetValue(x, i) + GetValue(x, i + 1) + GetValue(x, i + 2)))) * Cos(2 * Double.Pi * i / lag);
def sinePart = fold j = 3 to 48 with sinPart do sinPart + (3 * (x * GetValue(x, j) + GetValue(x, 1) * GetValue(x, j + 1) + GetValue(x, 2) * GetValue(x, j + 2)) - (x + GetValue(x, 1) + GetValue(x, 2)) * (GetValue(x, j) + GetValue(x, j + 1) + GetValue(x, j + 2))) / Sqrt((3 * (x * x + GetValue(x, 1) * GetValue(x, 1) + GetValue(x, 2) * GetValue(x, 2)) - Sqr(x + GetValue(x, 1) + GetValue(x, 2))) * (3 * (GetValue(x, j) * GetValue(x, j) +  GetValue(x, j + 1) * GetValue(x, j + 1) + GetValue(x, j + 2) * GetValue(x, j + 2)) - Sqr(GetValue(x, j) + GetValue(x, j + 1) + GetValue(x, j + 2)))) * Sin(2 * Double.Pi * j / lag);
def sqSum = Sqr(cosinePart) + Sqr(sinePart);

def Periodogram = ExpAverage(sqSum, 9);

#================================================================================
# Makes the CorrelationCylinderIndicator Adaptable
#
# Charles Schwab & Co. (c) 2008-2025
#
# Modified by implementing nested folds to enable adaptable indicator
def length = Periodogram;
def limit = if IsNaN(length) then limit[1] else Floor(length); #ensures length is not NaN and rounds it down to nearest integer

def sx = fold ii = 0 to limit with S do S + GetValue(close, ii); #Sum(close, length);
def sxx = fold jj = 0 to limit with SS do SS + GetValue(Sqr(close), jj); #Sum(Sqr(close), length);
#def sy1 = fold i1 = 0 to length with s1 do (s1 + Cos(2 * Double.Pi * i1 / length));
def sy1 = fold i1 = 0 to limit with s1 do (s1 + fold loop21 = 0 to 1 with c11 do c11 + COS(2 * Double.Pi * i1 / limit));
#def sxy1 = fold i2 = 0 to length with s2 do (s2 + GetValue(close, i2) * Cos(2 * Double.Pi * i2 / length));
def sxy1 = fold i2 = 0 to limit with s2 do (s2 + fold loop22 = 0 to 1 with c12 do c12 + GetValue(close, i2) * Cos(2 * Double.Pi * i2 / limit));
#def syy1 = fold i3 = 0 to length with s3 do (s3 + Sqr(Cos(2 * Double.Pi * i3 / length)));
def syy1 = fold i3 = 0 to limit with s3 do (s3 + fold loop23 = 0 to 1 with c13 do c13 + Sqr(Cos(2 * Double.Pi * i3 / limit)));

def sxx_sx = length * sxx - Sqr(sx);

def corrCosine = if sxx_sx == 0 then 0 else (limit * sxy1 - sx * sy1) / Sqrt(sxx_sx * (limit * syy1 - Sqr(sy1)));

#def sy2 = fold j1 = 0 to limit with t1 do (t1 - Sin(2 * Double.Pi * t1 / limit));
def sy2 = fold j1 = 0 to limit with t1 do (t1 - fold loop24 = 0 to 1 with c14 do c14 + Sin(2 * Double.Pi * t1 / limit));
#def sxy2 = fold j2 = 0 to limit with t2 do (t2 - GetValue(close, j2) * Sin(2 * Double.Pi * j2 / limit));
def sxy2 = fold j2 = 0 to limit with t2 do (t2 - fold loop25 = 0 to 1 with c15 do c15 + GetValue(close, j2) * Sin(2 * Double.Pi * j2 / limit));
#def syy2 = fold j3 = 0 to limit with t3 do (t3 + Sqr(Sin(2 * Double.Pi * j3 / limit)));
def syy2 = fold j3 = 0 to limit with t3 do (t3 + fold loop26 = 0 to 1 with c16 do c16 + Sqr(Sin(2 * Double.Pi * j3 / limit)));

def corrNegSine = if sxx_sx == 0 then 0 else (limit * sxy2 - sx * sy2) / Sqrt(sxx_sx * (limit * syy2 - Sqr(sy2)));

plot CorrelationWithCosine = corrCosine;
plot CorrelationWithNegativeSine = corrNegSine;
plot ZeroLine = 0;

CorrelationWithCosine.SetDefaultColor(GetColor(5));
CorrelationWithNegativeSine.SetDefaultColor(GetColor(6));
ZeroLine.SetDefaultColor(GetColor(7));

Hope this helps,

Sesqui
 
SUMMING UP THE ISSUE:
The coding issue I encountered was I could not pass the product of a variable and the fold index counter to function COS() inside a fold loop. I tried creating a script COS_adapatible() that would take as input parameters the Periodogram (rounded down) and the fold loop index counter. But thinkscript would not accept passing the fold loop counter to the script. As a result, a nested fold loop was implemented that computed the COS() value summation, which resolved the issue.

The code below provides a copy paste of the TOS AutoCorrelationPeriodogram followed my a modification of the TOS CorrelationCylinderIndicator that uses nested fold loops to make it adapatble to changes of dominant phase period. The functions that the nested loops are implementing are shown next to them as comments. Unclear that making this indicator adaptive adds any value. However, the nested fold loops may be useful as examples.

CODE:
The nested fold loops are in the second section, the one titled, "Makes the CorrelationCylinderIndicator Adaptable"

The first nested fold loop starts with, "def sy1 = fold i1 = 0"...

HTML:
#===============================================================================
# AutoCorrelationPeriodogram for Dominant Cycle Period
#
# Charles Schwab & Co. (c) 2008-2025
#

input lag = 48;

def x = EhlersRoofingFilter("cutoff length" = 8, "roof cutoff length" = 48);
def cosinePart = fold i = 3 to 48 with cosPart do cosPart + (3 * (x * GetValue(x, i) + GetValue(x, 1) * GetValue(x, i + 1) + GetValue(x, 2) * GetValue(x, i + 2)) - (x + GetValue(x, 1) + GetValue(x, 2)) * (GetValue(x, i) + GetValue(x, i + 1) + GetValue(x, i + 2))) / Sqrt((3 * (x * x + GetValue(x, 1) * GetValue(x, 1) + GetValue(x, 2) * GetValue(x, 2)) - Sqr(x + GetValue(x, 1) + GetValue(x, 2))) * (3 * (GetValue(x, i) * GetValue(x, i) +  GetValue(x, i + 1) * GetValue(x, i + 1) + GetValue(x, i + 2) * GetValue(x, i + 2)) - Sqr(GetValue(x, i) + GetValue(x, i + 1) + GetValue(x, i + 2)))) * Cos(2 * Double.Pi * i / lag);
def sinePart = fold j = 3 to 48 with sinPart do sinPart + (3 * (x * GetValue(x, j) + GetValue(x, 1) * GetValue(x, j + 1) + GetValue(x, 2) * GetValue(x, j + 2)) - (x + GetValue(x, 1) + GetValue(x, 2)) * (GetValue(x, j) + GetValue(x, j + 1) + GetValue(x, j + 2))) / Sqrt((3 * (x * x + GetValue(x, 1) * GetValue(x, 1) + GetValue(x, 2) * GetValue(x, 2)) - Sqr(x + GetValue(x, 1) + GetValue(x, 2))) * (3 * (GetValue(x, j) * GetValue(x, j) +  GetValue(x, j + 1) * GetValue(x, j + 1) + GetValue(x, j + 2) * GetValue(x, j + 2)) - Sqr(GetValue(x, j) + GetValue(x, j + 1) + GetValue(x, j + 2)))) * Sin(2 * Double.Pi * j / lag);
def sqSum = Sqr(cosinePart) + Sqr(sinePart);

def Periodogram = ExpAverage(sqSum, 9);

#================================================================================
# Makes the CorrelationCylinderIndicator Adaptable
#
# Charles Schwab & Co. (c) 2008-2025
#
# Modified by implementing nested folds to enable adaptable indicator
def length = Periodogram;
def limit = if IsNaN(length) then limit[1] else Floor(length); #ensures length is not NaN and rounds it down to nearest integer

def sx = fold ii = 0 to limit with S do S + GetValue(close, ii); #Sum(close, length);
def sxx = fold jj = 0 to limit with SS do SS + GetValue(Sqr(close), jj); #Sum(Sqr(close), length);
#def sy1 = fold i1 = 0 to length with s1 do (s1 + Cos(2 * Double.Pi * i1 / length));
def sy1 = fold i1 = 0 to limit with s1 do (s1 + fold loop21 = 0 to 1 with c11 do c11 + COS(2 * Double.Pi * i1 / limit));
#def sxy1 = fold i2 = 0 to length with s2 do (s2 + GetValue(close, i2) * Cos(2 * Double.Pi * i2 / length));
def sxy1 = fold i2 = 0 to limit with s2 do (s2 + fold loop22 = 0 to 1 with c12 do c12 + GetValue(close, i2) * Cos(2 * Double.Pi * i2 / limit));
#def syy1 = fold i3 = 0 to length with s3 do (s3 + Sqr(Cos(2 * Double.Pi * i3 / length)));
def syy1 = fold i3 = 0 to limit with s3 do (s3 + fold loop23 = 0 to 1 with c13 do c13 + Sqr(Cos(2 * Double.Pi * i3 / limit)));

def sxx_sx = length * sxx - Sqr(sx);

def corrCosine = if sxx_sx == 0 then 0 else (limit * sxy1 - sx * sy1) / Sqrt(sxx_sx * (limit * syy1 - Sqr(sy1)));

#def sy2 = fold j1 = 0 to limit with t1 do (t1 - Sin(2 * Double.Pi * t1 / limit));
def sy2 = fold j1 = 0 to limit with t1 do (t1 - fold loop24 = 0 to 1 with c14 do c14 + Sin(2 * Double.Pi * t1 / limit));
#def sxy2 = fold j2 = 0 to limit with t2 do (t2 - GetValue(close, j2) * Sin(2 * Double.Pi * j2 / limit));
def sxy2 = fold j2 = 0 to limit with t2 do (t2 - fold loop25 = 0 to 1 with c15 do c15 + GetValue(close, j2) * Sin(2 * Double.Pi * j2 / limit));
#def syy2 = fold j3 = 0 to limit with t3 do (t3 + Sqr(Sin(2 * Double.Pi * j3 / limit)));
def syy2 = fold j3 = 0 to limit with t3 do (t3 + fold loop26 = 0 to 1 with c16 do c16 + Sqr(Sin(2 * Double.Pi * j3 / limit)));

def corrNegSine = if sxx_sx == 0 then 0 else (limit * sxy2 - sx * sy2) / Sqrt(sxx_sx * (limit * syy2 - Sqr(sy2)));

plot CorrelationWithCosine = corrCosine;
plot CorrelationWithNegativeSine = corrNegSine;
plot ZeroLine = 0;

CorrelationWithCosine.SetDefaultColor(GetColor(5));
CorrelationWithNegativeSine.SetDefaultColor(GetColor(6));
ZeroLine.SetDefaultColor(GetColor(7));

Hope this helps,

Sesqui
thanks for posting.
i don't understand what this is telling you? i loaded several symbols and stared at several charts and these lines and didnt see anything consistant.
i added declare lower to it in my TOS

your nested loops are not doing anything. they loop 0 to 1. but loops dont evaluate the last number, so 1 is not used. so only 0 is used. so the 2nd fold could be removed, or increase the loop range.
 
@halcyonguy Thanks for looking deeper into this. I think you are on to something. I did some more tests and I believe the issue that I originally had was due to my original script not having the following line it it:

def limit = if IsNaN(length) then limit[1] else Floor(length);

Not having that was forcing the results to NaN or some such. I mistakenly concluded that I could not pass the fold loop index to the trig functions, not being familiar yet enough programming with fold.

The following code removes the nested folds and successfully passes the fold loop index to the trig functions as well as the variable value. It runs and I believe gets the correct results (more testing needed). However, again it is not evident that making this indicator adaptable provides any value.

Thanks for reviewing the code,

Sesqui

HTML:
#===============================================================================
# AutoCorrelationPeriodogram for Dominant Cycle Period
#
# Charles Schwab & Co. (c) 2008-2025
#

input lag = 48;

def x = EhlersRoofingFilter("cutoff length" = 8, "roof cutoff length" = 48);
def cosinePart = fold i = 3 to 48 with cosPart do cosPart + (3 * (x * GetValue(x, i) + GetValue(x, 1) * GetValue(x, i + 1) + GetValue(x, 2) * GetValue(x, i + 2)) - (x + GetValue(x, 1) + GetValue(x, 2)) * (GetValue(x, i) + GetValue(x, i + 1) + GetValue(x, i + 2))) / Sqrt((3 * (x * x + GetValue(x, 1) * GetValue(x, 1) + GetValue(x, 2) * GetValue(x, 2)) - Sqr(x + GetValue(x, 1) + GetValue(x, 2))) * (3 * (GetValue(x, i) * GetValue(x, i) +  GetValue(x, i + 1) * GetValue(x, i + 1) + GetValue(x, i + 2) * GetValue(x, i + 2)) - Sqr(GetValue(x, i) + GetValue(x, i + 1) + GetValue(x, i + 2)))) * Cos(2 * Double.Pi * i / lag);
def sinePart = fold j = 3 to 48 with sinPart do sinPart + (3 * (x * GetValue(x, j) + GetValue(x, 1) * GetValue(x, j + 1) + GetValue(x, 2) * GetValue(x, j + 2)) - (x + GetValue(x, 1) + GetValue(x, 2)) * (GetValue(x, j) + GetValue(x, j + 1) + GetValue(x, j + 2))) / Sqrt((3 * (x * x + GetValue(x, 1) * GetValue(x, 1) + GetValue(x, 2) * GetValue(x, 2)) - Sqr(x + GetValue(x, 1) + GetValue(x, 2))) * (3 * (GetValue(x, j) * GetValue(x, j) +  GetValue(x, j + 1) * GetValue(x, j + 1) + GetValue(x, j + 2) * GetValue(x, j + 2)) - Sqr(GetValue(x, j) + GetValue(x, j + 1) + GetValue(x, j + 2)))) * Sin(2 * Double.Pi * j / lag);
def sqSum = Sqr(cosinePart) + Sqr(sinePart);

def Periodogram = ExpAverage(sqSum, 9);

#================================================================================
# Makes the CorrelationCylinderIndicator Adaptable
#
# Charles Schwab & Co. (c) 2008-2025
#
# Modified by implementing nested folds to enable adaptable indicator
def length = Periodogram;
def limit = if IsNaN(length) then limit[1] else Floor(length); #ensures length is not NaN and rounds it down to nearest integer
def LL = 10;
def sx = fold ii = 0 to limit with S do S + GetValue(close, ii); #Sum(close, length);
def sxx = fold jj = 0 to limit with SS do SS + GetValue(Sqr(close), jj); #Sum(Sqr(close), length);
#def sy1 = fold i1 = 0 to LL with s1 do (s1 + Cos(2 * Double.Pi * i1 / LL));
def sy1 = fold i1 = 0 to limit with s1 do (s1 + Cos(2 * Double.Pi * i1 / limit));
#def sy1 = fold i1 = 0 to limit with s1 do (s1 + fold loop21 = 0 to 1 with c11 do c11 + COS(2 * Double.Pi * i1 / limit));
def sxy1 = fold i2 = 0 to limit with s2 do (s2 + GetValue(close, i2) * Cos(2 * Double.Pi * i2 / limit));
#def sxy1 = fold i2 = 0 to limit with s2 do (s2 + fold loop22 = 0 to 1 with c12 do c12 + GetValue(close, i2) * Cos(2 * Double.Pi * i2 / limit));
def syy1 = fold i3 = 0 to limit with s3 do (s3 + Sqr(Cos(2 * Double.Pi * i3 / limit)));
#def syy1 = fold i3 = 0 to limit with s3 do (s3 + fold loop23 = 0 to 1 with c13 do c13 + Sqr(Cos(2 * Double.Pi * i3 / limit)));

def sxx_sx = length * sxx - Sqr(sx);

def corrCosine = if sxx_sx == 0 then 0 else (limit * sxy1 - sx * sy1) / Sqrt(sxx_sx * (limit * syy1 - Sqr(sy1)));

def sy2 = fold j1 = 0 to limit with t1 do (t1 - Sin(2 * Double.Pi * t1 / limit));
#def sy2 = fold j1 = 0 to limit with t1 do (t1 - fold loop24 = 0 to 1 with c14 do c14 + Sin(2 * Double.Pi * t1 / limit));
def sxy2 = fold j2 = 0 to limit with t2 do (t2 - GetValue(close, j2) * Sin(2 * Double.Pi * j2 / limit));
#def sxy2 = fold j2 = 0 to limit with t2 do (t2 - fold loop25 = 0 to 1 with c15 do c15 + GetValue(close, j2) * Sin(2 * Double.Pi * j2 / limit));
def syy2 = fold j3 = 0 to limit with t3 do (t3 + Sqr(Sin(2 * Double.Pi * j3 / limit)));
#def syy2 = fold j3 = 0 to limit with t3 do (t3 + fold loop26 = 0 to 1 with c16 do c16 + Sqr(Sin(2 * Double.Pi * j3 / limit)));

def corrNegSine = if sxx_sx == 0 then 0 else (limit * sxy2 - sx * sy2) / Sqrt(sxx_sx * (limit * syy2 - Sqr(sy2)));

plot CorrelationWithCosine = corrCosine;
plot CorrelationWithNegativeSine = corrNegSine;
plot ZeroLine = 0;

CorrelationWithCosine.SetDefaultColor(GetColor(5));
CorrelationWithNegativeSine.SetDefaultColor(GetColor(6));
ZeroLine.SetDefaultColor(GetColor(7));
 

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