FOLD Statement with Variable

kiwi

New member
VIP
The code below helps to explain the question. It appears the FOLD Statement cannot accept an input that changes as a variable. The variable is len. If a fixed value is used, as in len=10, the code will plot a moving average, if the "len" value is allowed to adjust, as defined by the code, no plot is indicated.

Is there a work around for this. I've searched the site, but did not find an answer.

Thanks for any guidance that can point me in a direction with working with the FOLD statement.

############################################
Original Code from Tradingview:

//@version=5
// Copyright (c) 2019-present, Franklin Moormann (cheatcountry)
// Ultimate Moving Average [CC+RedK] script may be freely distributed under the MIT license.
indicator('Ultimate Moving Average [CC+RedK]', max_bars_back=3000, overlay=true)

inp = input(title='Source', defval=close)
res = input.timeframe(title='Resolution', defval='')
rep = input(title='Allow Repainting?', defval=true)
bar = input(title='Allow Bar Color Change?', defval=true)
src = request.security(syminfo.tickerid, res, inp[rep ? 0 : barstate.isrealtime ? 1 : 0])[rep ? 0 : barstate.isrealtime ? 0 : 1]
acc = input.float(title='Accelerator', defval=1.0, minval=1.0, step=0.01)
minLength = input.int(title='MinLength', defval=5, minval=1)
maxLength = input.int(title='MaxLength', defval=50, minval=1)
smoothLength = input.int(title='SmoothLength', defval=4, minval=1)

// We are checking current volatility to see how high it is and adjust accordingly.
// If volatility is very high then we use the minLength and vice versa
// Reason behind this is to hug price closely during high volatility but lag for low volatility
mean = ta.sma(src, maxLength)
stdDev = ta.stdev(src, maxLength)
a = mean - 1.75 * stdDev
b = mean - 0.25 * stdDev
c = mean + 0.25 * stdDev
d = mean + 1.75 * stdDev

length = 0.0
length := src >= b and src <= c ? nz(length[1], maxLength) + 1 : src < a or src > d ? nz(length[1], maxLength) - 1 : nz(length[1], maxLength)
length := math.max(math.min(length, maxLength), minLength)
len = math.round(length)

// We are providing a dynamic weight depending on the current momentum
// When momentum is at an extreme for overbought/oversold then we give more weight to the current price
mf = na(volume) or volume == 0 ? 100.0 - 100.0 / (1.0 + src / len) : ta.mfi(src, len)
mfScaled = mf * 2 - 100
p = acc + math.abs(mfScaled) / 25

sum = 0.0
weightSum = 0.0
for i = 0 to len - 1 by 1
weight = math.pow(len - i, p)
sum += src * weight
weightSum += weight
weightSum
uma = ta.wma(sum / weightSum, smoothLength)

slo = src - uma
sig = slo > 0 ? slo > nz(slo[1]) ? 2 : 1 : slo < 0 ? slo < nz(slo[1]) ? -2 : -1 : 0
alertcondition(ta.crossover(sig, 1), 'Strong Buy Signal', 'Strong Bullish Change Detected')
alertcondition(ta.crossunder(sig, -1), 'Strong Sell Signal', 'Strong Bearish Change Detected')
alertcondition(ta.crossover(sig, 0), 'Buy Signal', 'Bullish Change Detected')
alertcondition(ta.crossunder(sig, 0), 'Sell Signal', 'Bearish Change Detected')
umaColor = sig > 1 ? color.green : sig > 0 ? color.lime : sig < -1 ? color.maroon : sig < 0 ? color.red : color.black
barcolor(bar ? umaColor : na)
plot(uma, title='UMA', linewidth=2, color=umaColor)
################################################

My TOS conversion:

input maxLength = 50;
input minLength = 5;
input StDevLength = 50;
input smoothLength=4;
input acc = 1.00; #Hint: min value = 1

def mean = SimpleMovingAvg(close, maxLength);
def dev = StDev(close, StDevLength);
def a = mean - 1.75 * dev ;
def b = mean - 0.25 * dev ;
def c = mean + 0.25 * dev ;
def d = mean + 1.75 * dev;

def length = if IsNaN(length[1]) then maxLength else if close >= b and close <= c then length[1] + 1 else if close < a or close > d then length[1] - 1 else maxLength;

def length1 = Max(Min(length, maxLength), minLength);
def len = Round(length1[1], 0);

####################Money Flow Index
input MFMovAvgLength = 1;
def mfi = if IsNaN(mfi[1]) then Average(MoneyFlow(high, close, low, volume, (maxLength - minLength) / 2), MFMovAvgLength) else 100 - 100 / (1.0 + close / len);
def mfiScaled = mfi * 2 - 100;
def p = acc + AbsValue(mfiScaled) / 25;
############################################


def sum = fold i = 0 to len-1 with s=0 do s + GetValue(close,i) * Power(GetValue(len, -i), p);
def weightSum= fold j=0 to len-1 with t=0 do t + Power(GetValue(len, -j), p);

plot uma=wma(sum/weightSum,smoothLength);
 
@kiwi
The issue you're having is that the value of len is, at some point on your chart, equal to Double.NaN.
This prevented the iteration of the FOLD statement.

KQe61ZS.png


Ruby:
input maxLength = 50;
input minLength = 5;
input StDevLength = 50;
input smoothLength=4;
input acc = 1.00; #Hint: min value = 1

def mean = SimpleMovingAvg(close, maxLength);
def dev = StDev(close, StDevLength);
def a = mean - 1.75 * dev ;
def b = mean - 0.25 * dev ;
def c = mean + 0.25 * dev ;
def d = mean + 1.75 * dev;


def length = if IsNaN(length[1]) then maxLength else if close >= b and close <= c then length[1] + 1 else if close < a or close > d then length[1] - 1 else maxLength;

def length1 = Max(Min(length, maxLength), minLength);
def len = Round(length1[1], 0);
#**************************************************************************************
def foldlen = if IsNaN(close[maxlength]) then foldlen[maxlength] else len[maxlength];#*
#**************************************************************************************

####################Money Flow Index
input MFMovAvgLength = 1;
def mfi = if IsNaN(mfi[1]) then Average(MoneyFlow(high, close, low, volume, (maxLength - minLength) / 2), MFMovAvgLength) else 100 - 100 / (1.0 + close / len);
def mfiScaled = mfi * 2 - 100;
def p = acc + AbsValue(mfiScaled) / 25;
############################################

#*******************************************************************************************************
#def sum = fold i = 0-1 to len-1 with s=0 do s + GetValue(close,i) * Power(GetValue(len, -i), p);      *
#def weightSum= fold j=0-1 to len-1 with t=0 do t + Power(GetValue(len, -j), p);                       *
def sum = fold i = 0-1 to foldlen-1 with s=0 do s + GetValue(close,i) * Power(GetValue(len, -i), p);#  *
def weightSum= fold j=0-1 to foldlen-1 with t=0 do t + Power(GetValue(len, -j), p);#                   *
#*******************************************************************************************************

plot uma=wma(sum/weightSum,smoothLength);
 
In my attempt to understand what could be happening as you describe, if len at some point isNaN, then if I code the following:
plot test=if isNaN(len) then 1 else 0;
then at some point in my chart if len isNaN, then the code would identify it. The test plot, in my case, does not identify that len isNaN.

With your suggestion in mind I modified "def length" line to remove the possibility of isNan. The modified code did not work either:
def length = compoundvalue(maxLength, if close >= b and close <= c then length[1] + 1 else if close < a or close > d then length[1] - 1 else maxLength, close);

The solution you offer, def folden= if IsNaN(close[maxlength] then foldlen[maxlength] else len[maxlength], the lookback values referenced are fixed values that are defined as inputs. This would be similar to defining foldlen=maxlength. If the lookback values are changed to reference "length" the plot fails.

Trying to make sense of what you are suggesting, but with my limited knowledge of ThinkScript, it appears that the FOLD statement only works with fixed length values. If the method(s) I've used above is flawed, please point that out for me. My intention here is to learn, and for others to benefit from our discussion.
 
@kiwi
Your test plot to identify when len = double.nan will not work because it can only test bars where close is not double.nan.
Script exists on all bars of a chart, even ones that do not have a value yet (think expansion area to the right of last close). The FOLD statement has to be able to calculate on these bars too.
If you take your original code and change the expansion area on the right to '0' on the time axis tab in chart settings, then your original code works as is.
foldlen[maxlength] is not similar to foldlen=maxlength.
Foldlen[maxlength] is a value a number bars(where number of bars is equal to maxlength) ago and changes as the chart progresses working as a variable.
 
@kiwi
Your test plot to identify when len = double.nan will not work because it can only test bars where close is not double.nan.
Script exists on all bars of a chart, even ones that do not have a value yet (think expansion area to the right of last close). The FOLD statement has to be able to calculate on these bars too.
If you take your original code and change the expansion area on the right to '0' on the time axis tab in chart settings, then your original code works as is.
foldlen[maxlength] is not similar to foldlen=maxlength.
Foldlen[maxlength] is a value a number bars(where number of bars is equal to maxlength) ago and changes as the chart progresses working as a variable.
Can you explain the logiic in:
def foldlen = if IsNaN(close[maxlength]) then foldlen[maxlength] else len[maxlength];#*
 
The code below helps to explain the question. It appears the FOLD Statement cannot accept an input that changes as a variable. The variable is len. If a fixed value is used, as in len=10, the code will plot a moving average, if the "len" value is allowed to adjust, as defined by the code, no plot is indicated.

Is there a work around for this. I've searched the site, but did not find an answer.

i was browsing and saw this, i don't mean to but in , @Svanoy

@kiwi
..when posting code to be converted, please post a link to the original code.
..please post code inside of a code window. it is the icon at the top of window , </>
..your post has over 4500 characters. instead of expecting people to look at all of that, for it to explain it to them, just ask a question.
like this , 'how do i convert this tradingview code snippet to thinkscript?'
..using 2 code windows would have made this so much easier to understand
 
i was browsing and saw this, i don't mean to but in , @Svanoy

@kiwi
..when posting code to be converted, please post a link to the original code.
..please post code inside of a code window. it is the icon at the top of window , </>
..your post has over 4500 characters. instead of expecting people to look at all of that, for it to explain it to them, just ask a question.
like this , 'how do i convert this tradingview code snippet to thinkscript?'
..using 2 code windows would have made this so much easier to understand
 
i was browsing and saw this, i don't mean to but in , @Svanoy

@kiwi
..when posting code to be converted, please post a link to the original code.
..please post code inside of a code window. it is the icon at the top of window , </>
..your post has over 4500 characters. instead of expecting people to look at all of that, for it to explain it to them, just ask a question.
like this , 'how do i convert this tradingview code snippet to thinkscript?'
..using 2 code windows would have made this so much easier to understand
I'll follow that advice in future posts, thank you.
 
@kiwi
Can you explain the logiic in:
def foldlen = if IsNaN(close[maxlength]) then foldlen[maxlength] else len[maxlength];#*
This line checks if a bar has a close value and provides values for those bars that do not. The FOLD statements in the original script cannot calculate on bars that do not yet have a close because the values that FOLD statement is looking for do not yet exist on those bars.

The plot that is produced from your code trails the last bar by around 47 bars. This cannot be correct. Can you please explain.
Your FOLD statements, as written:
Ruby:
def sum = fold i = 0 to len-1 with s=0 do s + GetValue(close,i) * Power(GetValue(len, -i), p);
def weightSum= fold j=0 to len-1 with t=0 do t + Power(GetValue(len, -j), p);
are looking for values from a number of bars after the bar they are calculating on.
As denoted by the negative offsets of GetValue(len,-i) and GetValue(len,-j).

If there is not a valid bar (bar with a close value) then the FOLD statement cannot calculate and produces no value.

Your original script on a chart without an expansion area produces the same trailing plot:
pZFEr8q.png

Whether or not the plotting is supposed to be trailing or not, I have no idea, That wasn't the original question asked in this thread.


If you set the value of len to a constant of 10 then you will get a plot on every bar because you are negating the negative offset.

Ruby:
Power(GetValue(len, -i), p)
becomes:
Ruby:
Power(10, p)
Because you have already assigned the value of len to 10 on every bar, even bars without a valid close value (expansion area).
 
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
380 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