Expert Advisor to work after shutdown

 
Can you please write here, from your experience, what are your best precautions to add to an Expert Advisor code lines, so it will resume the best way, after restarting the metatrader4/computer?

In other words:
What are the scenarios you may think that could happen after EA starts working again, if metatrader 4 platform needed to restart?
I know Global Variables (GV), but please elaborate on some scenarios that may happen and how can the EA overcome these cases if they happen in real?
 
6969111:
Can you please write here, from your experience, what are your best precautions to add to an Expert Advisor code lines, so it will resume the best way, after restarting the metatrader4/computer?

In other words:
What are the scenarios you may think that could happen after EA starts working again, if metatrader 4 platform needed to restart?
I know Global Variables (GV), but please elaborate on some scenarios that may happen and how can the EA overcome these cases if they happen in real?

you need to be careful with the variables you use and how you code, because if there is a VPS crash, those variables will be lost.

so always use "PositionXXXX()" and analyse the opened trades rather than relying upon variables.

 

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 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 to recover, 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.