Whenever the server goes offline, EA will reset. Is there any way to solve this problem?

 


As shown in the picture, as long as the server goes offline and reconnects, EA will reset.
It often resets the strategy logic.

Is there any way to solve it?

 
Yu Zhang:
It often resets the strategy logic.

This is an implementation issue, not a strategy logic issue.

Yu Zhang:
Is there any way to solve it?

Fix the unreliable code, specifically, ensure proper operation recovery after an EA restart. I thought the ability to restore operation after a restart was an obvious minimum requirement for any EA.

Yu Zhang:
as long as the server goes offline and reconnects, EA will reset

It works as intended.

https://www.mql5.com/en/docs/runtime/running

Expert Advisors are loaded in the following cases:

  • connection to an account, even if the account number is the same (if the Expert Advisor was attached to the chart before the authorization of the terminal on the server).
 
Yu ZhangEA will reset.

Depends on how it is coded.

EAs must be coded to recover. If the power fails, OS crashes, terminal, or chart is accidentally closed, on the next tick, any static / global ticket variables will have been lost. You will have an open order / position but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

On a network disconnection, you might get ERR_NO_RESULT or ERR_TRADE_TIMEOUT. There is nothing to be done, but log the error, return and wait for a reconnection and a new tick. Then reevaluate. Was an order opened or is the condition still valid.

 
A simple solution for a simple EA that only trades one position at a time, is to call and Print() PositionsTotal() within OnInit(), insert PositionsTotal() == 0 into your entry conditions within OnTick(), and PositionsTotal() == 1 into your exit conditions within OnTick(). Of course, you'll still presumably need to select a position to detect whether it's a long or short position─unless the EA is "always-in-the-market." I've used this method and never had a problem with it.
 
Ryan L Johnson # Print() PositionsTotal() within OnInit(), 
Wrong.
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.