Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
For example:
- I make no change in my Expert Advisor mql5 file
- I compile EA mql5 file
- I run the Single test in Strategy tester - runs ok, no problems
- I run the same test again - tester stopped because OnInit returns non-zero code (error 5035)
Is there something happening during the compilation that flushes some variables? or is is something else?
My OnInit function looks like this, during the second run GetLastError returns 5035 even though _LastError variable is reset before OnInit stars
You are not supposed to be referencing quote information, or any trade data at all, in the OnInit() event handler, which is for initialisation only.
There is no guarantee that any data will be available at all during the initialisation process, and you should release the event quickly, or you will hold up the event queue.
Your main logic should be in the OnTick() event handler.
Forum on trading, automated trading systems and testing trading strategies
Getting the bar opening time (SERIES_LASTBAR_DATE) having the position opening time
Fernando Carreiro, 2023.09.17 00:35
Because the OnInit should be used for initialisation only. There are many times when trade data has not even been loaded yet and not available during the initialisation phase.
All data coming from the trade server should be read in either the OnTick (EAs) or OnCalculate (Indicators) event handlers, not the OnInit.
Create a startup routine in OnTick/OnCalculte for this. Don't do it in OnInit.
Forum on trading, automated trading systems and testing trading strategies
Getting the bar opening time (SERIES_LASTBAR_DATE) having the position opening time
Fernando Carreiro, 2023.09.17 03:16
No! Simply use a static boolean variable to detect the first-time use.
void OnTick() { static bool bFirstTime = true; if( bFirstTime ) { bFirstTime = false; // ... carry out a start-up procedure }; // ... do other new tick event handling };
Forum on trading, automated trading systems and testing trading strategies
Fernando Carreiro, 2023.04.13 18:23
Don't do any trade related tasks in the OnInit() nor even data feed realted tasks. That is not the purpose of that event handler.
During the execution of OnInit() there may be no connection to the trade server yet, so do it in the OnTick() only after the arrival of the first tick.
And again, don't use a loop with "sleep". That blocks the thread. Use an event driven architecture with a state machine instead.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I would like to ask for help with my Expert Advisor that runs only first time after compilation.
Every other attempt to run EA fails with OnInit with runtime error code 5035.
For example:
Is there something happening during the compilation that flushes some variables? or is is something else?
My OnInit function looks like this, during the second run GetLastError returns 5035 even though _LastError variable is reset before OnInit stars
Thanks for any tip