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:
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
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...
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]
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.
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.
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).
@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
I can only imagine how frustrating it is for Tomasz, to defend himself (for the n-th time ) against such ridiculus accusations