How to write If current Signal is Buy


How to write

If current signal is Buy Then Plot () or if current signal is Sell then Plot()


if current signal is UpArrow then Plot () else if current signal is DownArrow then Plot()


The answer to your question could be as simple as this:

if (Buy)
  Plot( ... );


That returns error 6

I guess need a for statement , which I dont know what it should be


@twin: Please don't take offence, but if you don't understand @bobptz's answer, it is probably because you need to do more work on learning AFL, or explain your issue more clearly.

Also, Where do you get error 6? In the debugger? What is it referring to? BE CLEAR.

I think @bobptz answer was a clear and direct pointer to what you should READ about. "IF" Statement in the Manual is what he showed you. Now your job is to go READ it and then TRY it, then if still not getting it, POST your code and tell us where you have problem.


The built-in variable Buy is an array, and you can't use an array as the condition for an if(). There are lots of examples of using IIf() inside of a plot() statement. Make sure you understand the difference between if() and IIf().


I admit that I sparingly use the BUY variable and I was puzled by your comment. So I tested it.

The following very simple code does work, it does not give eny error messages:

Buy = 1;

if (Buy)
  Printf( "Hello there");

However, this does NOT work:

if (High >1.54)
  Printf( "Hello there");

This DOES work:

if (High[BarCount-1]>1.54)
  Printf( "Hello there");

So what @mradtke about IF and arrays is right. However the BUY seems to be an exception.

WOW! We managed to confuse @twin


@bobptz, when you reassign with your Buy = 1; it appears to become a scalar.

WOW! We managed to confuse @twin

Only the Left one... The other one is RIGHT :rofl:


@bobptz thank you so very much :slight_smile:


No, it still behaves as an array. The following codes still works without error:

Buy = 1;
if (Buy)
  Printf( "Hello there");

PS: I thought there was a built in function that checks if a variable is array or scalar. I could not find it.


@bobptz the function (really an operator) that you are looking for is typeof() :

In your sample code, you did nothing to change the type of Buy from a scalar to an array. I suspect (but @Tomasz can confirm) that when all elements of an array have the same value, then AB will treat it like it's a scalar value. That's why it still works as the conditional for the if().

However, if you assign the Buy array with something that changes from bar to bar, like this:

Buy = Day() == 1;            // Buy on Mondays

Then as soon as you do a code check AmiBroker will report Error 6, because now there is no denying that Buy is an array, not a scalar.


If you assign a constant to variable it becomes scalar (like buy =1). If you assign a condition it becomes an array (Day() == 1).


You @QuantTrader and @mradtke are absolutely correct. I just tested it.

Thank you for teaching me something new tonight!


Take a look at this code:

Buy = 1;
printf("1) "+typeof(buy)+"\n");

if (Buy)
  Printf( "Hello there\n");
printf("2) "+typeof(buy)+"\n");


1) number
Hello there
2) number

BUY is reported as "scalar" even after I have accessed Buy[barcount-1], which definitely implies it is an array.


This has been covered on this forum multiple times. Really searching the forum reveals that most "problems" are already answered. So please use Search facility.
You will find answers QUICKER than when you asking the same questions again.

@twin - highly recommended reading:




and When scalar becomes an array, aka. type handling in AFL