Using the result of a fold as the "to" parameter of another fold

DrTrev4

New member
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.
 
Solution
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 -...
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.

maybe this will work.
...use a large number as the loop to number, like 500.
...add a while statement to check
if index < numberOfBars .
keep processing the do formula while it is true, and to stop the loop if it is false.

def big = 500;
def totalWicks = fold index = 0 to big
with result
while (index <= numberOfBars)
do #this line is the problem result
 
Solution

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
336 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