Loading quotations using FOREIGN() from a file of ticker names

I am trying to create a batch that executes several optimizations in different pairs of symbols. For doing that I need to load tickers from a file. But when I use those tickers to load quotations they are not valid. First line in the file contains "BAC" and second line contains "USB". The code loads the ticker names correctly but not the data:


But if I use the names directly the data loads well:


What I am doing wrong? Is it that FOREIGN() does not accept volatile strings as arguments? Maybe the strings from the file contain additional characters like return, new line, etc? Thanks

file = "C:\\TEMP\\pairs.txt"; 
fh = fopen( file, "r" );

if( fh ) 
    symbol1 = fgets( fh );  
    symbol2 = fgets( fh );      
    fclose( fh );   

//symbol1 = "BAC";
//symbol2 = "USB";

C1 = Foreign(symbol1,"C");
C2 = Foreign(symbol2,"C");

@Armin_Tamzarian as you suspected:

if( fh ) 
    symbol1 = StrTrim(fgets( fh ), "\n");
    symbol2 = StrTrim(fgets( fh ), "\n");
    fclose( fh );   

You need to remove newlines chars.

1 Like

There is no concept of "volatile" variables in AFL. All variables are just the same. So there is absolutely no difference if you assign the value from the code or read the value from file.
It will work exactly the same if string is the same. You should watch however for non-printable characters. Chances are that your file contains non-printable characters that you don't see (line feeds, carriage returns, tabs, any char code below 32, etc) that would make those strings different.

As explained here: How do I debug my formula? you can use TRACE/TRACEF function

_TRACEF(">%s< len = %g", symbol1, strlen( symbol1 ) );

Watch window does not display new line characters due to the limitations of Windows list view control (it does not support multiline cells). However the tooltips in the Debugger do show new lines, as presented in the picture below:


FWIW, in the future versions, the watch window will convert non-printable characters back to escape sequences.


Thank you very much. That fixes the problem and explains why it didn't work. Glad to hear that new watch window will cover these invisible characters.

1 Like

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