if the electric energy goes away???

 

hi guys ...i have a problem. i have noticed that if suddenly the electric energy goes away or maybe the computer turns off or maybe i turn on my first account and then come back in the second one ..... when i re open the platform...... my EA close all positions . which could be the reason??

my EA are easy.... i check the number of pending order and if sends an order if the some conditions are ok but these conditions take in account the price of the first order opened. what do u think???? it seems like it loses the reference of the first order


i use this ;

orderselect(ticket,select by pos, mode trades)
if ( orderopenprice()-ask > x )
for (cnt=0;cnt<total;cnt++)
orderselect(cnt,select by pos, mode trades )
if(ordertype==op sell && ordersymbol==symbol )
orderclose(orderticket(),orderlots(),ask,3,violet)
return(0)

if i go on my second account when i come back ......the experts are not activated.....so i have to turn on manually....... but today in this way....all my positions had been closed. the problem is this ???? i have to maintan always the same screen on one account???



 
Post or attach your code and in case the code isn't too long and you decide to post it don't forget to use the SRC button above.
 
giulioron:

i have noticed that if suddenly the electric energy goes away or maybe the computer turns off or maybe i turn on my first account and then come back in the second one ..... when i re open the platform...... my EA close all positions . which could be the reason??

I guess you don't have a persistence layer in your EA...? Experts must be designed to 'deal' with the situation of a total failure (Terminal crashes, power-out, etc.). The code involved with recovering after starting the Terminal/Expert again is called a persistence layer. Here's a good discussion about it -> https://www.mql5.com/en/forum/119716.

Anyway, we need to see your code to help.
 
orderselect(ticket,select by pos, mode trades)
if ( orderopenprice()-ask > x )
for (cnt=0;cnt<total;cnt++)
orderselect(cnt,select by pos, mode trades )
if(ordertype==op sell && ordersymbol==symbol )
orderclose(orderticket(),orderlots(),ask,3,violet)
I assume that's not a cut and paste since the braces are missing, capitalization, etc.
  1. on startup ticket would be undefined, the first orderselect fails and the if is bogus
  2. in the for loop you don't check orderselect
  3. when you close order position zero, order position one becomes position zero, so you close every other order. Always count down.
for(int index = OrdersTotal() - 1; index >= 0; index--) if (
    OrderSelect(index, SELECT_BY_POS)     // Only my orders w/
&&  OrderMagicNumber()  == MagicNumber    // my magic number
&&  OrderSymbol()       == Symbol() ) {   // and period and symbol
   // now check for closing, modifying etc.
Reason: