The function expected an Array here, but found a STRING

Hello Everyone,
For sending order to my broker, it requires a unique Token number for each ticker. I can assign token number at start of afl for the tickers as shown in below afl.

ACC29JUL21FUT = 53184; //Assigned Token Number to ACC29JUL21FUT
BEL29JUL21FUT = 53205; //Assigned Token Number to BEL29JUL21FUT (Just added to show as Token number will be assigned to more than 100 tickers here.)

_SECTION_BEGIN("Angel_Token");

Sym_AB= Name(); //Ticker in Amibroker database (In this example Name() = ACC-I)
Sym = StrTrim( Sym_AB, "-I", 2); //Ticker in Broker terminal
Sym_Fut = Sym+"29JUL21FUT"; //Future Ticker

Filter = 1;

AddtextColumn(Sym_AB,"Ticker in AB   ");//Ticker in Amibroker database
AddtextColumn(Sym,"Symbol    "); //Ticker in Broker terminal
AddtextColumn(Sym_Fut,"Future Symbol         ");//Future Ticker

AddColumn(Sym_Fut,"Token Assigned",1); //This should also return value 53184(Assigned in first line of code) because Sym_Fut = ACC29JUL21FUT, but it is showing error "The function expected an Array here, but found a STRING." 

_SECTION_END();

In the last line of above code I want to get the value 53184. Have I missed anything in above code?
What function can I use to return the value of token number which is 53184.?
Thank you.

AddtextColumn(Sym_Fut,"Future Symbol         ");

As the error states, you are using a function that needs an array and not a string. Use the AddTextColumn to get a string.

1 Like

AddColumn is for numeric output not for Strings.
For String you have to use AddTextColumn.

Filter = status("lastbarinrange");
AddColumn(C, "THIS IS ARRAY", 1.2);
AddColumn(1000, "THIS IS NUMBER", 1.2);
AddTextColumn("Hello World", "This is STRING");

If you want to output value then e.g.

ACC29JUL21FUT = 53184; //Assigned Token Number to ACC29JUL21FUT
BEL29JUL21FUT = 53205; //Assigned Token Number to BEL29JUL21FUT (Just added to show as Token number will be assigned to more than 100 tickers here.)

if ( Name() == "ACC-I" )
	token = ACC29JUL21FUT;
//else if ( Name() == "...." )
//	token = ...;
else	
	token = Null;

// Or use switch statement instead of if-else
	
AddColumn(token,"Token Assigned",1); 
1 Like

Thank you everyone.
I found the solution. With the following code, I got the desired result:

StaticVarSetText("ACC29JUL21FUT","53184"); //Assigned Token Number to ACC29JUL21FUT
StaticVarSetText("BEL29JUL21FUT","53205"); //Assigned Token Number to BEL29JUL21FUT (Just added to show as Token number will be assigned to more than 100 tickers here.)

_SECTION_BEGIN("Angel_Token");

Sym_AB= Name(); //Ticker in Amibroker database (In this example Name() = ACC-I)
Sym = StrTrim( Sym_AB, "-I", 2); //Ticker in Broker terminal
Sym_Fut = Sym+"29JUL21FUT"; //Future Ticker of Symbol

Filter = 1;

AddtextColumn(Sym_AB,"Ticker in AB   ");//Ticker in Amibroker database
AddtextColumn(Sym,"Symbol    "); //Ticker in Broker terminal
AddtextColumn(Sym_Fut,"Future Symbol         ");//Future Ticker of Symbol

AddtextColumn(StaticVarGetText(Sym_Fut),"Token Assigned",1); //With this I am getting the 53184.

_SECTION_END();

That is not a solution but total non sense.
You do not need Static vars in same code.
What a waste.

Everything is dependent on ticker symbol so you can just use Name() function and save lots of code lines and functions.

And again you do not need static vars.

VarSetText("ACC-I","53184"); //Assigned Token Number to ACC29JUL21FUT
VarSetText("BEL-I","53205"); //Assigned Token Number to BEL29JUL21FUT (Just added to show as Token number will be assigned to more than 100 tickers here.)

_SECTION_BEGIN("Angel_Token");

Sym_AB= Name(); //Ticker in Amibroker database (In this example Name() = ACC-I)

Filter = 1;

AddtextColumn(Sym_AB,"Ticker in AB   ");//Ticker in Amibroker database
AddtextColumn(VarGetText(Name()),"Token Assigned (...29JUL21FUT)",1); //With this I am getting the 53184.
1 Like

Thank you very much. Your solution is working perfectly.
I am not an expert in coding. Can you please let me know, why we should not use StaticVarSetText. What are the disadvantages of using StaticVarSetText?

Thanks.

Mainly because it uses up precious memory unnecessarily (when StaticVarSetText is not required).

Static variables in AFL have TWO purposes.

Purpose 1: to keep STATE data between executions of the formula
Purpose 2: to SHARE data between formulas

All other uses are incorrect. Because of their per-process global characteristics, they are:

  • slower than regular variables
  • visible from other formulas (so you can have undesired side effect if more than one formula uses same static variable name)
  • require get/set function calls instead of plain assignments
  • consume RAM for as long as AmiBroker is open (also between formula executions)

So you SHOULD NOT use static variables, unless you have good reason to do so (see purpose 1 and/or 2)

1 Like

Thank you very much .

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.