TimeFrame of the Chart

 

Hi guys, is there a method to get the current timeframe of the chart?

I need to use this for allowing the EA to only run on 1H, or 4H time frames and alert if any other time frame is activated to turn off the EA.

Thanks,

 
c0d3:

Hi guys, is there a method to get the current timeframe of the chart?

I need to use this for allowing the EA to only run on 1H, or 4H time frames and alert if any other time frame is activated to turn off the EA.

Thanks,


Found a solution, but it does depends on the number of Bars being different between timeframes

here is the method, in case someone needs it

int checkTimeFrame()
   {
     if(Bars!=iBars(Symbol(),PERIOD_H4))
     {
         Alert("Wrong chart timeframe");
         return(0);
     }
   }   
 
c0d3:

Hi guys, is there a method to get the current timeframe of the chart?

I need to use this for allowing the EA to only run on 1H, or 4H time frames and alert if any other time frame is activated to turn off the EA.

Thanks,

Period()

Asked earlier today . . . https://www.mql5.com/en/forum/139280

Why does it matter what timeframe the chart is ?

 

I do it this way ...

int init(){

   ...

   if( Period()!= PERIOD_M5 ){
      Comment("ERROR: You must use M5 timeframe");
      MessageBox( "ERROR: Needs M5 timeframe.\nRun Aborted", ADVISORNAME, MB_ICONSTOP );      
      abort=true;
      return( -1 );
   }

   return(0);
}

//--------------------------------------------------------------------------------------+
int start(){
  
   if( abort )
      return( 0 );

   ...
}
 
RaptorUK:

Why does it matter what timeframe the chart is ?


I only want my EA to run on H4, and skip all other time frames.

 
Thanks for the replies, Period() works better than Bars
 
c0d3:


I only want my EA to run on H4, and skip all other time frames.

I understand that . . . but why does it have to run on H4 to trade H4 ? not saying what you are doing is wrong, but you do have other options that you should be aware of.
 
RaptorUK:
I understand that . . . but why does it have to run on H4 to trade H4 ? not saying what you are doing is wrong, but you do have other options that you should be aware of

  • For my new EA experiment, there is an order every new bar, if signals show a buy or a sell.
  • If trading on H1, there are 4x more trades, which increases EA's market exposure.
  • Using H4 timeframe gives the EA enough break between trades.
  • Also when I start MT4, and I open a new chart, it defaults to H1, and I miss this sometimes and go ahead and apply the EA to H1, and let it run...
  • To prevent human error, and to limit the number of trades, I programatically check for H4, only then allow the EA to trade.
What other options are you referring 2??

 
c0d3:

    What other options are you referring 2??

    Well he is pointing out that anything you can do on an H4 chart you can also do on an M1 chart (for example) if you modify the code to avoid any references to the chart specific variables. Instead of using Time[] you would use iTime() for example. You will still be using your chosen timeframe for all your trade decisions by accessing the data using functions rather than the built in arrays.

    Close[] -> iClose()

    Open[] -> iOpen()

     
    dabbler:

    Well he is pointing out that anything you can do on an H4 chart you can also do on an M1 chart (for example)

    I see what you mean, and I do agree...
     
    dabbler:

    Well he is pointing out that anything you can do on an H4 chart you can also do on an M1 chart (for example) if you modify the code to avoid any references to the chart specific variables. Instead of using Time[] you would use iTime() for example. You will still be using your chosen timeframe for all your trade decisions by accessing the data using functions rather than the built in arrays.

    Close[] -> iClose()

    Open[] -> iOpen()

    Exactly . . . not saying DO IT ! just be aware and then you have the option :-)
    Reason: