Clarifications on getting moving average of daily bars when base time interval is hourly

When using end-of-day data with base time interval set at daily, the code for getting 50-day moving average of closing daily price is;

MV_50 = EMA( Close, 50);

What if the database contains hourly data with base time interval set at 1 hour, how should the code for getting 50-day moving average look like?

Is my code below for getting 50-day moving average correct?

TimeFrameSet( inDaily );
MV_50 = EMA( Close, 50);
TimeFrameRestore();

When base time interval is hourly, does it mean that MV_50 = EMA( Close, 50); alone without TimeFrameSet() and TimeFrameRestore() will return moving average of 50 hourly closing bars?

Please advise if my understanding is correct.

The code is incorrect (or only partially correct) because it misses TimeFrameExpand. It was explained in the guide http://www.amibroker.com/guide/h_timeframe.html (orange boxes contain most imporant stuff) as it says:

IMPORTANT: TimeFrameExpand IS REQUIRED for any formula that uses TimeFrame* functions. If you don't expand time compressed data you will have incorrect timestamps (see description below in "How it works").

and

Time-frame functions do not change the BarCount - they just squeeze the arrays so you have first N-bars filled with NULL values and then - last part of the array contains the actual time-compressed values.
This is why it is essential to expand the data back to the original frame with TimeFrameExpand.

and this: How do I debug my formula?

As advised in the manual and on the forum, to gain insight into how functions work you should use exploration:

// this code is intended to be run in HOURLY timeframe

HourlyMA = EMA( Close, 50 ); // this is HOURLY EMA

TimeFrameSet( inDaily );
SqueezedDailyEMA = EMA( Close, 50); // Daily EMA but squeezed !
TimeFrameRestore();

// should expand to use it !
ExpandedDailyEMA = TimeFrameExpand( SqueezedDailyEMA , inDaily );

Filter = 1;
AddColumn( HourlyMA , "HourlyEMA" );
AddColumn( SqueezedDailyEMA , "SqueezedDailyEMA " );
AddColumn( ExpandedDailyEMA , "ExpandedDailyEMA " );

If you now run this exploration, you will clearly see how all functions work. EMA alone produces EMA in hourly timeframe. EMA placed inside TimeFrameSet/Restore produces SQUEEZED array (daily bars are placed (squeezed) at the very end of the array and timestamps do NOT match with hourly data).
Only after TimeFrameExpand compressed daily data are aligned with hourly data and can be used together because all timestamps match.

The most important thing to remember is written in the first orange box in the tutorial http://www.amibroker.com/guide/h_timeframe.html

IMPORTANT: TimeFrame functions are NOT intended to replace Periodicity setting. To switch periodicity/interval you should only use Periodicity setting. TimeFrame functions are ONLY for formulas that MIX many different intervals at once in single formula.

As can be seen from the above - actually entire explanation is in the manual. I just quoted relevant parts. I can't write it better. Only you can read it multiple times until it sinks and/or run exploration to see how it works in practice. There is no substitute for actually running exploration. It explains everything.

3 Likes