Download MetaTrader 5
To add comments, please log in or register
Do you know C++? MQL5 has much in common with it. Just try it yourself!
jon
424
jon 2016.09.12 21:42 

When a disconnection occurs when an EA tries to open an order. 

Is there a way to ensure that it would place that last order once connection is established again?

(Regardless of the conditions that were set for the order to be opened in the first place.)

Fernando Carreiro
1571
Fernando Carreiro 2016.09.12 23:09  
johnnybegoode: When a disconnection occurs when an EA tries to open an order. Is there a way to ensure that it would place that last order once connection is established again? (Regardless of the conditions that were set for the order to be opened in the first place.)

Yes, absolutely! An EA should always be coded to be able to recover from a disconnect or even a PC Crash. It should be coded to store its state, and to pick up where it left off, such as a disconnection.

However, you should not just retry the order without reconsidering the new market state. Consider for example, that during the disconnected period, market conditions changed sufficiently that would no longer comply with your original signal criteria and once the connection was reestablished it would be a very bad idea to place the order.

So, I would suggest, that you code your EA, so that in the event of disconnects or crash, that it restart from a known base state and have it reevaluate market conditions and signals (and any open trades), and only then, if still valid, would it proceed with placing the order adjusting for new prices quotes (and/or also manage any open trades accordingly).

jon
424
jon 2016.09.13 00:40  

Lets say the strategy requires me to place that order regardless of the conditions

How to tell the EA to place that order it missed once connection resume. 

Fernando Carreiro
1571
Fernando Carreiro 2016.09.13 01:37  
johnnybegoode: Lets say the strategy requires me to place that order regardless of the conditions. How to tell the EA to place that order once connection resume.

I don't quite get what the exact nature of your difficulty is. Maybe you should show some code so that we can understand what it is that you are having trouble with.

Just monitor (and react to) the normal Checkup points, that you would normally verify during the EA runtime - errors (_LastError), connection (IsConnected), trade context (IsTradeContextBusy), etc.

In other words, you could use a simplistic state machine (or any other appropriate method), to keep track of what has to be done depending on what state the EA is in.

EDIT: Here is a good reference PDF that might help you understand that the difficulty you might be facing is not so much a coding issue, but a basic design (or human thought) issue:

However, if that is too mathematical for you, here are a few links to simpler explanations:

jon
424
jon 2016.09.13 06:51  
      int ticket=OrderSend(Symbol_1,OP_BUY,LongLot,ask,slippage,0,0,"My EA",12345,0,Green);         
      if(ticket>0)
      {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
         {
         Print("BUY " ,Symbol_1 ," order opened : ",OrderOpenPrice());
         
         }
      }
         else Print("Error opening BUY " ,Symbol_1 ," order : ",GetLastError());
         return;  

 I receive error 128Trade timeout.

if (int GetLastError() ==  128)

int ticket=OrderSend(Symbol_1,OP_BUY,LongLot,ask,slippage,0,0,"My EA",12345,0,Green); 

Fernando Carreiro
1571
Fernando Carreiro 2016.09.13 08:50  
johnnybegoode: I receive error 128 - Trade timeout.

if (int GetLastError() ==  128)
int ticket=OrderSend(Symbol_1,OP_BUY,LongLot,ask,slippage,0,0,"My EA",12345,0,Green);

"ERR_TRADE_TIMEOUT" is not necessarily a disconnection. However, from your code, I see that it seems that maybe the concept of "state machines" or even "event driven architecture", may be a little beyond your knowledge and experience to be able to implement, so maybe you can just try something a little simpler - maybe you can just code a simple retry loop:

// Please note: Uncompiled and untested!!!

input int
   intRetryCount = 3,   // Order Retry Count
   intSleepTime  = 100; // Retry Sleep Time

...

void someFunction( void )
{
   ...

   int intOrderRetryCount = IsTesting() ? 0 : intRetryCount;
   
   do
   {
      ResetLastError(); // Reset Error Codes
   
      int intTicket = OrderSend(
            _Symbol, intType, dblLots, dblOpenPrice,
            intSlippagePoints, dblStopLossPrice, dblTakeProfitPrice, strComment,
            intMagic, 0, intColour );
   
      if( intTicket != EMPTY ) break;
      
      // Test for various Error codes
      switch( _LastError )
      {
         case ERR_INVALID_STOPS:
            // Do something accordingly in this case
            ...
            // Don't Retry and Exit Loop
            intOrderRetryCount = 0;
            break;
      
         case ERR_TRADE_TIMEOUT:
            // Do something accordingly in this case
            break;
            
         ...
         
         // etc for all Error codes you wish to handle
         
         ...

         default:
            // Do something accordingly in this case
            break;
      }
   
      intOrderRetryCount--;
      if( intOrderRetryCount > 0 ) Sleep( intSleepTime );
   }
   while( intOrderRetryCount > 0 );

   ...
}
/
To add comments, please log in or register