Converting PERIOD to numbers and working with 2 periods in one EA

 

Hi.

It is possible to convert the PERIOD and the ENUM_TIMEFRAMES (PERIOD_M1, PERIOD_H1, PERIOD_D1, etc...)

into something like numbers, in example an int variable?


In example it is possible to convert PERIOD_M1 into an int?

something like


int peri = PERIOD_M1;


this is useful for me, for in example when using an EA with 2 indicators in it, and those indicators work

with 2 different period (in exmaple MA on a 5 minute period (PERIOD_M5) and MACD on a 15 minute period (PERIOD_M15).


if i set the common variable _PERIOD for both indicators in the EA, it would run on the same period i testing (or live trading),

in example if i test the EA in M5 it will have M5 in MA and MACD, if i test H1 it will have both period MA and MACD with H1.


I want something like to have in example MA with M5 period and MACD with M15 period.


It is possible?

if i try to do this, the compiler say me "can't convert enum".

from:

input int MAperi=0, MACDperi=0;


   macdd=iMACD(NULL,MACDperi,12,26,9,PRICE_CLOSE);

   maa=iMA(_Symbol,MAperi,75,0,MODE_SMA,PRICE_LOW);


in other words more, i need the case to optimze the PERIOD for 2 different indicators, with not the only

case they have the same PERIOD, but different periods too, preferably settable by an extern(input) int variable.


I remember that in mq4 there were a way to so something like this.

 

What about is it? Every indicator has two parameters - Symbol and Timeframe. For example, MACD:

int  iMACD(
   string              symbol,              // symbol name
   ENUM_TIMEFRAMES     period,              // period
   int                 fast_ema_period,     // period for Fast average calculation
   int                 slow_ema_period,     // period for Slow average calculation
   int                 signal_period,       // period for their difference averaging
   ENUM_APPLIED_PRICE  applied_price        // type of price or handle
   );

See also Chart Timeframes and function PeriodSeconds().


 
Rosh:

What about is it? Every indicator has to parameters - Symbol and Timeframe. For example, MACD:

See also Chart Timeframes and function PeriodSeconds().


I speak to the possibility when to using 2 indicators into an EA (an EA that use 2 indicators for trade, as conditions to open and close trades in example)

and the possibility to externally (input) set 2 different periods of those 2 indicators, in the Input Window of Optimization function

(in example input period for MA = 5 minutes and input period for MACD = 15 minutes).


I hope you got my point.


If, in example, there is the possibility to convert the

PERIOD_M5 into something like int period=5,

and PERIOD_M15 into something like int period=15,

with the int period variable running as an integer value for 1 minutes (1 means 1 minute period, 5 means 5 minute period,

60 means 1 hour period, 240 means 4h period, etc..)



 

Please, read about Input Variables and Enumerations, then try this simple EA and you will the same picture:


//+------------------------------------------------------------------+
//|                                  Demo_Periods_For_Indicators.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//--- input parameters
input ENUM_TIMEFRAMES      first_period=PERIOD_M15;
input ENUM_TIMEFRAMES      second_period=PERIOD_H1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+

 

Even more, you can add description for every input parameter, for example, rewrite the code above

//--- input parameters
input ENUM_TIMEFRAMES      first_period=PERIOD_M15; // Period for MA indicator
input ENUM_TIMEFRAMES      second_period=PERIOD_H1; // Period for MACD

Compile again and run EA:


 

Thanks for the reply.

Interesting how qith mq5 we can have human descriptions of the timeframes.

But it does not satisfy my question at all.


Controversely, what i need is the exactly contrary:

i want the possibility to turn/switch/convert the enum time frames (1 minute, 2 minute, 20 minute, 1 hour, etc...) into an

integer variable, for let me switching the values during optimization, something like:

int a=1;  //  where 1 is M1 (or enum time frame / case of    M1)

int a=2; // where 2 is M2 (or enum time frame / case of   M2)

// etc...

int a=6; // where 6 is M6 ior enum time frame / case of   M6)

int a=7; // where 7 is M10 (or enum time frame / case of   M10)

int a=8; //where 8 is M12 (or enum time frame / case of   M12)

// etc...

int a=12; // where 12 is H1 (or enum time frame / case of   H1)

int a=13; // where 13 is H2 (or enum time frame / case of   H2)


in other words, a possibility to optimize 2 variable into an EA that uses 2 indicators:

those variable to be switched/optimized are the

TIMEFRAME OF INDICATOR1 AND TIMEFREAME OF INDICATOR2

with the way to optimize them, in a fashion like the optimizator engine:

start (0), step(1), stop(13)

where 0 is the current chart timeframe

1 is m1

2 is m2

...

6 is m6

..

12 is h1

etc..



 
I hope you are going to study how to insert code properly.

 
Rosh:
I hope you are going to study how to insert code properly.

Yes, i'll do.

My previous reply was not true code inserting, but just a series of examples to

let me explain better what i demand for.

I'm going to edit my post.


I think, what i need is a programmer's trick more than a guide to the correctness of the code

or how to use timeframes and periods.

In more other words, i simply want the possibility to step, during optimization, the timeframes

like they are extern(input) integer variables, starting from 0(current timeframe choosen in the

optimization window), going on with 1(first time frame, so M1), and so on with 2(second time frame, so M2),

etc..

In this way i can optimize the EA with in it 2 indicators, by different time frame working with them

(in example MA=M1 and MACD=M1; MA=M2 and MACD=H1, MA=D1 and MACD=H4, etc...) with all combinations

possible.


Regards

 

Try to optimize this EA in Strategy Tester. Why not?



 
Rosh:

Try to optimize this EA in Strategy Tester. Why not?



Whoa!

It is possible to do this?

Great.


I was turning around a method of conversion of enum time frames into integers for

the optimization/combinations of the time frames, and it is a still implemented feature.

I was not knowning that in mt5 was possible to optimize (stepping) also time frames.

This make the MT5 the perfect forex optimizer!


This  last reply solved all my problems.

Thanks!

 

If you have a time frame current

and you want to get another multiple timeframe, you can only do it with string conversion:



uint second;

string val_seconds;

// Example

// current period PERIOD_M5

// second period PERIOD_M30


if (seconds == 300) // PERIOD_M5

last_period = PERIOD_M30;

val_seconds = EnumToString (PERIOD_M30);


You can not convert val_seconds to int because

ENUM_TIMEFRAME can only convert for long whole lengths

2E + 14 (16384

Reason: