Pending Order should cancel but does not,

 
//Enumerations
   enum DateTimeToDays
     {
     s1DAY    = 1,   // 1 DAY
     s2DAY    = 2,   // 2 DAY
     s3DAY    = 3,   // 3 DAY
     };

// Inputs

input DateTimeToDays      Inp_Pending_Expire_Date     = s3DAY;         // Days till Pending Order Expiration

// Global
int PendingDate; 
//Function

void DaysForPending()
  
  {
   PendingDate = 0; //Zero Out Pending Date
  
   if(Inp_Pending_Expire_Date == 1)
   {
    PendingDate = 86400;
   }

   if(Inp_Pending_Expire_Date == 2)
   {
    PendingDate = 172800;
   }

   if(Inp_Pending_Expire_Date == 3)
   {
    PendingDate = 259200;
   }
}
//Pending Order Execution
void LongPositionOpen()
  {
   datetime tm=TimeCurrent();
   datetime expiretm=tm+PendingDate;
 
  MqlTradeRequest mrequest;                             
   MqlTradeResult mresult;                           
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);

   if(!PositionSelect(_Symbol))
     {
     
       mrequest.action=TRADE_ACTION_PENDING;
       mrequest.magic=0;
       mrequest.symbol=Symbol();
       mrequest.volume=Inp_Lot;
       mrequest.price=NormalizeDouble(MRate[1].high,_Digits);
       mrequest.sl=NormalizeDouble(iEnvelope[1],_Digits);   // Stop Loss level of the order
       mrequest.tp=NormalizeDouble(iEnvelope[1],_Digits);   // Take Profit level of the order
       mrequest.deviation=5;                                // Maximal possible deviation from the requested price
       mrequest.type=ORDER_TYPE_BUY_STOP;
       mrequest.type_filling=ORDER_FILLING_RETURN;
       mrequest.type_time=ORDER_TIME_GTC;
       mrequest.expiration=expiretm;
       mrequest.comment=""; 
       //OrderSend(mrequest,mresult);          
       if(OrderSend(mrequest,mresult))
           {
            if(mresult.retcode==TRADE_RETCODE_PLACED || mresult.retcode==TRADE_RETCODE_DONE)
              {
               Print("[TICKET] Order ticket: ",mresult.order," retcode: ",mresult.retcode);
              }
            else
              {
               Print("Order error: ",GetLastError()," retcode: ",mresult.retcode);
               ResetLastError();
               return;
              }
           }
         else
           {
            Print("Order Send error.");
            ResetLastError();
            return;
           }   
     }
  }
 
wehsnim:

Did you check the documentation ? Also you have to be sure that an expiration mode is allowed for your symbol.

SYMBOL_EXPIRATION_GTC

1

The order is valid during the unlimited time period, until it is explicitly canceled

 
angevoyageur:

Did you check the documentation ? Also you have to be sure that an expiration mode is allowed for your symbol.

SYMBOL_EXPIRATION_GTC

1

The order is valid during the unlimited time period, until it is explicitly canceled

int OnInit()
  {
  /* Check is pending orders and expiration is allowed*/
     if(SymbolInfo.ExpirationTime())
      Print("Pending Order Expiration is allowed on this symbol");
      
   else
     {
      Print("Pending Order Expiration is NOT allowed on this symbol"); 
     }
  }

I fixed it to : ORDER_TIME_SPECIFIED

Since I am in demo I wouldn't think that it would be a problem for testing purposes.  The broker I signed up for on the Demo in question " Does not allow pending Order Expiration."

I guess I have to create some type of timer later.

I would like to test what I have built with  a broker that supports this specific feature is there a list?

When I scan I only get 1 broker.

Thank you for leading me in the right direction. In the previous post.

 
I would like to post brokers who support
The ORDER_TIME_SPECIFIED feature I would like to know if its okay to do so. The only information I would post would be  The Broker Name and ORDER_TIME_SPECIFIED is supported and not supported. When I search the site I could not find anything on the subject matter.
 
wehsnim:
I would like to post brokers who support
Why not try with Metaquotes demo account ? Probably good for this purpose.
 
wehsnim:

I fixed it to : ORDER_TIME_SPECIFIED

Since I am in demo I wouldn't think that it would be a problem for testing purposes.  The broker I signed up for on the Demo in question " Does not allow pending Order Expiration."

I guess I have to create some type of timer later.

I would like to test what I have built with  a broker that supports this specific feature is there a list?

When I scan I only get 1 broker.

Thank you for leading me in the right direction. In the previous post.

SymbolInfo.ExpirationTime() return a datetime, so this code isn't valid :

     if(SymbolInfo.ExpirationTime())

You have to check SYMBOL_EXPIRATION_MODE.

 
wehsnim:

I fixed it to : ORDER_TIME_SPECIFIED

Since I am in demo I wouldn't think that it would be a problem for testing purposes.  The broker I signed up for on the Demo in question " Does not allow pending Order Expiration."

I guess I have to create some type of timer later.

I would like to test what I have built with  a broker that supports this specific feature is there a list?

When I scan I only get 1 broker.

Thank you for leading me in the right direction. In the previous post.

Hi, you don't need a broker list or even a timer, just scan the list of pending orders and close the expired ones.

The problem of this solution is when your terminal is offline, since your solution is not server side.

Anyway, if this is not relevant to you, this solution must work with any broker.

 
angevoyageur:

SymbolInfo.ExpirationTime() return a datetime, so this code isn't valid :

You have to check SYMBOL_EXPIRATION_MODE.

I checked it out. I didn't know how to implement it until I came across 

ENUM_SYMBOL_INFO_STRING,

at the bottom of the page was some example code.

Implementation came later when I read an article : Trade operation in MQL5.

I ended up with.         Thank you again for replying back your attention to detail an knowledge in this area is amazing.

// global variable 
   
    string smbol=Symbol();

// oninit

int OnInit()
  {
  /* Check is pending orders and expiration is allowed*/
   int expiration=(int)SymbolInfoInteger(smbol,SYMBOL_EXPIRATION_MODE);
     if (expiration == true)
     Print("Pending Order Expiration is allowed on this symbol");
     else
     {
      Print("Pending Order Expiration is NOT allowed on this symbol"); 
     }
  }
 
figurelli:

Hi, you don't need a broker list or even a timer, just scan the list of pending orders and close the expired ones.

The problem of this solution is when your terminal is offline, since your solution is not server side.

Anyway, if this is not relevant to you, this solution must work with any broker.

That is an even better idea. Your also right the solution must work with any broker. So if OnInit, replies False then a way to make up for the short comings of the limitation must be introduced.
 
wehsnim:
That is an even better idea. Your also right the solution must work with any broker. So if OnInit, replies False then a way to make up for the short comings of the limitation must be introduced.
To be honest, in my case, I do this test even if SYMBOL_EXPIRATION_MODE returns true (is allowed), just in case, as a watchdog solution.
 

Good night,


I just review all topics about this error without success because the issue still appear in my code.

Value for SYMBOL_EXPIRATION_MODE is SYMBOL_EXPIRATION_GTC.

And the code is:

      mrequest.action = TRADE_ACTION_PENDING;              // Pending order execution
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = GetLotSize(Lot);                 // Number of lots to trade
   int offset = 50;                                                    // distancia con respecto al precio actual para poner la orden, en puntos
   double price;                                                       // precio de activación de la orden
   double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);                // tamaño del punto
   int digits=SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);                // número de dígitos tras la coma (precisión)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID)-offset*point;         // precio de apertura 
      mrequest.price=NormalizeDouble(price,_Digits);                  // precio de apertura normalizado 
      mrequest.sl = NormalizeDouble(price+1300*point,_Digits);
      mrequest.tp = NormalizeDouble(vPrice - ((pStopLoss-vPrice) * InpTakeProfit),_Digits);                                   // Take Profit
      mrequest.type= ORDER_TYPE_SELL_STOP;                    // Sell order
      mrequest.magic = 0;                                // Magic Number
      mrequest.type_filling = SYMBOL_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      mrequest.type_time = SYMBOL_EXPIRATION_GTC;

         

And result is:

2018.08.29 22:37:15.750 2016.01.05 06:00:00   failed sell limit 0.20 EURUSD at 1.08259 sl: 1.09559 tp: 1.06951 [Invalid expiration]

2018.08.29 22:37:15.750 2016.01.05 06:00:00   OrderSend error 4756

2018.08.29 22:37:15.750 2016.01.05 06:00:00   retcode=10022  deal=0  order=0


I'll appreciate all your support ir order to go ahead and finish this EA.