How to use different indicators in different timeframes

Hello

I want to produce buy and sell signals based on different timeframes. For example, if the close price in 4h was more than the MA(50), I want to look for buy signals in the hourly timeframe when the hourly close price crosses MA(10), and vice vers for the sell signal. Could you please help me in this regard?

Best regards,

Study the topic below it might get you strated.

Also read carefully

2 Likes

Thank you very much. I appreciate it.

Hello,

I wrote the following code:

_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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("3MA");

// Buy and Sell
TimeFrameSet( inHourly );
ma5_h = MA(C, 5);
ma20_h = MA(C, 20);
TimeFrameRestore(); 

TimeFrameSet( 4*inHourly );
ma50_4h = MA(C, 50);
ma20_4h = MA(C, 20);
TimeFrameRestore(); 

Buy = (ma20_4h > ma50_4h) AND Cross(ma5_h, ma20_h);

Sell = Cross(ma20_h, ma5_h);

Buy = ExRem (Buy, Sell);
Sell = ExRem (Sell, Buy);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0, L, Offset = -10);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0, L, Offset = -20);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0, L, Offset = -15);
                
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorRed, 0, H, Offset = 35);
_SECTION_END();

_SECTION_BEGIN("MA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();

_SECTION_BEGIN("MA2");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();

_SECTION_BEGIN("MA3");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();

What is the buy signal? MA(C,20) should be greater than MA(C,50) (4h timeframe) and MA(C,5) crosses MA(C,20) in the hourly timeframe.

In the below pic, MA(C,20) is less than MA(C,50) (4h timeframe), so there shouldn't be any buy signal, but if I go the hourly timeframe I find some wrong buy signals:
image
image

Could you please tell me why is that?

Best regards,
Siamak
@awilson

@simon you have not properly used the MultiTimeFrame functionality. you need TimeFrameExpand.

For example use 1 hour data and chart/backtest in the 1 hour time frame.

// run the backtest, the charting and the exploration on 1 hour data
ma5_h = MA(C, 5);
ma20_h = MA(C, 20);

TimeFrameSet( 4*inHourly );
ma50_4h = MA(C, 50);
ma20_4h = MA(C, 20);
TimeFrameRestore(); 

// use better variable names than this, but for clarity I've used long names
Expanded_ma50_4h = TimeFrameExpand( ma50_4h, 4*inHourly);
Expanded_ma20_4h = TimeFrameExpand( ma20_4h, 4*inHourly);

image

1 Like

Check the link posted by @awilson and focus on TimeFrameExpand

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.