October 20, 2014
Time compression of data retrieved from another symbol
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 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.
The following sequence is required to code such conditions properly:
- switch to the other symbol with SetForeign
- compress data into higher interval with TimeFrameSet
- store the weekly values / conditions in custom variables
- with TimeFrameRestore() or RestorePriceArrays() functions restore the original arrays of the tested symbol (in the original time-frame)
- use custom variables assigned in step (3) expanded to original time-frame using TimeFrameExpand()
Here is the AFL formula, which implements the above conditions:
// first switch to ^GSPC symbol
SetForeign( "^GSPC" );
//
// compress data to weekly interval
TimeFrameSet( inWeekly );
//
// assign weekly values to custom variables
indexWeeklyClose = Close;
indexWeeklyMA = MA( Close, 52 );
indexWeeklyFilter = Close > MA( Close, 52 );
//
// restore original arrays (back to the primary symbol)
// RestorePriceArrays() function is an equivalent
TimeFrameRestore();
//
// align data back to original interval
indexFilterExpanded = TimeFrameExpand( indexWeeklyFilter, inWeekly );
//
// exploration shows the results, note that all weekly values
// need to be expanded if we haven't done it yet
//
Filter = 1;
AddColumn( Close, "Close AAPL" );
AddColumn( TimeFrameExpand( indexWeeklyClose, inWeekly ), "Weekly close ^GSPC" );
AddColumn( TimeFrameExpand( indexWeeklyMA, inWeekly ), "Weekly MA ^GSPC" );
AddColumn( indexFilterExpanded, "Weekly index filter", 1 )
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.
There is also an alternative method we can use:
- 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
indexClose = Foreign("^GSPC","C");
indexWeeklyClose2 = TimeFrameCompress( indexClose, inWeekly );
indexWeeklyMA2 = MA( indexWeeklyClose2, 52 );
indexWeeklyFilter2 = indexWeeklyClose2 > indexWeeklyMA2;
//
Filter = 1;
AddColumn( Close, "Close AAPL" );
AddColumn( TimeFrameExpand( indexWeeklyClose2, inWeekly ), "Weekly close ^GSPC" );
AddColumn( TimeFrameExpand( indexWeeklyMA2, inWeekly ), "Weekly MA ^GSPC" );
AddColumn( TimeFrameExpand( indexWeeklyFilter2, inWeekly ), "Weekly index filter", 1 )
Filed by Tomasz Janeczko at 4:27 pm under Exploration,Indicators
Comments Off on Time compression of data retrieved from another symbol