Keeping EA panel settings when the user changes timeframe - static variables, GlobalVariables or a file?

Hello everyone,

A user reviewing one of my CodeBase EAs found a bug that I think many panel EAs have: when you switch the chart timeframe, the EA is unloaded and loaded again (OnDeinit with REASON_CHARTCHANGE, then OnInit), and everything the user changed on the panel goes back to the input defaults. In my case it was the lot mode and the fixed lot size, which is dangerous, because the next click can open a trade with the wrong volume.

These are the options I am considering:

1. Static/global variables in the program. They keep their values on REASON_CHARTCHANGE, but not when the terminal restarts or the EA is removed and attached again.

2. Terminal GlobalVariables (GlobalVariableSet with a prefix of symbol + magic + chart ID). They survive restarts, but they only store doubles, and I need to clean them up when the EA is removed (REASON_REMOVE).

3. A small file in MQL5\Files (one per chart), written only when a value changes and read in OnInit. More flexible (strings, several fields), but more code and more chances for a corrupted file.

Whichever I use, I plan to check the deinit reason, so a real removal resets everything and only a timeframe/symbol change or restart restores the saved panel state. I also want to show clearly on the panel when a restored value is different from the input default.

Questions for other developers:

- Which method do you use to keep panel state, and why?

- Do you restore the state after a terminal restart, or only after a timeframe change? Restoring an old lot size after a restart could also surprise the user.

- How do you handle the same EA running on two charts of the same symbol, so they do not overwrite each other's state?

Interested to hear your experience.

Thank you. 

It's not a bug, it's a feature. Mql5 documentations says on chart timeframe change ea deinitialize and initialize again.

I use Global Variables with magic number, ile options is also good.
Rajesh Kumar Nait #:
It's not a bug, it's a feature. Mql5 documentations says on chart timeframe change ea deinitialize and initialize again.

I use Global Variables with magic number, ile options is also good.

thanks