Download MetaTrader 5

Pending Order should cancel but does not,

To add comments, please log in or register
Diane Minshew
351
Diane Minshew  
//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;
           }   
     }
  }
Alain Verleyen
Moderator
31183
Alain Verleyen  
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

Diane Minshew
351
Diane Minshew  
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.

Diane Minshew
351
Diane Minshew  
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.
Alain Verleyen
Moderator
31183
Alain Verleyen  
wehsnim:
I would like to post brokers who support
Why not try with Metaquotes demo account ? Probably good for this purpose.
Alain Verleyen
Moderator
31183
Alain Verleyen  
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.

Rogerio Figurelli
Moderator
40336
Rogerio Figurelli  
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.

Diane Minshew
351
Diane Minshew  
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"); 
     }
  }
Diane Minshew
351
Diane Minshew  
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.
Rogerio Figurelli
Moderator
40336
Rogerio Figurelli  
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.
To add comments, please log in or register