Timeframeset how to use

Hi, I am trying to draw the opening range of 5 minute chart on 1 minute chart with Timeframeset, but something is not working. Please help fix my code, thanks.

NewDay = Day()!= Ref(Day(), -1); 
TimeFrameSet(in5Minute);
OH = ValueWhen(NewDay,H);         OL = ValueWhen(NewDay,L);

OH = IIf(OH!=Ref(OH,-1),Null,OH);
OL= IIf(OL!=Ref(OL,-1),Null,OL);
TimeFrameRestore();

Plot(OH,"OH",colorBlue,styleLine,styleDashed);

Plot(OL,"OL",colorRose,styleLine,styleDashed);
PlotOHLC( OH,OH,OL,OL, "", colorLightGrey,styleCloud,Null, Null, Null, -5);


After TimeFrameRestore where is your TimeFrameExpand ?
Read in amibroker’s help “Multiple Time Frame support in AFL”

1 Like

it worked, thanks a lot!!

I wish I could have a chart in sheet 1 in daily timeframe, the same chart in the same chart but in sheet 2 in weekly timeframe and the same chart in sheet 3 in monthly time frame. This is possible?
I do it with timeframeset?

I know that it is done by changing the timeframe above but I want to have the different timeframe fixed on each sheet and I would like to do it by code

You need TimeFrame functions (with expandPoint mode) for that. And Chart time frame set to Daily. Then weekly and monthly chart time frame is set via code (Sheet 2 and Sheet 3). Sheet 1 would have non time frame function code.

procedure LTF_set( timeframe ) {
	// set OHLCV to Longer time frame (LTF) 
	/// @link https://forum.amibroker.com/t/timeframeset-how-to-use/3694/7
	local i, OHLCstr, field;
	OHLCstr = "O,H,L,C,V";
	for ( i = 0; i < 4; i++ ) {
		field = StrExtract(OHLCstr, i);
		VarSet(field, TimeFrameGetPrice(field, timeframe, 0, expandPoint));
	}
}
function SparseExandIndicator(cond, indicator) {
	return ValueWhen(cond, SparseExpand(cond, indicator));
}

LTF_set( inWeekly );// OHLCV are LTF ones now

// LTF price plot using inbuilt DateTime axis
Plot( C, "Chart weekly", colorDefault, styleCandle );

// ######## For indicator calculations #########
// after setting LTF OHLCV
// compress non Null price/volume
cond = NOT IsNull(C);// C is LTF one 
C = SparseCompress(cond, C);
H = SparseCompress(cond, H);
L = SparseCompress(cond, L);
// etc.

// LTF Indi 1
mtf_ma = SparseExandIndicator(cond, MA(C, 20));
Plot( mtf_ma, "MA", colorRed );

// LTF Indi 2
mtf_ema = SparseExandIndicator(cond, EMA(C, 20));
Plot( mtf_ema, "EMA", colorOrange );

// LTF Indi 3, ADX needs HLC
mtf_adx = SparseExandIndicator(cond, ADX(15));
Plot( mtf_adx, "ADX", colorYellow, styleOwnScale );

// etc.

RestorePriceArrays();// restore original OHLCV

9

1 Like

Thanks for helping. The east code you write is for sheet 1 or on sheet 1 I put the normal chart on a daily basics timeframe. For sheet 2 is this code to put in weekly? And for sheet 3 is this same graph changing in the code any reference to weekly and putting monthly?
Excuse me so much question but I read and I find it hard to understand.

Code can be placed to all three sheets. But in sheet 1 it is not necessary as your chart time frame already is daily.

For simplicity I have modified code to add ParamList. So you just have to open param window via CTRL+R and then in each sheet (1 to 3) change to desired interval from there (see animation below).

procedure LTF_set( timeframe ) {
	// set OHLCV to Longer time frame (LTF) 
	/// @link https://forum.amibroker.com/t/timeframeset-how-to-use/3694/7
	local i, field, OHLCstr;	
	OHLCstr = "O,H,L,C,V";
	for ( i = 0; i < 4; i++ ) {
		field = StrExtract(OHLCstr, i);
		VarSet(field, TimeFrameGetPrice(field, timeframe, 0, expandPoint));
	}
}
function SparseExpandIndicator(cond, indicator) {
	return ValueWhen(cond, SparseExpand(cond, indicator));
}

Param_list = ParamList("TimeFrame", "inDaily|inWeekly|inMonthly", 1);
LTF_set( VarGet(Param_list) );// OHLCV are LTF ones now

// LTF price plot using inbuilt DateTime axis
Plot( C, EncodeColor(colorGold)+Param_list, colorDefault, styleCandle );

// ######## For indicator calculations #########
// after setting OHLCV to longer time frame
// compress non Null price/volume
cond = NOT IsNull(C);// C is LTF one 
C = SparseCompress(cond, C);
H = SparseCompress(cond, H);
L = SparseCompress(cond, L);
// etc.

// LTF Indi 1
mtf_ma = SparseExpandIndicator(cond, MA(C, 20));
Plot( mtf_ma, "MA", colorRed );

// LTF Indi 2
mtf_ema = SparseExpandIndicator(cond, EMA(C, 20));
Plot( mtf_ema, "EMA", colorOrange );

// LTF Indi 3, ADX needs HLC
mtf_adx = SparseExpandIndicator(cond, ADX(15));
Plot( mtf_adx, "ADX", colorBlueGrey, styleOwnScale );

// etc.

RestorePriceArrays();// restore original OHLCV

5

5 Likes

how i learn !! And with this functionality can I leave the shee 1 fixed daily and leave fixed on sheet 2 weekly?
So I don't have to change
regards
By the way, how can I contact you?
Thank you

Hi,

I have a (most likely) basic question regarding the indicator color. I applied 2 EMAs based on daily chart and 1 EMA based on weekly chart using TimeFrameSet. The following are my code.

//Plain Candlestick Chart

_SECTION_BEGIN("Price");

SetChartOptions(0,chartShowDates|chartLogarithmic);

Plot(C,"Close",colorBlack,styleCandle|styleNoTitle);

_SECTION_END();



//Indicators

_SECTION_BEGIN("Short EMA");

Short_EMA = EMA(C,Param("Period",21,1));
Plot(Short_EMA,_DEFAULT_NAME(),ParamColor ("Color", colorRed),styleLine);

_SECTION_END();



_SECTION_BEGIN("Mid EMA");

Mid_EMA = EMA(C,Param("Period",50,1));
Plot(Mid_EMA,_DEFAULT_NAME(),ParamColor ("Color", colorDarkBlue),styleLine);

_SECTION_END();



_SECTION_BEGIN("Long EMA");

Long_EMA_Activation = ParamToggle ("Activate","No|Yes",0);
Long_EMA = EMA(C,Param("Period",200,1));


if(Long_EMA_Activation == 1)
{
	Plot(Long_EMA,_DEFAULT_NAME(),paramcolor("Color",colorBrightGreen),styleLine);
}

_SECTION_END();



//Weekly EMA in Daily Timeframe

_SECTION_BEGIN("Weekly EMA in Daily Timeframe");

TimeFrameSet(inWeekly);

EMA21_W = EMA(C,Param("Period",21,1));

Plot(TimeFrameExpand(EMA21_W,inWeekly),"EMA 21 Weekly",ParamColor("Color",colorAqua),styleLine | styleThick);

TimeFrameRestore();

_SECTION_END();

However, when I switched the chart from daily to weekly, the indicator color for EMA 21 in weekly is not red, and if I deleted the "Weekly EMA in Daily Timeframe" section, the color become red again.

Daily Chart:
Daily

Weekly Chart:
Weekly

Please kindly advise how to keep the color red, both in daily and weekly chart.
Thanks!

Alfyan

You need to read here
http://www.amibroker.com/guide/h_timeframe.html

TimeFrameExpand follows after TimeFrameRestore !

//Weekly EMA in Daily Timeframe

_SECTION_BEGIN("Weekly EMA in Daily Timeframe");

TimeFrameSet(inWeekly);

EMA21_W = EMA(C,Param("Period",21,1));

TimeFrameRestore();

Plot(TimeFrameExpand(EMA21_W,inWeekly),"EMA 21 Weekly",ParamColor("Color",colorAqua),styleLine | styleThick);

_SECTION_END();

Short EMA and Weekly EMA have same default period. So they are overlapping in weekly interval.
So add condition to plot weekly EMA on intervals smaller than weekly only.

//Weekly EMA in Daily Timeframe

_SECTION_BEGIN("Weekly EMA in Daily Timeframe");

TimeFrameSet(inWeekly);

EMA21_W = EMA(C,Param("Period",21,1));

TimeFrameRestore();

color = ParamColor("Color",colorAqua);

if ( Interval() < inWeekly )
	Plot(TimeFrameExpand(EMA21_W,inWeekly),"EMA 21 Weekly", color, styleLine | styleThick);

_SECTION_END();

-> Entire code:

//Plain Candlestick Chart

_SECTION_BEGIN("Price");

SetChartOptions(0,chartShowDates|chartLogarithmic);

Plot(C,"Close",colorBlack,styleCandle|styleNoTitle);

_SECTION_END();



//Indicators

_SECTION_BEGIN("Short EMA");

Short_EMA = EMA(C,Param("Period",21,1));
Plot(Short_EMA,_DEFAULT_NAME(),ParamColor ("Color", colorRed),styleLine);

_SECTION_END();



_SECTION_BEGIN("Mid EMA");

Mid_EMA = EMA(C,Param("Period",50,1));
Plot(Mid_EMA,_DEFAULT_NAME(),ParamColor ("Color", colorDarkBlue),styleLine);

_SECTION_END();



_SECTION_BEGIN("Long EMA");

Long_EMA_Activation = ParamToggle ("Activate","No|Yes",0);
Long_EMA = EMA(C,Param("Period",200,1));


if(Long_EMA_Activation == 1)
{
	Plot(Long_EMA,_DEFAULT_NAME(),paramcolor("Color",colorBrightGreen),styleLine);
}

_SECTION_END();



//Weekly EMA in Daily Timeframe

_SECTION_BEGIN("Weekly EMA in Daily Timeframe");

TimeFrameSet(inWeekly);

EMA21_W = EMA(C,Param("Period",21,1));

TimeFrameRestore();

color = ParamColor("Color",colorAqua);

if ( Interval() < inWeekly )
	Plot(TimeFrameExpand(EMA21_W,inWeekly),"EMA 21 Weekly", color, styleLine | styleThick);

_SECTION_END();

Hi @fxshrat ,

Noted.

Understood. So in my previous code when I switched to weekly timeframe, the system recognised 2 EMA with same parameter, therefore it's like "conflicting". Therefore to avoid such conflict, it is necessary to add another rule which is to plot the "EMA 21 Weekly" only if the interval is smaller or higher than weekly timeframe. Am I understanding this correctly?

Thanks.