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);
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);