Coding Issue Timeframe Key Down Event

 

Is anyone able to please help me with this code? I'm trying to have key "R" to cycle down through 4 timeframes (D1,H1,M5,M1) and key "F" to cycle up through the same 4 timeframes. My issue is that sometimes it seems to be working but mostly the ChartPeriod(); of the current chart is not returning the correct value when I change the timeframe and seems to get stuck on a timeframe value. Is there a way to force MT5 to refresh or update the ChartPeriod value so that it would work on every key down?

int chartCur = ChartPeriod();
int M1 = 1;
int M5 = 5;
int H1 = 16385;
int D1 = 16408;

if(lparam == KEY_R){
         if(chartCur == D1){
            ChartSetSymbolPeriod(0,_Symbol,PERIOD_H1);
         }     
         if(chartCur == H1){
            ChartSetSymbolPeriod(0,_Symbol,PERIOD_M5);
         }
         if(chartCur == M5){
            ChartSetSymbolPeriod(0,_Symbol,PERIOD_M1);
         }      
}                                        
if(lparam == KEY_F){
         if(chartCur == M1){
            ChartSetSymbolPeriod(0,_Symbol,PERIOD_M5);
         }
         if(chartCur == M5){
            ChartSetSymbolPeriod(0,_Symbol,PERIOD_H1);
         }
         if(chartCur == H1){
            ChartSetSymbolPeriod(0,_Symbol,PERIOD_D1);
         }
}

Appreciate if anyone is able to shed any light on this.


Kind regards

James

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Properties
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Properties
  • www.mql5.com
Object Properties - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Do not hard code constants.
int M1 = 1;
int M5 = 5;
int H1 = 16385;
int D1 = 16408;
Use the proper enumerations (ENUM_TIMEFRAMES)
PERIOD_M1, PERIOD_M5, …
 

Thank you William! It's working now!

int M1 = 1;
int M5 = 5;
int H1 = 16385;
int D1 = 16408;
      
      if(lparam == KEY_R){
         int chartCur = ChartPeriod();
         if(chartCur == D1){
            ChartSetSymbolPeriod(0,NULL,PERIOD_H1);
         }     
         if(chartCur == H1){
            ChartSetSymbolPeriod(0,NULL,PERIOD_M5);
         }
         if(chartCur == M5){
            ChartSetSymbolPeriod(0,NULL,PERIOD_M1);
         }      
      }                                           
      if(lparam == KEY_F){
         int chartCur = ChartPeriod();
         if(chartCur == M1){
            ChartSetSymbolPeriod(0,NULL,PERIOD_M5);
         }     
         if(chartCur == M5){
            ChartSetSymbolPeriod(0,NULL,PERIOD_H1);
         }
         if(chartCur == H1){
            ChartSetSymbolPeriod(0,NULL,PERIOD_D1);
         }
      }

This made it work.

Is it bad practise to hard code constants? or just not neccessary?

 
Jwpenfold #: Is it bad practise to hard code constants?

Yes, it's very bad practice! For example the time-frame constants are different between MQL4 and MQL5 and could change in the future. It also makes updating code very difficult when a constant needs to be changed.

That is why enumerations exist, so that you can have a mapping scheme between a mnemonic name and its respective constant.

So, use enumerations, and not hard-coded constants.

 

Good to know, thank you. I just started coding these last two weeks so you would probably have a field day looking at my code! hahaha!

Edit: I have changed it now using enumerations and it still works. Thanks :)