Can you help me turn these steps into code

hello, now I need operate different period in my EA,

requirement:

1, loop in MT5 periods, like h1,h2,h3…to mn1.

2, print period in loop which working, like PERIOD_H1, PERIOD_H2…

processes like below

for (iPeriod=h1, iPeriod <=mn1, iPeriod++)

{

analyze iCCI on iPeriod; //some operations with iPeriod

Print("Current period is ",iPeriod);

}

Anyone has experience on this or paste example here? Thanks in advance.

What’s your problem actually ? It seems you started well…

  • for (iPeriod=h1, iPeriod <=mn1, iPeriod++){

You can’t do that because Chart Timeframesare not consecutive. Incrementing PERIOD_H1 (60) give 61 not PERIOD_H2 (120). You need to create appropriate functions.

MT$ Code:


     /** @defgroup tf Time Frames
     * A simple enumeration of time frames and related functions.
     * @{                                                                         */
    /** Whereas `ENUM_TIMEFRAMES` is all timeframes (supported and unsupported,) but
     * it can't be incremented because it's values is number of minutes,
     * (incrementing M30 would be M31 not H1.) This is a simple enumeration
     * (incrementing this results in the next one.)                               */
    enum Timeframe{      TF_CURRENT,    ///< Place holder for the EA attached chart.
       TF_M1,      TF_M2,      TF_M3,      TF_M4,      TF_M5,      TF_M6,
       TF_M10,     TF_M12,     TF_M15,     TF_M20,     TF_M30,     TF_H1,
       TF_H2,      TF_H3,      TF_H4,      TF_H6,      TF_H8,      TF_H12,
       TF_D1,      TF_W1,      TF_MN1};
       const Timeframe   TF_FIRST = TF_M1;    ///< Smallest supported Timeframe.
       const Timeframe   TF_Last  = TF_MN1;   ///< Largest supported Timeframe.
       #define           TF_COUNT   TF_MN1    /**< Number of supported Timeframe%s
                                      * not including the @ref TF_CURRENT marker. */
    // For converting between periods (as_period) and timeframes (as_timeframe.)
    /// @cond Skip
    const ENUM_TIMEFRAMES  gcPeriods[]={   PERIOD_CURRENT,
       PERIOD_M1,  PERIOD_M2,  PERIOD_M3,  PERIOD_M4,  PERIOD_M5,  PERIOD_M6,
       PERIOD_M10, PERIOD_M12, PERIOD_M15, PERIOD_M20, PERIOD_M30, PERIOD_H1,
       PERIOD_H2,  PERIOD_H3,  PERIOD_H4,  PERIOD_H6,  PERIOD_H8,  PERIOD_H12,
       PERIOD_D1,  PERIOD_W1,  PERIOD_MN1};
    /// @endcond
    /// Convert a period to a timeframe.
    Timeframe            as_timeframe(ENUM_TIMEFRAMES period=PERIOD_CURRENT){
       if(period == PERIOD_CURRENT)  period   = (ENUM_TIMEFRAMES) _Period;
       Timeframe tf = TF_M1;   while(gcPeriods[tf] < period) ++tf;
       return tf;
    }
    /// Convert a timeframe to a period;
    ENUM_TIMEFRAMES   as_period(Timeframe tf){         return gcPeriods[tf];       }
    /** Returns the next larger timeframe. Larger timeframes should be 5 times
     * larger (approximately,) but M5 to M15 is 3, and M15 to M30 and M30 to H1 is
     * only 2. So for those I skip two sizes. H1 to H4 is 4 so I allow that.      */
    Timeframe         larger(Timeframe tf){
       static const Timeframe  larger[] = {   WRONG_VALUE,
       // TF_M1,      TF_M2,      TF_M3,      TF_M4,      TF_M5,      TF_M6,
          TF_M5,      TF_M10,     TF_M15,     TF_M20,     TF_M30,     TF_M30,
       // TF_M10,     TF_M12,     TF_M15,     TF_M20,     TF_M30,     TF_H1,
          TF_H1,      TF_H1,      TF_H1,      TF_H2,      TF_H2,      TF_H4,
       // TF_H2,      TF_H3,      TF_H4,      TF_H6,      TF_H8,      TF_H12,
          TF_H12,     TF_H12,     TF_D1,      TF_D1,      TF_D1,      TF_D1,
       // TF_D1,      TF_W1,      TF_MN1
          TF_W1,      TF_MN1,     WRONG_VALUE};
       return larger[tf];
    }
    ENUM_TIMEFRAMES   next_larger(ENUM_TIMEFRAMES period){
       return as_period(as_timeframe(period) + 1);
    }
    // defgroup tf Time Frames
    /// @}

Just mindmap, my problem is transfer it to real code in mql5. :slight_smile:

Thanks your code first, could you please provide code in mql5?

 ENUM_TIMEFRAMES periods[22]={ PERIOD_CURRENT, PERIOD_M1,  PERIOD_M2,
                                 PERIOD_M3,      PERIOD_M4,  PERIOD_M5,
                                 PERIOD_M6,      PERIOD_M10, PERIOD_M12,
                                 PERIOD_M15,     PERIOD_M20, PERIOD_M30,
                                 PERIOD_H1,      PERIOD_H2,  PERIOD_H3,
                                 PERIOD_H4,      PERIOD_H6,  PERIOD_H8,
                                 PERIOD_H12,     PERIOD_D1,  PERIOD_W1,
                                 PERIOD_MN1};
   for(int i=0; i<22; i++)
     {
      // do your analysis using periods[i]
      Print("Analysed ",EnumToString(periods[i]));
     }

I don’t have MT5. I gave you my code . You try to compile it and see if anything needs to change.

Yes, you can easily do this if you use an iterator class. Here is one I made for such an occasion.

Example use:

#include <TimeFrames.mqh>
void OnStart()
{
   CTimeFrames tfs;  // using all periods when calling the default constructor
   string res = "";
   for(tfs = PERIOD_H1; tfs <= PERIOD_D1;tfs++)
   {
      res+= tfs.ToString()+", ";
      cci_func(tfs.Period());
   }
   Print(res);
}

You can even filter it down to only use the time-frames you select.

 CTimeFrames f(FLAG_M1|FLAG_M5|FLAG_H1|FLAG_D1); //filtered time-frames using parameterized constructor
   //you could have also call the SetPeriodsByFlags method
   //f.SetPeriodsByFlags(FLAG_M1|FLAG_M5|FLAG_H1); //set timeframes by flags
   Print(f.AllToString());
   
   f+= PERIOD_M15; // add a time-frame to list
   Print("Added time-frame ",f.ToString());
   
   f=PERIOD_H1;
   Print("Changed time-frame to ",f.ToString());
   
   res = "";
   for(f.GetFirst();!f.Last();f.Next()) //iterate forward over all time-frames in list
      res+= f.ToString()+", ";
   Print(res);
   
   res="";
   for(f.GetLast();!f.First();f.Prev()) //iterate backward over all time-frames in list
      res+=f.ToString()+", ";
   Print(res);

Files:

TimeFrames.mqh 8 kb

TF_Iterate.mq5 2 kb

Thank you, I have solved it already, thank you again.

your reply very helpful to me,thanks your code. additionally, do you have any idea about period loop? like start from PERIOD_H1 and analyze some symbol info, if failed then escalate to the next, till all conditions are meet.

Great! your answer and code satisfied my requirements.

I will study/test it, thank you so much.