Future Mode, Multiple Time Frame, Foreign

Hello all,
I have developed a code in the Future mode which involved two different time frames and Foreign function. Let's assume that the original code is in M15 and now I switch to hourly time frame to get EURUSD Open and Close in the hourly time frame, do some math and expand it back to M15:

TimeFrameSet(inHourly);
EURUSD_C = Foreign( "EURUSD", "C" );
EURUSD_O = Foreign( "EURUSD", "O" );
EUR_S = (EURUSD_C - EURUSD_O )/(EURUSD_C + EURUSD_O );
TimeFrameRestore();

S_var				= TimeFrameExpand( EUR_S, inHourly);

I don't know why I got always the same value for candle open and close! and thus S_var always return zero!

Any error in the above code?
Thanks.

Some good advice: You have to read all Knowledge base articles before. Also there is search function there.
Almost everything (articles about questions having been asked multiple times) can be found there including manual and this forum here.
https://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/
https://forum.amibroker.com/search?q=setforeign%20timeframeset

So long story short... instead of Foreign use SetForeign placed before TimeFrameSet.

1 Like

Thank you so much for your swift reply.
Would you please explain why
EURUSD_C = Foreign( "EURUSD", "C" );

won't return the Close of EURUSD, but the following does!

SetForeign( "EURUSD" );
EURUSD_C =  Close;

Thanks again.

Sorry, I forgot to add that if I want to use SetForegin multiple times, do I need to use TimeFrameRestore() as well every time or in the final stage. What about TimeFrameSet()?
I want to get open and close of AUDUSD, EURUSD and GBPUSD in the hourly timeframe and expand it to the original time frame (for example M15).
Does the below combination of SetForeign and TimeFrameSet works?

SetForeign("AUDUSD");
TimeFrameSet(inHourly);

AUD = (C-O)/(C+O);

SetForeign( "EURUSD" );
EUR = (C-O)/(C+O);

SetForeign( "GBPUSD" );
GBP = (C-O)/(C+O);

TimeFrameRestore();
RestorePriceArrays() // Does this restore it to the previous step or to the original before the first SetForeign?

AUD = TimeFrameExpand( AUD, inHourly);
EUR = TimeFrameExpand( EUR, inHourly);
GBP = TimeFrameExpand( GBP, inHourly);

or I need to use the following order for each pair?

  1. SetForeign
  2. math ...
  3. TimeFrameSet
  4. RestorePriceArrays()
  5. TimeFrameRestore();

Thanks.

As explained in KB article of 2nd post... RestorePriceArrays and TimeFrameRestore are equivalent.

SetForeign("AUDUSD");
TimeFrameSet(inHourly);
	AUD = (C-O)/(C+O);
RestorePriceArrays();

SetForeign( "EURUSD" );
TimeFrameSet(inHourly);
	EUR = (C-O)/(C+O);
RestorePriceArrays();

SetForeign( "GBPUSD" );
TimeFrameSet(inHourly);
	GBP = (C-O)/(C+O);
RestorePriceArrays();

AUD = TimeFrameExpand( AUD, inHourly);
EUR = TimeFrameExpand( EUR, inHourly);
GBP = TimeFrameExpand( GBP, inHourly);
2 Likes