Type error for if expression

Hello,

following expression generates type error in Explorer. It runs without any error with AFL editor. If I check the type of the operands they are all numbers. No arrays at all.

if (PosSize == 0 AND Holding == Name())
{
	Sell = True;
	SellPrice = Close;
}

Capture

Double click on error line and the manual page that will open, i.e. THIS PAGE: http://www.amibroker.com/guide/errors/6.html And that is what you need to read to learn what is wrong with your formula.

Plus this: How do I debug my formula?

And this http://www.amibroker.com/guide/h_understandafl.html

Your PosSize is an ARRAY. Use debugger and it will tell you the type.

Thank you for your guide.

What I am confused here is that PosSize and Holding are not array types they are numbers. I checked their type with typeof().
The code worked fine with AFL editor and debugger but not with Explore.

Furthermore

AddTextColumn(Holding, "Holding", 1.0, 1, ColorGreen); 
AddColumn(PosSize, "PosSize (%) ", 1.0, 1, ColorYellow);

displays numbers and strings correctly in Explore. (Holding is a string type not a number.)

In fact any variable defined as

PosSize     =  StaticVarGet("PosSize"  +  symbol);	

style, I cannot use them inside if-expression. They cause type error in Explore. The Explore identifies them as array types while AFL editor/debugger identify them as scalar types. This is a frustrating situation.

What did you put in that static variable ?

Really don’t argue with the compiler. If compiler tells you that you have an error it means that you have an error. Computers don’t make mistakes.

If program generates an error 6 it means that you used ARRAY.

Your “checks” mislead you. StaticVarGet would give you NULL (which is a NUMBER) if static variable is NOT defined (not present), but it will give you ARRAY if you have written array to static variable..

Really you should FOLLOW ADVICE given previously. Just follow it: How do I debug my formula?

You are doing coding mistake described precisely in the error page I already pointed out to you. PosSize variable is AN ARRAY. Use _TRACE and it will be all clear to you

PosSize     =  StaticVarGet("PosSize"  +  symbol);	
_TRACE( "Type of variable  is " + typeof( PosSize ) ); 

And really, you need to read this: http://www.amibroker.com/guide/h_understandafl.html
That is the most important reading for everyone who wants to write any formula in AFL.

1 Like

Hi Aron and Tomaz,

Thank you for your support. I already read the documents but I’ll read them again more carefully.

Thanks

@ryank,

Sometimes it takes a shift in the thinking to get a good understanding of AmiBroker AFL.

I tell people to think of variables like a Row in Excel, with the dates in the columns.
Your code should only reference Up and to the Left in the excel example.

So consider every variable an array, and hopefully this idea can help with your thinking in your programming.