Using Foreign() and TimeFrameSet()

Hi guys,

I have a problem with one indicator in my chart. Looking at it in-depth, I have realized it seems related to the way I am using Foreign() and TimeFrameSet(). I am sure I am wrong, but I don't find my error, so I would appreciate some help

If I use Foreign() alone, I have no problem and Foreign Data it's ok.

SymbolClose = C; 
SPXClose = Foreign("$SPX", "C");

Filter = DayOfWeek() == 5 ;

AddColumn(SymbolClose,"SymbolClose",1.2);
AddColumn(SPXClose,"SPXClose",1.2);

image

$SPX data (my Foreign Symbol) it's right.

My problem begins when I add TimeFrameSet() function. I see that Foreign data changes for the same dates.

TimeFrameSet(inWeekly);

SymbolClose = C; 
SPXClose = Foreign("$SPX", "C");

TimeFrameRestore();

Filter = DayOfWeek() == 5 ;

AddColumn(TimeFrameExpand(SymbolClose,inWeekly,expandFirst),"SymbolClose",1.2);
AddColumn(TimeFrameExpand(SPXClose,inWeekly,expandFirst),"SPXClose",1.2);

image

But, on the other hand, if I use the same piece of code in Exploration, using $SPX as the current symbol, I see results are ok.

image

This is what confuses me the most, but I must recognize that surely I have my own big mistake in front of my eyes but I don't see it.

Best regards,

@BernieTGN I think you are confusing Foreign with the use of SetForeign

indexClose 			= Foreign("$SPX","C");
indexWeeklyClose 	= TimeFrameCompress( indexClose, inWeekly );

Filter = DayOfWeek() == 5 ;
AddColumn( Close, "Close AGR" );
AddColumn( TimeFrameExpand( indexWeeklyClose, inWeekly ), "Weekly close $SPX" );

I think this article will help
http://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/

1 Like

Hi @portfoliobuilder,

Thank you a lot for responding. I think your code works right for $SPX, but the Close for AGR will be Friday's close, not week close. I mean, if I use High instead of Close, that value would probably be different. That's why I was using TimeFrameSet and not TimeFrameCompress and TimeFrameExpand.

Thanks for the link to that article. I know "KNOWLEDGE BASE" is a must-read, but I must admit that, if I have read this article, I do not remember it at all.

I'll read it and I will think more about this matter because I still a little confused. I know why your code works, but I think my head is too tired to see why mine is wrong, even being alerted I am confusing that two functions. :sweat_smile:

Thanks a lot.

1 Like

Hi @portfoliobuilder,

I think now I understand how it works and what was my mistake.

In the linked article, @Tomasz talk about a "required sequence" to work with time compression of data retrieved from another symbol. I see he use at first SetForeing and then TimeFrameSet. I didn't give any importance to it, thinking that usually there is a lot of ways to do the same thing and probably it would be a mere thing. But I am afraid I was completely wrong. I think it was my big mistake and this is precisely the right order.

Also, I have realized I was using ExpandFirst in a bad way.

I have used a similar Tomasz's code and it works fine.

// SYMBOL............

TimeFrameSet(inWeekly);

SymbolClose = C; 

TimeFrameRestore();


// FOREIGN symbol............

SetForeign( "$SPX" );

TimeFrameSet( inWeekly );
SPXWeeklyClose = Close;
TimeFrameRestore();

Filter = DayOfWeek() == 5 ;

AddColumn( TimeFrameExpand( SymbolClose, inWeekly ), "Weekly close Symbol" );
AddColumn( TimeFrameExpand( SPXWeeklyClose, inWeekly ), "Weekly close $SPX" );

image

But, if I change the order between SetForeign() and TimeFrameSet(), as shown in the code below, it seems not working fine.

TimeFrameSet( inWeekly );

// SYMBOL............
SymbolClose = C; 


// FOREIGN symbol............
SetForeign( "$SPX" );
SPXWeeklyClose = Close;
RestorePriceArrays();

TimeFrameRestore();

Filter = DayOfWeek() == 5 ;


AddColumn( TimeFrameExpand( SymbolClose, inWeekly ), "Weekly close Symbol" );
AddColumn( TimeFrameExpand( SPXWeeklyClose, inWeekly ), "Weekly close $SPX" );

image

I hope been alerted if my thought is wrong :sweat_smile:

Thanks a lot,

1 Like

@BernieTGN.

besides of Knowledge base articles (first place to go after visiting AB manual) most if not all basics have been covered in forum already too.

Please always use forum search too before.
https://forum.amibroker.com/search?q=timeframeset%20setforeign

There is no secrets and hidden info.

As @Tomasz often writes, every word of sentence and every sample line of code is important.

1 Like