{"id":578,"date":"2014-10-20T16:27:07","date_gmt":"2014-10-20T21:27:07","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=578"},"modified":"2014-12-04T08:20:32","modified_gmt":"2014-12-04T13:20:32","slug":"foreign-timeframeset","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/10\/20\/foreign-timeframeset\/","title":{"rendered":"Time compression of data retrieved from another symbol"},"content":{"rendered":"

AmiBroker’s Time-Frame functions (http:\/\/www.amibroker.com\/guide\/h_timeframe.html<\/a>) 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.<\/p>

This article shows how to combine these two features together and properly use Time-Frame functions on data retrieved from another symbol. Let us consider an example of a strategy, which works on daily data, but uses an additional filter based on weekly readings of S&P500 index.<\/p>

The following sequence is required to code such conditions properly:<\/p>

  1. switch to the other symbol with SetForeign
  2. compress data into higher interval with TimeFrameSet
  3. store the weekly values \/ conditions in custom variables
  4. with TimeFrameRestore() or RestorePriceArrays() functions restore the original arrays of the tested symbol (in the original time-frame)
  5. 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>

    \"TimeFrame<\/p>

    There is also an alternative method we can use:<\/p>

    1. retrieve values from ^GSPC using Foreign() function
    2. compress these readings into weekly interval using TimeFrameCompress
    3. perform calculations on weekly compressed array
    4. 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}]}}