How to write If current Signal is Buy


#1

How to write

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

Or

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


#2

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

if (Buy)
  Plot( ... );

#3

That returns error 6

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


#4

@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.


#5

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().


#6

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


#7

@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:


#8

@bobptz thank you so very much :slight_smile:


#9

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

Buy = 1;
printf("buy[barcount-1]="+Buy[barcount-1]+"\n");
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.


#10

@bobptz the function (really an operator) that you are looking for is typeof() : https://www.amibroker.com/guide/keyword/typeof.html

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.


#11

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


#12

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

Thank you for teaching me something new tonight!


#13

Take a look at this code:

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

printf("buy[barcount-1]="+Buy[barcount-1]+"\n");
if (Buy)
  Printf( "Hello there\n");
  
printf("2) "+typeof(buy)+"\n");

Output:

1) number
buy[barcount-1]=1
Hello there
2) number

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


#14

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:
http://www.amibroker.com/guide/a_mistakes.html

and

and
http://www.amibroker.com/guide/errors/6.html

and
http://www.amibroker.com/guide/h_understandafl.html

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