Exit loop based on array condition

Hi, I'd like to terminate a loop based on an array condition.
Normally I would write something like this:

for (i = 1; i < BarCount; i++)
{
if (Close[i] > Close[i-1])
break;
}

My problem is that I already have extensive loops, and another nested loop would slow processing considerably.
Is there a way to terminate a loop doing something like this:

TestCond = C > Ref(C,-1);
Start = 25;
End = 50;

for (x = Start; x < End; x+5)
{
...

    if (TestCond)
    break;

}

Thanks for your help.

Yes, inner loop can be terminated by break. But your code has an error. x+5 alone does not increase x by 5. You need assignment. Correct statement would be either x = x + 5 or shorter form x += 5

1 Like
TestCond = C > Ref(C,-1);
Start = 25;
End = 50;

for (x = Start; x < End; x = x+5)
{
// ... (additional code)

    if (TestCond)
    break;
}

TestCond is an array, so it won't work with if(). Is there a simple way for AB to break the loop using the array-based TestCond?

Moderator comment: Code tags were missing. Corrected.

Sure there is, read the manual it is all covered there
https://www.amibroker.com/guide/keyword/if.html - explains how to use if() including working with arrays
and
http://www.amibroker.com/guide/a_mistakes.html

(BTW: use code tags as per "how to use this site")

I've read through the manual, and the only solution I found is to use another nested loop or a loop-based function to break the main loop. Calling a [for(i=0;i<BarCount;i++)] loop at every iteration of the main loop is very inefficient. There must be a way to break a loop with an array-based condition...

if TestCondition is an array inside loop you need to use TestCondition[x]

How to use TestCond[x] with using a loop, is this possible in AB?


TestCond = C > Ref(C,-1);
Start = 25;
End = 50;

for (x = Start; x < End; x = x+5)
{
// ... (additional code)

    if (TestCond)
    break;
}

Read this


TestCond = C > Ref(C,-1);
Start = 25;
End = 50;

for (x = Start; x < End; x = x+5)
{
// ... (additional code)

    if (TestCond[x])
    break;
}

The manual goes into details and gives source code that includes the answer to the very question you asked - at the bottom of the page. What's more you did not even read the @awilson answer (single line) - the one where he just wrote you:

if TestCondition is an array inside loop you need to use TestCondition[x]

What can be simpler than that?

Unfortunately "x" in my loop doesn't match bar Nrs, so TestCond[25/30/35/40/45/50] would not match data bars in my case.
I've read through the manual, and only found [for(i=0;i<BarCount;i++)] loop solutions. If you have a solution, please post it here.

Post full code of BOTH (nested) loops that you wrote because this exchange without FULL code goes nowhere.

There are no nested loops - only one simple loop that needs to break on an array-based condition. Full code posted above.

Change your logic or make it match

Are you just trying to determine whether your array based condition was true for any bar, without looping through the bars? If so, you could do something like this:

TestCond = C > Ref(C,-1);
if (LastValue(Cum(TestCond) > 0)
    break;

Of course, in this simple example you will always break from the loop immediately, unless the Close price never went up, which seems highly unlikely.

Thanks @mradtke - you appear to be the only one here who understands the problem.

Loops in other languages are not as limited as AFL. For example, in Python one can break a loop based on a MA crossover, without any need to convert the data array to static values.

You are simply wrong in your statements about AFL.

Loops in AFL work and look exactly the same as in C, C++, Java, JavaScript and the only limited thing is your understanding of AFL (or lack of). Python is no better than AFL. Just the opposite.

What you don't understand is the fact that AFL is superior because it offers scalar processing (like all other languages) AND native vector processing making it 100x faster than python when processing financial price series. Read this: http://www.amibroker.com/guide/h_understandafl.html and this: http://www.amibroker.com/kb/2008/08/12/afl-execution-speed/

In 90% of cases when you need loops in Python, you can write a simple native vector (array) statement in AFL which runs at the assembly speed (hyper fast). You need to un-learn slow ways (loops) and learn new ways (array/vector) to write compact and fast formulas.

Newbie like you should rather start with describing his goal in plain English than trying to write half-baked (incorrect) code.

There is no slight description of what your code is SUPPOSED to do. And your code does not tell that either. In the code that you posted the for loop and the if-statement is nonsense because TestCond is loop-invariant (does not depend on loop counter).
This could change if you provided skipped part (//additional code part). Because of that no-one really knows what in your mind you really wanted to write. For this reason it is better to describe the goal in plain English. Or write full formula as I suggested earlier (without skipping // additional code).

5 Likes

@ojacopo your posts above show, that your knowledge regarding AmiBroker and programming in AFL is very limited, so please restrain yourself from expressing such groundless and unfair opinions... It's not a good idea to criticize in your first posts on this forum, something that you clearly don't understand! In a similar way, you could criticize Boeing or Airbus for making such bad planes just because you can't fly them :wink:

I can only imagine how frustrating it is for Tomasz, to defend himself (for the n-th time ) against such ridiculus accusations :tired_face:

3 Likes