Infinite Loop Problem with For Loop

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #176391 quote
    virus010
    Participant
    Junior

    Hello,

    i read about the problem with PRT Engine and for in for loops. I have the following indicator and want to run it in my ProScreener.

    Problem: i get the infinite loop error.

    How can i solve this?

    DEFPARAM CALCULATEONLASTBARS = 500
    timeRange=200
    result=0
    searchLeftOffset1=5
    searchLeftOffset2=20
    
    //check if actual close is near highest close in timeRange
    if close > 0.97*highest[timeRange](close) AND close < 1.02*highest[timeRange](close) then
    
    //if so, search for next close within timerange, that meets the same criteria. Start search searchLeftOffset1 Days before
    for z = searchLeftOffset1 to timeRange do
    
    if close[z] > 0.96*highest[timeRange](close) AND close[z] < 1.02*highest[timeRange](close) then
    
    //if second day found, search for next close within timerange, that meets the same criteria. Start search searchLeftOffset2 Days before
    for y = z + searchLeftOffset2  to (timeRange) do
    if close[y] > 0.96*highest[timeRange](close) AND close[y] < 1.02*highest[timeRange](close) then
    result=1
    endif
    
    next
    endif
    
    next
    else
    result=0
    endif
    
    return result

    Thank you

    Best regards,

    Branimir

    #177035 quote
    robertogozzi
    Moderator
    Master

    Too many iterations, almost 40K per candle!

    Try removing line 1 and setting 50K units, to allow enough bars to run.

    It will take quite a long time to calculate!

    I removed line 1, used 10K units and set timerange=50, and it worked!

    virus010 thanked this post
    #177077 quote
    Nicolas
    Keymaster
    Master

    You can try to encapsulate your loop into a condition with “IsLastBarUpdate”, so that the code is only once each time the last bar is updated (new price received), see:

    IsLastBarUpdate

    Speed calculation with IsLastBarUpDate instruction

    virus010 thanked this post
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Infinite Loop Problem with For Loop


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
virus010 @virus010 Participant
Summary

This topic contains 2 replies,
has 3 voices, and was last updated by Nicolas
4 years, 5 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 08/27/2021
Status: Active
Attachments: No files
Logo Logo
Loading...