Unexpected result with 'Foreign'

I have the following simple code which compiles without problems.

_SECTION_BEGIN("ValueEUR");
SetForeign("EURUSD"); 
V1 = C;
SetForeign("EURCAD");
V2 = C;
SetForeign("EURGBP");
V3 = C;
SetForeign("EURCHF");
V4 = C;
SetForeign("EURNZD");
V5 = C;
SetForeign("EURAUD");
V6 = C;
SetForeign("EURJPY");
V7 = C;
VEUR = (V1+V2+V3+V4+V5+V6+V7)/7;
Plot( VEUR, "VEUR", colorWhite, stylethick |styleNoTitle );
_SECTION_END();

It is supposed to run as subchart under whatever other chart.
I'm connected to Interactive Brokers, so the data should be available.
But instead of the expected result it simply draws a linechart
of the mainchart to which the code was associated...
  1. You should not do like you do in first post.

    Instead you should do like this

    _SECTION_BEGIN("ValueEUR");
    ticker_list = "EURUSD,EURCAD,EURGBP,EURCHF,EURNZD,EURAUD,EURJPY";
    total= 0;
    for (i = 0; (ticker = StrExtract(ticker_list, i)) != ""; i++) 
    	total+= Nz(Foreign(ticker, "C"));	
    sym_num = StrCount(ticker_list, ",")+1;
    VEUR = total/sym_num;
    Plot(VEUR, "VEUR", colorWhite, stylethick |styleNoTitle );
    _SECTION_END();
    

    In addition you might use static variables for storage of result (after some trigger event)
    as posted today here: Intra day volume for nifty 50 index

  2. Your symbols are not inline with Interactive Brokers symbology
    Please read here
    https://www.amibroker.com/kb/2014/12/02/how-to-find-correct-symbol-for-interactive-brokers-data/

    So e.g. EURUSD symbol on IdealPro would be EUR.USD-IDEALPRO-CASH
    So since your symbols are not proper IB symbols you won't get expected results.

1 Like

Thank you very much for your answer.
I changed all the symbols and all seemed to work out fine,
but the result doesn't correspond to the result I got with my original
script in easylanguage. It should be 18.xx actually, because the
average rises as the EURJPY is at 124.xx actually...
while I obtain now 0.169xx

image

Ok, I found the problem, as it seemed to exclude the JPY as it was not
integrated in my amibroker database, but I thought that this would not be necessary
with 'foreign' as my aim was to establish the same chart also with the
other 7 currencies to compare their relative strength, but without
integrating all the details in my amibroker database.
So doesn't it work like I expected with 'foreign' ?

image

You need to add symbols to DB first.

Foreign calls existing symbols data of DB. It is does not create/add symbols.
Please read documentation.
Nowhere is it said that Foreign() function creates symbols.

14