Puzzled by NumToStr function result

I used the NumToStr function on the Buy, Sell, Short and Cover array variables in my code, expecting to get four corresponding one-char strings variables... sBuy, sSell, sShort and sCover. When I viewed the 8 variables in an Exploration, sBuy & sCover are always equal to "1", and sSell & sCover are always equal to "0" regardless of what values Buy, Sell, Short and Cover may have. My goal is to be able to utilize flow control using if/then/else with the string variables such as...
if sBuy is "1" then execute an IBController function
else... don't execute IBController function

I find the same unexpected behavior with WriteVal and WriteIf. In contrast, when I assign values with the IIF function I get the expected outcome and have no problems; however, I'd like to end up with string values and IIf returns arrays.

I'm probably stuck in dumb mode, so apologies in advance.

Some example AFL code here to illustrate:

MALength1 = 2;
MALength2 = 3;
sBuy = Buy = Cover = Cross(ema( C, MALength1 ),ema( C, MALength2 ));
Sell = Short = Cross( ema( C, MALength2 ), ema( C, MALength1 ));

sBuy = NumToStr(Buy, 1.0);
sSell = NumToStr(Sell, 1.0);
sShort = NumToStr(Short, 1.0);
sCover = NumToStr(Cover, 1.0);

Filter = 1;
AddColumn(Buy, "Buy");
AddColumn(Sell, "Sell");
AddColumn(Short, "Short");
AddColumn(Cover, "Cover");
AddTextColumn(sBuy, "sBuy");
AddTextColumn(sSell, "sSell");
AddTextColumn(sShort, "sShort");
AddTextColumn(sCover, "sCover");

sBuy = IIf(Buy, 1, 0);
sSell = IIf(Sell, 1, 0); 
sShort = IIf(Short, 1, 0);
sCover = IIf(Cover, 1, 0);
AddColumn(sBuy, "sBuy");
AddColumn(sSell, "sSell");
AddColumn(sShort, "sShort");
AddColumn(sCover, "sCover");

Exploration output here:

Correction... the third line is:

Buy = Cover = Cross(ema( C, MALength1 ),ema( C, MALength2 ));

@mcescher you should carefully read the documentation for WriteVal(), in particular, the first "Comment" by @Tomasz

WriteVal always returns one value of the array
(not arrays of values). In almost all cases this is LastValue
of the array, but in indicators, it is "selected value" -
the one that is selected by the vertical line.

Same behaviour for the equivalent NumToStr():

NumToStr is a synonym for WriteVal function and NumToStr is preferred in new coding.

So these functions may not be the appropriate ones to use in your case.

I suggest reviewing the examples in the IBController Read.me to see some different ways to trigger your IB orders.

I also advise taking a look at the GetTradingInterface() function where you'll see a list of formulas from the AFL Library that may provide you additional approaches to use the OLE object to implement your automatic trading systems.

Additional info and examples also in the (unofficial) Amibroker Users' KB.

1 Like

Dear All,
I opened another similar thread but it was closed by the Admin as he regards my post is similar to this. I still could not figure out a solution after reading all the suggested reading.

So I wish to seek additional advice here. Let's say

list = "ticker1,ticker2";
nth_symbol = StrExtract( list, temp);

At this point temp is an array so StrExtract does not function as designed. I tried to use various methods to convert "temp" from array to number using for loop like

for (i=0;i<BarCount;i++){
if (O[i]>C[i])

However, from the result of
nth_symbol = StrExtract(list,StaticVarGet("temp"+BarIndex()));
I get ticker2 only for all bars.

Somewhere in the process the last value was selected for temp. I could not select the ticker from the list by the condition O[i]>C[i].

I would be grateful if you guys can provide me with more advice/readings to help me figure out a solution.

Thank you very much.


Sorry, but (speaking from my POV) you will have bad chances on getting further response/help on your code issue(s) as long as you do not follow simple rules on how to use this site. In your previous thread you were clearly and politely asked to read and to apply them. It is impossible to overlook it.

So either you do not bother to read it or you do not bother to follow advises.
If that should be the case why shall others bother to provide further inside?

Think about it.

1 Like

I read and read genuinely carefully indeed.I hope you understand that not all users are as technically competent as you do.

I have clearly learned from this forum int he past years, especially to follow-up questions, the users who asked questions will get 'educated' in some ways. I expected that coming too. You have to know, in many occasions, we simply cannot distinguish whether our questions are ignorant questions or one that really worth your attention. As a result of these, I'm afraid to ask.

Oftentimes, from reading the suggested readings I was unable to find a way out. It is not like I did not try. I tried every hence I could learned from all the cross references from the suggested readings to create a work around but failed. That was the reason we asked again. Then, we are often getting responses like 'it was answered' or 'you were politely asked to read'. If one could figure it out the solution from the first round of responses, why would we ask the same question again in a different way?


I was not talking about your code issue but was specifically talking about adding code tags to forum posts if posting code as explained in Tomasz's thread.

For that you do not need an programming experience but just have to watch his GIF animation there.

Have you?

How much more easy and simple can it get? C'mon it is year 2020(!) and you want to say that you use forum editors for the first time in your life? It is difficult to believe that.

Anyway... as for your code issue of your thread.

All you have to do is this:

ticker12 = Foreign("Ticker12", "H");
ticker13 = Foreign("Ticker13", "H");

condition = ROC(C,1)>0;
j1_ticker = IIf(condition,ticker12,ticker13);

Filter = 1;

As response to

I am sorry. I did not know the simple rule you mentioned refers to the code tags.

ticker12 = Foreign("Ticker12", "H");
ticker13 = Foreign("Ticker13", "H");

condition = ROC(C,1)>0;
j1_ticker = IIf(condition,ticker12,ticker13);

Filter = 1;

Thanks a million for the above. I could handle this when I wanted to display the the (High) prices of ticker12 or ticker13....What I could not figure it out (that was what I wanted to do in the first place but I wish I could express myself better) was how to display the ticker names in the AddtextColumn column. For example,

condition = ROC(C,1)>0;
result = WriteIF( condition, "Ticker12", "Ticker13" );
Filter = 1;

As you pointed out previously, Numtostr, WriteVal, WriteIf do 'selected value'. So my whole AddtextColumn column was the same.

I also tried..

list = "Ticker12,Ticker23";
result = StrExtract( list, IIf(ROC(C,1)>0,0,1) );//this is incorrect as this field takes number only


StrExtract did not take arrary...so this was not feasible.

Then I tried things like

for (i=0;i<BarCount;i++)
	if (temp[i]>0)



This still led me to selecting the last matched ticker. So all cells of the AddtextColumn was the same. I thought this was the right approach.

Thank you so much again.

You can do that via AddMultiTextColumn as shown e.g. in this thread

ticker12 = "AAPL";
ticker13 = "MSFT";

price12 = Foreign(ticker12, "H");
price13 = Foreign(ticker13, "H");

condition = ROC(C,1)>0;
j1_ticker = IIf(condition,price12,price13);

Filter = 1;

TextList = ticker13+"\n"+ticker12;
TextSelector = condition;
bg_col = IIf(condition, colorRed, colorOrange);
AddMultiTextColumn( TextSelector, TextList,"Foreign Ticker", 1.2, -1, bg_col );

AddColumn(j1,"j1",1.0, -1, bg_col);
AddColumn(j1_ticker,"Foreign Price",1.2, -1, bg_col);


1 Like