Error 138 "requote".

 

Hello,

Perhaps someone could explain to me, why the MQL4 tester deliveres sometimes an error 138 "requote", when my EA tries to close an Open Position.

I thought that the tester uses "local" date and doesn't comunicate with the broker.

Thanks

 

Error 138 "The requested price has become out of date or bid and ask prices have been mixed up. The data can be refreshed without any delay using theRefreshRates function and make a retry. If the error does not disappear, all attempts to trade must be stopped, the program logic must be changed."

For example: If you store the value of Ask ( or for that matter any of the other Predefined variables: https://docs.mql4.com/predefined/variables ) in a globally defined variable Ask_Price and then a few ticks later you try to use that variable to open a Buy then the ask price you are using may well be out of date . . . You can use RefreshRates() to refresh the Predefined Variables so they have current, not out of date, values.

 
//****************************************************************
// Trade Management Close Open Position         
//****************************************************************

  int Trade_mgmt_modify_close()
      { 
      
       Error_A_flag = 1;
       while(Error_A_flag == 1)                                        // Loop for error handling. If Error_A_flag == 1, trading action needs to be
                                                                       // repeted. If Error_A_flag == 0 or trading action successful, the loop is canceled
            {                                     
            
             Error_time=TimeToStr(iTime(Symbol(),Period(),0),TIME_SECONDS);
             Alert (Symbol(),period," ",Error_time,"  Modification ",OrderTicket_TM,". Close Open Position, Awaiting response..");
       
             bool Ans_del=OrderClose(OrderTicket_TM,OrderLots(),Ask,1,Red);


             if (Ans_del==true) 
                {
                 Error_time=TimeToStr(iTime(Symbol(),Period(),0),TIME_SECONDS);
                 Alert (Symbol(),period," ",Error_time,"  Order  ",OrderTicket()," Open Position closed ");

                 return;
          
                }else
                {
           
                 Error_trade_mgmt();
                 if(Error_A_flag == 0) return;
                }
      
             }                                                         // End_while(Error_A_flag == 1)
        return;
      }                                                                // End Trade_mgmt_modify_close()

As you see, in order to close a Buy or Sell Open Position, I close "At the market" using the Ask price.

I thought, as date for the tester is stored locally, this Ask price should be available and not be a issue for a requote fom the broker.

Do you know, if the tester depends on market info from the broker?

 

Do you know, if the tester depends on market info from the broker? Tester depends on MarketInfo (Test Symbol ONLY) for Ask & Bid which are provided in the data file. Your Spreads comes from the broker. That's if you are connected to the broker during a back-test. If Not then Spreads would be the last used at dis-connect from the broker.

bool Ans_del=OrderClose(OrderTicket_TM,OrderLots(),Ask,1,Red);

You Buy@Ask and Sell@Bid. Your code above will NOT work for Long Orders. It violates the first rule:

"Error 138 "...or bid and ask prices have been mixed up"

 

Oh boy, yes, you are right.... Thanks.

Sometimes I don't see the forest, because there are to many trees....

 

I changed the code, testing for OderType:

             if(OrderType_TM == 0) bool Ans_del=OrderClose(OrderTicket_TM,OrderLots(),Ask,1,Red);
             if(OrderType_TM == 1)      Ans_del=OrderClose(OrderTicket_TM,OrderLots(),Bid,1,Red);

Using the tester, for a SELL, I still recive a error 138.
What do I miss?
 
You have them the wrong way round, you close a OP_BUY by selling and an OP_SELL by buying . . .
 
OK, thanks.
 
  1. You Buy at the Ask, Sell at the Bid. So for a Buy you close at the Bid (the sell price.) Or you can just use this for either direction:
    if (!OrderSelect(OrderTicket_TM, SELECT_BY_TICKET))
        Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),
                          Slippage.Pips*pips2points, op.color ))
        Alert("OrderClose failed: ", GetLastError());
    
    
  2. iTime(Symbol(),Period(),0)
    Why are you using function calls identical to the predefined variables, i.e. Time[0].
 

You ask, why I use iTime instead of Time[0] ?

Well I'm not yet familliar with all the functionality of MQL4. I use what works for me.

With more experiance I exchange current functionality with more clever solutions.

Reason: