AFL REQUIRED (plots MA of DAILY TF on smaller TF)

#1
dera traders

i tried to find out this afl but cudnt.

i want to plot different MAs of DAILY TF on smaller TF.
for e.g. if i use 3 min TF, i want to plot different MA of not 3 min TF (which is usually the case) but DAILY TF.

please provide it. thks
 

ajeetsingh

Well-Known Member
#2
dera traders

i tried to find out this afl but cudnt.

i want to plot different MAs of DAILY TF on smaller TF.
for e.g. if i use 3 min TF, i want to plot different MA of not 3 min TF (which is usually the case) but DAILY TF.

please provide it. thks
use
TimeFrameSet(inDaily);
then after input whatever you want in daily thn use
TimeFrameRestore();
 
#4
_SECTION_BEGIN("Daily EMA");
TimeFrameSet(inDaily);
e5=EMA(C,5);
e50=EMA(C,50);
e200=EMA(C,200);
Plot(e5,"Daily 5Ema",colorRed,styleLine);
Plot(e200,"Daily 200Ema",colorblue,styleLine);
TimeFrameRestore();
_SECTION_END();
 
#5
hi
here is the correct code.
Novicetrader-check what was wrong in your code!


HTML:
        _SECTION_BEGIN("Daily EMA on intraday chart");
TimeFrameSet(inDaily);
e5d=EMA(C,5);
e50d=EMA(C,50);
e200d=EMA(C,200);
TimeFrameRestore();
_SECTION_END();

Plot( Close, "Price", colorRed, styleCandle );

// plot expanded average

Plot( TimeFrameExpand( e5d, inDaily), "5 MA daily bars", colorBlue );
Plot( TimeFrameExpand( e50d, inDaily), "50 MA daily bars", colorRed );
Plot( TimeFrameExpand( e200d, inDaily), "200 MA daily bars", colorBlack );

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();
 

pankajpari

RSITradeMaster
#6
Hi ford7k,
Pls correct me if my understanding is wrong :

TimeFrameExpand() is used only if an indicator is to be displayed in some other timeframe. But if we only want to use the indicator to, say, compare (instead of displaying the same), then we can use the indicator as it is.

For example, using the above code, if I want to Buy when the EMA(C, 5) in 3 minute TF crosses above EMA(C, 5) in Daily TF, then I can use the following code :

Buy = Cross( EMA( C, 5), e5d);

Is this correct or do I have to use TimeFrameExpand() here as well.

Also, pls let me know where and how is the TimeFrameCompress() function to be used.
 

sr114

Well-Known Member
#7
Multi time frame support in amibroker
please go thru this excerpt from amibroker manual

Multiple Time Frame support in AFL

Release 4.41 brings ability to use multiple time frames (bar intervals) in single formula. The time frame functions can be divided into 3 functional groups:
switching time frame of build-in O, H, L, C, V, OI, Avg arrays: TimeFrameSet, TimeFrameRestore
compressing/expanding single arrays to/from specified interval: TimeFrameCompress, TimeFrameExpand
immediate access to price/volume arrays in different time frame: TimeFrameGetPrice

First group is used when your formula needs to perform some calculations on indicators in different time frame than currently selected one. For example if you need to calculate 13-bar moving average on 5 minute data and 9 bar exponential avarage from hourly data while current interval is 1 minute you would write:

TimeFrameSet( in5Minute ); // switch to 5 minute frame

/* MA now operates on 5 minute data, ma5_13 holds time-compressed 13 bar MA of 5min bars */

ma5_13 = MA( C, 13 );

TimeFrameRestore(); // restore time frame to original

TimeFrameSet( inHourly ); // switch now to hourly

mah_9 = EMA( C, 9 ); // 9 bar moving average from hourly data

TimeFrameRestore(); // restore time frame to original

Plot( Close, "Price", colorWhite, styleCandle );

// plot expanded average

Plot( TimeFrameExpand( ma5_13, in5Minute), "13 bar moving average from 5 min bars", colorRed );
Plot( TimeFrameExpand( mah_9, inHourly), "9 bar moving average from hourly bars", colorRed );

TimeFrameSet( interval ) - replaces current built-in price/volume arrays: open, high, low, close, volume, openint, avg with time-compressed bars of specified interval once you switched to a different time frame all calculations and built-in indicators operate on selected time frame. To get back to original interval call TimeFrameRestore() funciton. If you want to call TimeFrameSet again with different interval you have to restore original time frame first using TimeFrameRestore(). Interval is time frame interval in seconds. For example: 60 is one minute bar. You should use convenient constants for common intervals: in1Minute, in5Minute, in15Minute, inHourly, inDaily, inWeekly, inMonthly.

With version 4.70 you can also specify N-tick intervals. This is done by passing NEGATIVE value as interval. For example -5 will give 5-tick bar compression, and -133 will give 133-tick compression. Please note that using N-tick intervals works only if your database uses Tick base time interval set in File -> Database Settings dialog.

TimeFrameSet( -133 ); // switch to 133-tick interval

TimeFrameRestore() - restores price arrays replaced by SetTimeFrame.Note that only OHLC, V, OI and Avg built-in variables are restored to original time frame when you call TimeFrameRestore(). All other variables created when being in different time frame remain compressed. To de-compress them to original interval you have to use TimeFrameExpand.

Once you switch the time frame using TimeFrameSet, all AFL functions operate on this time frame until you switch back the time frame to original interval using TimeFrameRestore or set to different interval again using TimeFrameSet. It is good idea to ALWAYS call TimeFrameRestore when you are done with processing in other time frames.

When time frame is switched to other than original interval the results of all functions called since TimeFrameSet are time-compressed too. If you want to display them in original time frame you would need to 'expand' them as described later. Variables created and assigned before call to TimeFrameSet() remain in the time frame they were created. This behaviour allows mixing unlimited different time frames in single formula.

Please note that you can only compress data from shorter interval to longer interval. So when working with 1-minute data you can compress to 2, 3, 4, 5, 6, ....N-minute data. But when working with 15 minute data you can not get 1-minute data bars. In a similar way if you have only EOD data you can not access intraday time frames.

Second group: TimeFrameCompress/TimeFrameExpand allow to compress and expand single arrays to / from different time frames. Especially worth mentioning is TimeFrameExpand that is used to decompress array variables that were created in different time frame. Decompressing is required to properly display the array created in different time frame. For example if you want to display weekly moving average it must be 'expanded' so the data of one weekly bar covers five daily bars (Monday-Friday) of corresponding week.
TimeFrameExpand( array, interval, mode = expandLast ) - expands time-compressed array from 'interval' time frame to base time frame ('interval' must match the value used in TimeFrameCompress or TimeFrameSet)
Available modes:
expandLast - the compressed value is expanded starting from last bar within given period (so for example weekly close/high/low is available on Friday's bar)
expandFirst - the compressed value is expanded starting from first bar within given period (so for example weekly open is available from Monday's bar)
expandPoint - the resulting array gets not empty values only for the last bar within given period (all remaining bars are Null (empty)).

Caveat: expandFirst used on price different than open may look into the future. For example if you create weekly HIGH series, expanding it to daily interval using expandFirst will enable you to know on MONDAY what was the high for entire week.

TimeFrameCompress is provided for completeness and it can be used when you want to compress single array without affecting built-in OHLC,V arrays. If you call TimeFrameCompress it does not affect results of other functions.

wc = TimeFrameCompress( Close, inWeekly );

/* now the time frame is still unchanged (say daily) and our MA will operate on daily data */

dailyma = MA( C, 14 );

/* but if we call MA on compressed array, it will give MA from other time frame */

weeklyma = MA( wc, 14 ); // note that argument is time-compressed array

Plot( dailyma, "DailyMA", colorRed );

weeklyma = TimeFrameExpand( weeklyma, inWeekly ); // expand for display

Plot( weeklyma, "WeeklyMA", colorBlue );

During this formula the time frame remained at original setting we only compressed single array.

TimeFrameCompress( array, interval, mode = compressLast )
- compresses single array to given interval using given compression mode available modes:
compressLast - last (close) value of the array within interval
compressOpen - open value of the array within interval
compressHigh - highest value of the array within interval
compressLow - lowest value of the array within interval
compressVolume - sum of values of the array within interval
Graph0 = TimeFrameExpand( TimeFrameCompress( Close, inWeekly, compressLast ), inWeekly, expandLast );
Graph1 = TimeFrameExpand( TimeFrameCompress( Open, inWeekly, compressOpen ), inWeekly, expandFirst );

Third group consist of just one useful function: TimeFrameGetPrice which allows to reference price and volume from other time frames without switching /compressing/expanding time frames. Just one function call to retrieve price from higher time frame. It allows also to reference not only current but past bars from different time frames.

TimeFrameGetPrice( pricefield, interval, shift = 0, mode = expandFirst );
- references OHLCV fields from other time frames. This works immediatelly without need to call TimeFrameSet at all.
Price field is one of the following: "O", "H", "L", "C", "V", "I" (open interest). Interval is bar interval in seconds. shift allows to reference past (negative values) and future (positive values) data in higher time frame. For example -1 gives previous bar's data (like in Ref function but this works in higher time frame).

Examples:
TimeFrameGetPrice( "O", inWeekly, -1 ) // gives you previous week Open price
TimeFrameGetPrice( "C", inWeekly, -3 ) // gives you weekly Close price 3 weeks ago
TimeFrameGetPrice( "H", inWeekly, -2 ) // gives you weekly High price 2 weeks ago
TimeFrameGetPrice( "O", inWeekly, 0 ) // gives you this week Open price.
TimeFrameGetPrice( "H", inDaily, -1 ) // gives previous Day High when working on intraday data

Shift works as in Ref() function but it is applied to compressed time frame.

Note these functions work like these 3 nested functions
TimeFrameExpand( Ref( TimeFrameCompress( array, interval, compress(depending on field used) ), shift ), interval, expandFirst )
therefore if shift = 0 compressed data may look into the future ( weekly high can be known on monday ). If you want to write a trading system using this function please make sure to reference PAST data by using negative shift value.

How does it work internally ?

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.
http://www.amibroker.com/guide/h_timeframe.html

all of ur doubts will be clear

rgds
subroto
 
Last edited:
#8
Hi subroto jee

please check if this code below is correct or not.
I use the code on 5 minute chart or 15 minute chart
thanks & regards
ford

=====================
HTML:
 //MA14 on 15min-30min-60min timeframs as seen on 5min chart-choose 5 min chart
//subroto says
//TimeFrameCompress is provided for completeness AND it can be used when you want to compress single array without affecting built-in OHLC,V arrays. 
//if you call TimeFrameCompress it does NOT affect results of other functions.

qC = TimeFrameCompress( Close, in15Minute );
Hhc = TimeFrameCompress( Close, 0.5*inHourly );
hC = TimeFrameCompress( Close, inHourly );
//========================
ma_15 = MA(qC,14);
ma_15 = TimeFrameExpand( ma_15, in15Minute ); // expand for display
Plot( ma_15, "ma_15", colorBlue );
//=================================
Halfhourlyma = MA(Hhc,14);
Halfhourlyma = TimeFrameExpand( halfhourlyma, 0.5*inHourly ); // expand for display
Plot( Halfhourlyma, "HalfhourlyMA", colorRed );
//=================================

Hourlyma = MA(Hc,14);
hourlyma = TimeFrameExpand( hourlyma, inHourly ); // expand for display
Plot( hourlyma, "hourlyMA", colorCustom12 );
//========================================
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();
 

sr114

Well-Known Member
#9
another way of doing it is the following:

we are using the 3 min tf and want to plot a MA(moving averages) of 1 hour tf in it. so we can multiply the tf part of the 3 min tf ma with 20( 1 hour = 60 min and 60 min/3min=20, so the factor is 20) to get the hourly equivalent of the MA

the reason is:

using extensively this coding along with TimeFrameSet and TimeFrameRestore seemed to use processor resources by simply multiplying the FastMa and SlowMA values of MACD by the relevant value was much faster and produced exactly the same plotting.

Example: instead of calling MACD (12,25,9) from inHourly when I am in 5 minutes chart, I found that just using MACD(12*12,25*12,9*12) gave the same plotting.Replace "12" by any other value depending on the difference between the 2 time frames. In this case 5*12= 60.
so u can use this framework

rgds
subroto
 
Last edited:

sr114

Well-Known Member
#10
Hi subroto jee

please check if this code below is correct or not.
I use the code on 5 minute chart or 15 minute chart
thanks & regards
ford

=====================
HTML:
 //MA14 on 15min-30min-60min timeframs as seen on 5min chart-choose 5 min chart
//subroto says
//TimeFrameCompress is provided for completeness AND it can be used when you want to compress single array without affecting built-in OHLC,V arrays. 
//if you call TimeFrameCompress it does NOT affect results of other functions.

qC = TimeFrameCompress( Close, in15Minute );
Hhc = TimeFrameCompress( Close, 0.5*inHourly );
hC = TimeFrameCompress( Close, inHourly );
//========================
ma_15 = MA(qC,14);
ma_15 = TimeFrameExpand( ma_15, in15Minute ); // expand for display
Plot( ma_15, "ma_15", colorBlue );
//=================================
Halfhourlyma = MA(Hhc,14);
Halfhourlyma = TimeFrameExpand( halfhourlyma, 0.5*inHourly ); // expand for display
Plot( Halfhourlyma, "HalfhourlyMA", colorRed );
//=================================

Hourlyma = MA(Hc,14);
hourlyma = TimeFrameExpand( hourlyma, inHourly ); // expand for display
Plot( hourlyma, "hourlyMA", colorCustom12 );
//========================================
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();
after every use of timeframeexpand when u r finishing the plotting , u have to go for the function timeframerestore or te total plotting will be erratic

rgds
subroto
 

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?