Mistake: Buy/Sell not working in If loop


I am trying to use Buy/Sell in an if loop, but I get an error saying array cannot be used in if.
But I am able to use Buy/Sell in Alertif function.

If I just _TRACE the values, I can clearly see they are either 0 or 1.

Could you please suggest what I am doing wrong?


You have to access elements of array. Scroll to the middle of this page.

But before using loop ask yourself whether it is actually required using loop and whether using array processing would be better solution (doing same thing but faster).

In wise foresight I am additionally adding this thread (again) being worth to be read (not just for you).

1 Like

And must read of all time

Also keep in mind that you just need to press F1 key on ANY error that you are getting and you will get relevant help page automatically open and explaining you immediately what mistake you are doing instead of asking people.


Thanks @fxshrat and @Tomasz

I understand now that Buy and Sell are arrays and cannot be used in If statements. But I dont understand how they work with AlertIf, when it also expects a boolean expression. The following statements are working perfectly fine -

AlertIf( Buy, "SOUND C:\Windows\Media\chimes.wav", "Buy alert", 2 );
AlertIf( Sell, "SOUND C:\Windows\Media\chimes.wav", "Sell alert", 3 );

I am basically trying to set the value of URL, based on whether I should call Buy API or Sell API.

I also tried printing the values of the Buy and Sell and I see only 0 and 1s and I still cannot apply arithmetic comparisons (<, > or ==).
Also when I try to print Sell [0] or Buy[0], I keep getting {Empty}, even if one of the prints as 1.

I know I am missing something very basic here. I just need to set the correct URL based on Buy/Sell signal.

Please help. Thanks in advance.

Just an update -
Kind of achieved what I was trying to do, but not sure if its the best way - used Writeif in the following manner -

buysignal=WriteIf (Buy, "YES", "NO");
sellsignal=WriteIf (Sell, "YES", "NO");

AlertIf is the exception from the rule, it accepts ARRAY as first argument as it checks not only last bar but a number of bars as it tries NOT to cause repeated alert if it sees REPEATS on few bars, see "lookback" parameter in the manual: http://www.amibroker.com/guide/afl/alertif.html (it decides how many bars back needs to be checked). So obviously this function must accept array.

You still don't understand fundamental difference between control flow statement (if-else) and FUNCTIONS, like AlertIf, WriteIf and so on.

Functions in AFL usually accept ARRAYS and usually return ARRAYS too. They work on ALL bars in parallel. There are few exceptions that accept ARRAYS but produce single value based on either selected bar value or last bar value.

Functions are completely different than control flow. Functions ALWAYS execute. They may produce different result depending on parameter, but they execute always

if-else statements are totally different. They are NOT functions. They are control taking different and mutually exclusive PATHS in the code. Like you are driving a car and decide to go left or right on crossover. You can only go ONE way, right OR left. You can NOT make decision based on array. You need SINGLE value.

if-else changes the flow of execution and can be used to totally SKIP execution of parts of the code. Since code is executed ONCE, the decision to go this or that way is done ONCE, therefore flow control statements like if require singe TRUE / FALSE value (scalar, NOT array).

If you want to use ARRAY with if-else, you need to select WHICH BAR you mean, using SelectedValue or LastValue. It is very clearly explained in the ERROR MESSAGE that you get when you use if-else incorrectly. You just need to press F1 key when you get the error message.

It is also described in http://www.amibroker.com/guide/a_mistakes.html#IFELSE

And before anything else you need to read (possibly many times)