Multi Time Frame Backtest Analysis/Coding with MQL4

 
Dear MQL4 developers,

What would be best practice to develop pattern recognition EA which is looking on multiple time-frames at same time?

My goal is to find candle pattern (using OHLC prices) on H1 chart and at the same time want to check for same pattern in H4.  If/when both patterns are found, then that is my entry signal.


Also it's required to be able to back-test.

Possible Solution:   I think could work is to run EA on H1 and simply aggregate H1 candles to get H4 candle.     Is this the only approach ?     or is there another better
suggested method of achieving multi time-frame trading with MQL4/MT4 ?


Thanks,

NG

 

You could retrieve the values with iHigh/iLow etc on PERIOD_H4 like this:

int shift=0;
double o=iOpen(NULL,PERIOD_H4,shift);
double h=iHigh(NULL,PERIOD_H4,shift);
double l=iLow(NULL,PERIOD_H4,shift);
double c=iClose(NULL,PERIOD_H4,shift);
 
lippmaje:

You could retrieve the values with iHigh/iLow etc on PERIOD_H4 like this:

Thanks for your reply Lippmaje.  I guess my first question is, is it possible to run  a code on two different timeframes/candle stick charts at the same exact time with MQL4, and have them work together(without aggregating data from a lower time frame to create the higher time frame candles) ?
 
biggy8133:
Thanks for your reply Lippmaje.  I guess my first question is, is it possible to run  a code on two different timeframes/candle stick charts at the same exact time with MQL4, and have them work together(without aggregating data from a lower time frame to create the higher time frame candles) ?
Yes, this should work both with MT4 and 5. Just run your EA on the lower timeframe. The calls above will return the right values. But be aware that the higher timeframe is still bound to its resolution. That is you get a new value for H4 only on 8:00 12:00 16:00 etc but not on 11:00. PERIOD_H4 does not mean 'for the last 4 hours', rather it's aligned with the time of day's hour modulo 4. And you are not running an EA against two different charts. Don't mix charts with timeframes. You are running an EA on exactly one chart object and you're able to make modifications to it such as drawing lines etc. But your EA is free to access candles of any instrument you may need, on any timeframe. You could query M5 candles from EURUSD or M30 candles from AMZN while your EA is sitting on a H1 Nasdaq chart, no problem as long as your terminal got that candle history.
 
lippmaje:
Yes, this should work both with MT4 and 5. Just run your EA on the lower timeframe. The calls above will return the right values. But be aware that the higher timeframe is still bound to its resolution. That is you get a new value for H4 only on 8:00 12:00 16:00 etc but not on 11:00. PERIOD_H4 does not mean 'for the last 4 hours', rather it's aligned with the time of day's hour modulo 4. And you are not running an EA against two different charts. Don't mix charts with timeframes. You are running an EA on exactly one chart object and you're able to make modifications to it such as drawing lines etc. But your EA is free to access candles of any instrument you may need, on any timeframe. You could query M5 candles from EURUSD or M30 candles from AMZN while your EA is sitting on a H1 Nasdaq chart, no problem as long as your terminal got that candle history.

Thanks for the detailed info.  One other question based on your reply. 

Is it in fact possible to run one EA on two different CHARTS (Not just time frames) at the same time.  This  means your code(EA) could plot data on two different charts (lets say EURUSD H1 and H4) at the same time, and have the charts talk to each other based on the results?

What I need is to run the same logic code(EA) on H1 and H4 and have them both plot the results they obtain on their own respective charts.  Then based on the results that I get from H4, H1 decides whether to enter a trade or not.  Is this scenario possible, and if so what is the best approach?

Thanks in advance for your help.

 

biggy8133: Is it in fact possible to run one EA on two different CHARTS (Not just time frames) at the same time.  This  means your code(EA) could plot data on two different charts (lets say EURUSD H1 and H4) at the same time, and have the charts talk to each other based on the results?

I think it's impossible to target two different charts with a single EA.

What I need is to run the same logic code(EA) on H1 and H4 and have them both plot the results they obtain on their own respective charts.  Then based on the results that I get from H4, H1 decides whether to enter a trade or not.  Is this scenario possible, and if so what is the best approach?

You could do this with a custom indicator and an EA. The indicator is responsible for updates to the chart drawing and provision of signals. The EA queries the signals from two such indicators (H1/H4) via iCustom and makes the trade decision.

 
1 - Edit Expert Advisors
Right click and modify

2 - Modify the ontick
void OnTick ()
{
// ---
// Backtest with multiple timeframes
double v = iRSI (NULL, PERIOD_M5,4, PRICE_CLOSE, 0);
GlobalVariableSet ("v", v);
Comment (v);

}

3 - To get value:
Print ("iRSI", GlobalVariableGet ("v"));

4 - Compile Specialist
5 - Compete Indicator
6 - click on expert and update
7 - Okay, backtesting within M1 will be able to use the value of M5
 
Douglas Tybel:

Please use the code button (Alt+S) when posting code.

Why would you need to Store the value in a Global Variable (Of the terminal)?

Just use the value from the iRSI call.

 
Keith Watford:

Please use the code button (Alt+S) when posting code.

Why would you need to Store the value in a Global Variable (Of the terminal)?

Just use the value from the iRSI call.

Thanks for the guidance, I'm new here.

you can't get the value of multiple timeframes in mt4, so you need to do this step by step.


If I'm backtesting with PERIOD_M1 and M5 I can't get the value of M5 only from M1. For this reason, I use the backtest expert that it can get the value and send by global variable.


1 - Edit Expert Advisors
Right click and modify

2 - Modify the ontick
void OnTick ()
{
// ---
// Backtest with multiple timeframes
double v = iRSI (NULL, PERIOD_M5,4, PRICE_CLOSE, 0);
GlobalVariableSet ("v", v);
Comment (v);

}

3 - To get value:
Print ("iRSI", GlobalVariableGet ("v"));

4 - Compile Specialist
5 - Compete Indicator
6 - click on expert and update
7 - Okay, backtesting within M1 will be able to use the value of M5
 
Douglas Tybel:

you can't get the value of multiple timeframes in mt4.......

Of course you can

Douglas Tybel:

If I'm backtesting with PERIOD_M1 and M5 I can't get the value of M5 only from M1. For this reason, I use the backtest expert that it can get the value and send by global variable.


What do you think that you are doing here?

double v = iRSI (NULL, PERIOD_M5,4, PRICE_CLOSE, 0);

You are getting the MTF value.

// Backtest with multiple timeframes
double v = iRSI (NULL, PERIOD_M5,4, PRICE_CLOSE, 0);
//GlobalVariableSet ("v", v);
Comment (v);


//3 - To get value:
//Print ("iRSI", GlobalVariableGet ("v"));
Print ("iRSI ", v);

Will get you the same value without using GVs

 
Keith Watford:

Of course you can

What do you think that you are doing here?

You are getting the MTF value.

Will get you the same value without using GVs

1 - Edit Expert Advisors

In the event OnTick () 

you can use another timeframe and store the value in a global variable

void OnTick ()
{
// ---
// Backtest with multiple timeframes
double v = iRSI (NULL, PERIOD_M5,4, PRICE_CLOSE, 0);
//save value in variable
GlobalVariableSet ("v", v);
//just show in the comments
Comment (v);

}

Now, to get the value inside the indicator you want to backtest in PERIOD_M1, just get the code below:


GlobalVariableGet ("v")


2 - Compile Specialist expert

3 - Compete Indicator

4 - click on expert and update

5 - Okay, backtesting within M1 will be able to use the value of M5

checkout this prints:

https://nimb.ws/20cMTz


then, this will backtest using data period_m5 and period_m1.


Do you understand?

Reason: