HELPPPPP!!!!!! trying to close pending orders

 

can someone help me please? I can get this function to delete a pending order. What am i doing wrong? the way I would like it to work is when a open order is closed. this function will then delete the pending order. I keep getting the error 4108 for invalid ticket. I really have issue if another order is opened. then the function tries to delete the open order. Need help.

Thank you

void CloseBuyStack1stLevelLimitOrder1M()
{
for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_TICKET,MODE_TRADES))
     if(OrderMagicNumber() == Stack1stLevelMagicNumber1M)
        if(OrderSymbol() == Symbol())
             if(OrderType()==OP_BUYLIMIT)
                if(CheckOrderOpenBuy1M () == false)
                     OrderDelete(OrderTicket(),clrNONE);
                    
   }


} 
 

Hey! Don't double post!! Please delete them all except one - its enough!

And use the SRC button for any kind of code!

 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Don't double post Don't triple post Don't quad post If you continue you will be ignored!

  3.  if(CheckOrderOpenBuy1M () == false)
    Does that do an OrderSelect, thus invalidating your loop?
  4. OrderDelete(OrderTicket(),clrNONE);
    Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  5. See also deleting all pending if found one working order - MQL4 forum



 

You have wasted my time as I have had to delete your other 10 posts.

You have wasted other people's time by bringing 10 old posts to the top of the list.

If you do it again, you will be banned for flooding.

   if(OrderSelect(b,SELECT_BY_TICKET,MODE_TRADES))

In a loop like this you do not select by ticket, select by position

 
First, I apologize for the multiple post, and thank you for the help. I did make some adjustments to the coding based on everyone's suggestions, but I am still having some more issues. the function is deleting the pending orders when a new position is opened. I only want the pending orders to delete when the open position that the pending order is attached to closes. here is the 2 functions that deal with these operations. can anyone tell me what I am doing wrong. thank you
bool CheckOrderOpenBuy1M ()
{

for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber() == MagicNumber1M)
        if(OrderSymbol() == Symbol())
          if(OrderType()==OP_BUY)
              return(true);
              else return(false);
   }
return(0);

}

void CloseBuyStack1stLevelLimitOrder1M()
{
if(CheckOrderOpenBuy1M () == false)
  for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
     if(OrderMagicNumber() == Stack1stLevelMagicNumber1M)
        if(OrderSymbol() == Symbol())
             if(OrderType()==OP_BUYLIMIT)
                     OrderDelete(OrderTicket(),clrNONE);
                     
   }


}
 
          if(OrderType()==OP_BUY)
              return(true);
              else return(false);
What happens if you have a buy order but it is not in the highest position?
 
jtubbs13791:
First, I apologize for the multiple post, and thank you for the help. I did make some adjustments to the coding based on everyone's suggestions, but I am still having some more issues. the function is deleting the pending orders when a new position is opened. I only want the pending orders to delete when the open position that the pending order is attached to closes. here is the 2 functions that deal with these operations. can anyone tell me what I am doing wrong. thank you

I have no idea what the highlighted text means

bool CheckOrderOpenBuy1M ()
{

for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber() == MagicNumber1M)
        if(OrderSymbol() == Symbol())
          if(OrderType()==OP_BUY)
              return(true);
              else return(false);
   }
return(0);

}

This only checks 1 order, remove the else

bool CheckOrderOpenBuy1M ()
{

for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber() == MagicNumber1M)
        if(OrderSymbol() == Symbol())
          if(OrderType()==OP_BUY)
              return(true);
   }
return(false);

}
 
WHRoeder:
What happens if you have a buy order but it is not in the highest position?
If there were not been pending orders(opened) than it is ok.
 
Pending orders that are open are buy orders. But what happens in the code posted when there is a buy order and it is not in the highest position?  Then it is NOT ok. You don't listen, you don't think, no one can teach you.
 
:))))))))))))))))))))))))))))))
 
WHRoeder:
What happens if you have a buy order but it is not in the highest position?
eevviill:
If there were not been pending orders(opened) than it is ok.

How can it be ok?

The loop only checks the first order, so there may be a buy order, but if it is not in the highest position, it will not be detected.