use custom variables assigned in step (3) expanded to original time-frame using TimeFrameExpand()<\/ol>Here is the AFL formula, which implements the above conditions:<\/p><\/span>\/\/ first switch to ^GSPC symbol
<\/span>SetForeign<\/span>( <\/span>"^GSPC" <\/span>);
<\/span>\/\/
\/\/ compress data to weekly interval
<\/span>TimeFrameSet<\/span>( <\/span>inWeekly <\/span>);
<\/span>\/\/
\/\/ assign weekly values to custom variables
<\/span>indexWeeklyClose <\/span>= <\/span>Close<\/span>;
<\/span>indexWeeklyMA <\/span>= <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>52 <\/span>);
<\/span>indexWeeklyFilter <\/span>= <\/span>Close <\/span>> <\/span>MA<\/span>( <\/span>Close<\/span>, <\/span>52 <\/span>);
<\/span>\/\/
\/\/ restore original arrays (back to the primary symbol)
\/\/ RestorePriceArrays() function is an equivalent
<\/span>TimeFrameRestore<\/span>();
<\/span>\/\/
\/\/ align data back to original interval
<\/span>indexFilterExpanded <\/span>= <\/span>TimeFrameExpand<\/span>( <\/span>indexWeeklyFilter<\/span>, <\/span>inWeekly <\/span>);
<\/span>\/\/
\/\/ exploration shows the results, note that all weekly values
\/\/ need to be expanded if we haven't done it yet
\/\/
<\/span>Filter <\/span>= <\/span>1<\/span>;
<\/span>AddColumn<\/span>( <\/span>Close<\/span>, <\/span>"Close AAPL" <\/span>);
<\/span>AddColumn<\/span>( <\/span>TimeFrameExpand<\/span>( <\/span>indexWeeklyClose<\/span>, <\/span>inWeekly <\/span>), <\/span>"Weekly close ^GSPC" <\/span>);
<\/span>AddColumn<\/span>( <\/span>TimeFrameExpand<\/span>( <\/span>indexWeeklyMA<\/span>, <\/span>inWeekly <\/span>), <\/span>"Weekly MA ^GSPC" <\/span>);
<\/span>AddColumn<\/span>( <\/span>indexFilterExpanded<\/span>, <\/span>"Weekly index filter"<\/span>, <\/span>1 <\/span>)<\/code>Let us compare the readings obtained from the code with a sample chart – both ^GSPC raw reading and 52-week MA values match the chart and the condition is properly aligned to the bars starting on 2011-10-28 and extends until new weekly bar is formed.<\/p>
<\/p>
There is also an alternative method we can use:<\/p>
- retrieve values from ^GSPC using Foreign() function
- compress these readings into weekly interval using TimeFrameCompress
- perform calculations on weekly compressed array
- expand the compressed data back to the original timeframe using timeFrameExpand<\/ol>
indexClose <\/span>= <\/span>Foreign<\/span>(<\/span>"^GSPC"<\/span>,<\/span>"C"<\/span>);
<\/span>indexWeeklyClose2 <\/span>= <\/span>TimeFrameCompress<\/span>( <\/span>indexClose<\/span>, <\/span>inWeekly <\/span>);
<\/span>indexWeeklyMA2 <\/span>= <\/span>MA<\/span>( <\/span>indexWeeklyClose2<\/span>, <\/span>52 <\/span>);
<\/span>indexWeeklyFilter2 <\/span>= <\/span>indexWeeklyClose2 <\/span>> <\/span>indexWeeklyMA2<\/span>;
<\/span>\/\/
<\/span>Filter <\/span>= <\/span>1<\/span>;
<\/span>AddColumn<\/span>( <\/span>Close<\/span>, <\/span>"Close AAPL" <\/span>);
<\/span>AddColumn<\/span>( <\/span>TimeFrameExpand<\/span>( <\/span>indexWeeklyClose2<\/span>, <\/span>inWeekly <\/span>), <\/span>"Weekly close ^GSPC" <\/span>);
<\/span>AddColumn<\/span>( <\/span>TimeFrameExpand<\/span>( <\/span>indexWeeklyMA2<\/span>, <\/span>inWeekly <\/span>), <\/span>"Weekly MA ^GSPC" <\/span>);
<\/span>AddColumn<\/span>( <\/span>TimeFrameExpand<\/span>( <\/span>indexWeeklyFilter2<\/span>, <\/span>inWeekly <\/span>), <\/span>"Weekly index filter"<\/span>, <\/span>1 <\/span>)<\/code>","protected":false},"excerpt":{"rendered":"AmiBroker’s Time-Frame functions (http:\/\/www.amibroker.com\/guide\/h_timeframe.html) allow to use multiple intervals within a single formula and combine them together. Another set of functions in AFL (Foreign and SetForeign) allow us to retrieve data of another symbol from the database, so we can implement strategies where rules are based on multiple symbols.This article shows how to combine these […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,3],"tags":[53,54],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/578"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=578"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/578\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/578\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}