EA: what if MT4 re-connect ?

 

Hi,

I'm planning an EA, which will send a series of orders if trend develops well. The latter orders depend on former orders. And orders's sizes are not the same. The very first order's size, especially, is quite different.

So, I'm thinking: what if MT4 re-connect? ----even if I try to keep it running, often there is net problem with MT4. I surely don't want this EA send from the first order again. And I don't want EA just disabled.

Is there any trick for this?

Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Detect language Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh YiddishAfrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish
English (auto-detected) » Chinese (Simplified)

 
When you write Experts, you'll want to write them to withstand dis-connections and shut-downs at a minimum. Your Expert would have to loop through all orders to determine the current state before making decisions upon that state. If this is not clear, then show your code you're having problems with and/or provide better examples of what concerns you.
 

MT4 should somehow support the recovery after shutdown especially since when it is company "policy" with unpredictable auto-updates and restarts which cannot be disabled.

 
szgy74:

MT4 should somehow support the recovery after shutdown especially since when it is company "policy" with unpredictable auto-updates and restarts which cannot be disabled.

It's up to the coder to design a recoverable EA, how can MetaQuotes compensate for a power or hardware failure ? auto updates can be disabled, restarts are not automatic.
 

Thanks.

Now I have this idea, please take a look. Still thinking about the recover part, this is just a beginning :

static datetime EUT,GUT,AUT;               // store order(ONLY by this EA) open time for different currency pair 

bool EAEnabled = true;  
//--------------------------------------------------------------------

int start()
  {
   if (!EAEnabled)                                             // check if this EA disabed
   {
      Comment("there is manual open order , can't run EA.");
      return(0);
   }
 
 datetime EUTL=0,GUTL=0,AUTL=0;                          // time of latest order (including manual and EA) for each currency pair
 

 for(int i=OrdersTotal()-1;i>=0;i--)                        // if this currency has any order, find out latest order of them.
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()!=Symbol()) continue;
      //----------------------------------------------------if ordersymbol = symbol, then:
      switch(Symbol())
         {
         case 'EURUSD':
             EUTL= MathMax(OrderOpenTime(),EUTL);      // here may have data type error - "datetime" vs "mathmax double" ? how to fix? 
             break;
         case 'GBPUSD':
          ......
         default:
         }
   }
      
  
              
      // ------------------------ check if latest order is made by this EA
 switch(Symbol())                     
         {
          case 'EURUSD':
             if( EUT == EUTL ) break;      // made by EA, continue to send EA orders.
             else{                        //  not made by EA, will disable EA.
                   EAEnabled = false;                        
                   Comment("Still has manual open order, can NOT run EA.");
                   return(0);  
             }       
                                 
          case 'GBPUSD':
          ......
          default:
             Print("weird currency pair");
             return(0);
         }  

   ......
   
   //-----------------------------------finally, orders !!!
   int ticket=OrderSend(......);                  
   if(OrderSelect(ticket,SELECT_BY_TICKET)==true 
   {
      switch(Symbol())                     // store EA order 's time
         {
          case 'EURUSD':
             EUT = OrderOpenTime();  
             break;    
          case 'GBPUSD':
          ......
          default:
             Print("weird currency pair");
             return(0);
         }   
   
   }
......

But I'm wondering : what's the difference between disconnect and mt4 restart? Does disconnect also do things written in "deinit()", as well as restarting ?

 

You should use the Magic Number to track which orders your EA created. https://www.mql5.com/en/forum/106755

The MagicNumber is your own reference number that stays with the order.

 

Thank you very much for the tip, ydrol.

That's much better than my code above, LOL.

I'll move on to the recovery part.

 
joshatt: But I'm wondering : what's the difference between disconnect and mt4 restart? Does disconnect also do things written in "deinit()", as well as restarting ?
https://docs.mql4.com/constants/uninit Do you see disconnect?
 
WHRoeder:
https://docs.mql4.com/constants/uninit Do you see disconnect?


No. So it's impossible to tell?

 

joshatt2013.08.19 04:16

So, I'm thinking: what if MT4 re-connect? ----even if I try to keep it running, often there is net problem with MT4. I surely don't want this EA send from the first order again. And I don't want EA just disabled.

Is there any trick for this?

There is no reason to assume an EA would break when the connection to the broker is disconnected. It will just sit there waiting for a new tick to arrive just like it usually does. When the internet is reconnected and a new tick arrives the EA will continue its operation.

 
OK, thanks.
Reason: