# amibroker

### Using loops with TimeFrame functions

AmiBroker features a powerful set of TimeFrame functions that allow combining different time intervals in single system formula. There is one aspect of TimeFrame functions that is important to understand to properly use them. When we switch to higher interval using TimeFrameSet function – the BarCount does not really change – TimeFrameSet just squeezes the arrays so we have first N-bars filled with Null values (undefined) and then – last part of the array contains the actual time-compressed values. This is explained in details here: http://www.amibroker.com/guide/h_timeframe.html

Normally it does not present any problem as long as we use array functions, because array functions check for Nulls occuring at the beginning of the data series and skip them appropriately. The story is different when we try to use loops.

If we want to use looping code in higher time-frame, we can not really start our calculations from the bar 0, because it would contain Null instead of real data. That is why we would first need to detect were the actual compressed data begins and start calculations on that particular bar instead.

Here is a sample formula showing how to compute AMA function in a loop, based on weekly data (the code should be applied in Daily interval). Code will identify the first non-Null bar and initialize the first AMA value with Close of that bar, then it will continue calculations

`Plot( Close, "Close", colorBlack );// switch to higher timeframeTimeFrameSet( inWeekly );smooth = 0.2;myAMA = Close;// search for start (non-null) barfor( start = 0; start < BarCount; start++ ){   if( NOT IsNull( Close[ start ] ) ) break;}// looping codefor ( i = start + 1; i < BarCount; i++ ){    // this part will execute only after the first non-null bar has been identified    myAMA[ i ] = Close[ i ] * smooth + myAMA[ i - 1 ] * ( 1 - smooth );}// regular AMA function for comparisonweeklyAMA = AMA( Close, 0.2 );//restore original time-frameTimeFrameRestore();// plot expanded values retrieved from Weekly framePlot( TimeFrameExpand( myAMA, inWeekly ), "weekly AMA loop", colorRed );Plot( TimeFrameExpand( weeklyAMA, inWeekly ), "weekly AMA", colorBlue, styleDots )`

The code above is good for pre-5.90 versions. In version 5.90 we have a new function that counts Nulls for us making the code shorter and clearer, as shown below:

`Version( 5.90 );Plot( Close, "Close", colorBlack );// switch to higher timeframeTimeFrameSet( inWeekly );smooth = 0.2;myAMA = Close;// new 5.90 function that counts leading Nullsstart = NullCount( Close );// looping codefor ( i = start + 1; i < BarCount; i++ ){    // this part will execute only after the first non-null bar has been identified    myAMA[ i ] = Close[ i ] * smooth + myAMA[ i - 1 ] * ( 1 - smooth );}// regular AMA function for comparisonweeklyAMA = AMA( Close, 0.2 );//restore original time-frameTimeFrameRestore();// plot expanded values retrieved from Weekly framePlot( TimeFrameExpand( myAMA, inWeekly ), "weekly AMA loop", colorRed );Plot( TimeFrameExpand( weeklyAMA, inWeekly ), "weekly AMA", colorBlue, styleDots )`

### How to plot daily High and Low on intraday chart

The AFL offers a set of time-frame functions which allow to use multiple intervals within a single formula (the topic is explained in details in the following tutorial chapter: http://www.amibroker.com/guide/h_timeframe.html)

In situations, where we do not need to calculate any indicators based on higher interval data, but rather just read OHLC, V or OI arrays – TimeFrameGetPrice is the most convenient function to use.

To plot daily High and Low levels we just need to read the respective arrays calling: TimeFrameGetPrice(“H”, inDaily ) – the first argument specifies the array we want to read, the second argument defines the interval we are reading data from. As with any other TimeFrame functions – we can only read data from higher intervals, so it is possible to read daily data when we work with 1-minute quotes, but not the other way round.

Here is a sample formula which draws daily high and low in the intraday chart:

`Plot( Close, "Close", colorDefault, styleBar );Plot( TimeFrameGetPrice("H", inDaily ), "day high", colorGreen, styleStaircase | styleThick);Plot( TimeFrameGetPrice("L", inDaily ), "day low", colorRed, styleStaircase | styleThick)`

TimeFrameGetPrice() functions allow also to easily shift the reading by N-bars of the higher interval if we specify that in 3rd argument of the function, so calling TimeFrameGetPrice( “H”, inDaily, -1 ) will return the high of previous day.

The following code draws high / low of previous day on top of the intraday chart:

`Plot( Close, "Close", colorDefault, styleBar );hlstyle = styleStaircase | styleThick;Plot( TimeFrameGetPrice( "H", inDaily, -1 ), "Prev High", colorGreen, hlstyle );Plot( TimeFrameGetPrice( "L", inDaily, -1 ), "Prev Low", colorRed, hlstyle )`