How to delete pending order, related current open position ?

 

I'm sorry for my bad english * lets skip :P

I using Martiangle system

this code delete all pending order at > 01.00AM

How to make some code so just close pending order at 01.00AM - 17.00PM with condition :

1. Close all BuyLimit if there is no Open Position for buy [Delete all OP_BUYLIMIT when there is no OP_BUY], vice versa [don't Delete all OP_BUYLIMIT when there is OP_BUY]

2. Close all SellLimit if there is no Open Position for sell [Delete all OP_SELLLIMIT when there is no OP_SELL], vice versa [don't Delete all OP_SELLLIMIT when there is OP_SELL]


Thanks for help :)

 if (Hour()>=01 && Hour()<=17) //default 00 dan 17
{   

 for(int i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType() < OP_BUY && OrderType() == OP_BUYLIMIT)
         {
            int ticket = OrderTicket();
            while(IsTradeAllowed() == false) Sleep(10000);
            OrderDelete(ticket,CLR_NONE);
         }
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType() > OP_SELL && OrderType() == OP_SELLLIMIT)
         {
            ticket = OrderTicket();
            while(IsTradeAllowed() == false) Sleep(10000);
            OrderDelete(ticket,CLR_NONE); 
         }
      }

return(0);
 } 
 
bitung:

I'm sorry for my bad english * lets skip :P

I using Martiangle system

this code delete all pending order at > 01.00AM

How to make some code so just close pending order at 01.00AM - 17.00PM with condition :

1. Close all BuyLimit if there is no Open Position for buy [Delete all OP_BUYLIMIT when there is no OP_BUY], vice versa [don't Delete all OP_BUYLIMIT when there is OP_BUY]

2. Close all SellLimit if there is no Open Position for sell [Delete all OP_SELLLIMIT when there is no OP_SELL], vice versa [don't Delete all OP_SELLLIMIT when there is OP_SELL]


Thanks for help :)


Check in your loop the ordertype() and count every type separate

if the loop is done then you know total every type

loop again if needed and you can close pending trades...

 
bitung:

I'm sorry for my bad english * lets skip :P

I using Martiangle system

this code delete all pending order at > 01.00AM

How to make some code so just close pending order at 01.00AM - 17.00PM with condition :

1. Close all BuyLimit if there is no Open Position for buy [Delete all OP_BUYLIMIT when there is no OP_BUY], vice versa [don't Delete all OP_BUYLIMIT when there is OP_BUY]

2. Close all SellLimit if there is no Open Position for sell [Delete all OP_SELLLIMIT when there is no OP_SELL], vice versa [don't Delete all OP_SELLLIMIT when there is OP_SELL]


Thanks for help :)

1.A little advice, there's plenty story about traders losing money using Martingale. I don't think I have to tell you more ...

2. The whole code within the for loop is wrongly coded. Here's how to use order select correctly https://book.mql4.com/trading/orderclose scroll down a little over there

 

deVries & onewithzachy


thanks for reply :)

I will check and read again :D


:beer:

 
bitung:

You have ...

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType() < OP_BUY && OrderType() == OP_BUYLIMIT)

You are testing OrderType twice. This isn't "wrong" and it will probably work, but it is unnecessary. I don't happen to remember the numerical value of OP_BUY but one should not rely on it being a particular value. That is bad practice (and unfortunately all too common on this site).

This version is simpler and safer ...

 if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType() == OP_BUYLIMIT)

EDIT: Your code is not doing what you think / want it to do. See posts below.

 

Let's look at this part of the code ...

 for(int i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType() < OP_BUY && OrderType() == OP_BUYLIMIT)
         {
            int ticket = OrderTicket();
            while(IsTradeAllowed() == false) Sleep(10000);
            OrderDelete(ticket,CLR_NONE);
         }
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType() > OP_SELL && OrderType() == OP_SELLLIMIT)
         {
            ticket = OrderTicket();
            while(IsTradeAllowed() == false) Sleep(10000);
            OrderDelete(ticket,CLR_NONE); 
         }
      }

There are two OrderSelects, two tests for OrderSymbol, two tests for OrderMagicNumber. Programmers hate wasted lines of code as it is inefficient. So it could be re-written as

   for( int i=OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) ){
         if( OrderSymbol()==Symbol() ){
            if( OrderMagicNumber()==magic ){
               if( OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT ){
                 int ticket = OrderTicket();
                  while(IsTradeAllowed() == false) Sleep(10000);
                  OrderDelete(ticket,CLR_NONE);
               }
            }
         }
      }
   }

I don't like having lots of tests on one line as it makes debugging more difficult. On the other hand I don't like indenting code as many levels as I have above. So i might write it like this ...

   for( int i=OrdersTotal()-1; i>=0; i-- ){
      if( !OrderSelect(i,SELECT_BY_POS,MODE_TRADES) ){
         Print("Order Select FAILED!");
         continue;
      }
      
      if( OrderSymbol()!=Symbol() )
         continue;
         
      if( OrderMagicNumber()!=magic )
         continue;
         
      if( OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT ){
         while( !IsTradeAllowed() )
            Sleep(1000);
         OrderDelete(OrderTicket(),CLR_NONE);
      }
   }

Although I should have checked the OrderDelete to make sure it worked.

 

dabbler, bitung wants only to delete buy limit if there is no open buy, and delete sell limit if there is no open sell :)

for(int i=OrdersTotal()-1;i>=0;i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) &&
       OrderSymbol() == Symbol() && 
       OrderMagicNumber() == magic )
       {
       // code here
       
       }          
    }

I think we should execute the 'for loop' twice. First to check the existence of OP_BUY or OP_SELL. second if no existence delete the corresponding pending

 
onewithzachy:

dabbler, bitung wants only to delete buy limit if there is no open buy, and delete sell limit if there is no open sell :)

I think we should execute the 'for loop' twice. First to check the existence of OP_BUY or OP_SELL. second if no existence delete the correspondence pending

Thank you. I carelessly optimized code which was not even close to doing what was needed :-(

Agreed that two for loops are needed with flags to note which positions are held.

Reason: