Code:
script totalBarsRequired { # finds the number of bars required to look back to see exactly "lookback" number of green bars
input lookback = 0;
input multiplier = 0;
def period = AggregationPeriod.DAY;
plot barsNeeded;
def totalBars = fold index = 0 to lookback * multiplier with result=0 do
result + if result < lookback * multiplier
then if result <= lookback and GetValue(close(GetSymbol(), period), index) > GetValue(open(GetSymbol(), period), index)
then if result == lookback
then (lookback * index)
else 1
else 0
else 0;
barsNeeded = (totalBars - lookback) / lookback;
}
def lookback = 20; # number of green bars to look at
def lengthMultiplier = 5;
def period = AggregationPeriod.DAY;
def totalBars = totalBarsRequired(lookback, lengthMultiplier); # the total number of bars required to to lookback to see "lookback" number of green bars
script totalWickLength { # do something with every green bar found while looking back the amount totalBarsRequired to see the specified amount of green bars
input numberOfBars = 0;
def period = AggregationPeriod.DAY;
plot totalWicklengths;
def totalWicks = fold index=0 to numberOfBars with result=0 do #this line is the problem
result + if GetValue(close(GetSymbol(), period), index) > GetValue(open(GetSymbol(), period), index)
then 1
else 0;
totalWickLengths = totalWicks;
}
plot avgWick = totalWicklength(totalBars);
avgWick.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgWick.SetDefaultColor(Color.ORANGE);
In the script totalWickLengths I'm attempting to use numberOfBars as the "to" parameter. I am passing the result of the previous script totalBarsRequired into totalWickLength as the numberOfBars parameter.
totalBarsRequired counts the total number of daily bars required to look back to see exactly 20 green bars. I can plot this and is confirmed to be accurate.
If I pass totalBarsRequired into totalWickLength and return it unaltered or perform basic math and return it I get an expected result.
If I use the code above and replace numberOfBars as the to parameter in the fold with a specific number (ex: 42), then I get an expected result.
The problem I am having is when I specifically use the result of the first fold as the "to" parameter in the second fold the code breaks and I receive an N/A result.
Is anyone able to please help explain to me why this is or assist with guiding me towards a potential work around ?
I would really appreciate it and thank you in advance.