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?
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 :)

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?
Appreciate if anyone is able to shed any light on this.
Kind regards
James