What's wrong with OrderClose()

 

Hello, I don't know why but I have a bug with OrderClose() in the next function.

I know the condition are meet because sound alert work, but order still open.

Need help!


Thank's

pgforex


//==== Function StopLoss
void MaxStopLoss()
{ 
  for(int i=OrdersTotal()-1;i>=0;i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
    {
      if(OrderType() == OP_SELL && OrderSymbol() == Symbol())
	   {
	   //---- Max Stop Loss OP_SELL
        if(Bid >= OrderOpenPrice()+(StopLoss*Point))PlaySound("alert.wav");
        {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
		  }
		}
		else if(OrderType() == OP_BUY && OrderSymbol() == Symbol())
		 {
		 //---- Max Stop Loss OP_BUY
		   if(Ask <= OrderOpenPrice()-(StopLoss * Point))
         {
          OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
         }
   	 }
     }
  }
}
//---- End Function
 
 if(Bid >= OrderOpenPrice()+(StopLoss*Point))PlaySound("alert.wav");
        {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
		  }
		}
		else
Your IF is being executed (sound plays) but the orderClose isn't inside the if. Change to
 if(Bid >= OrderOpenPrice()+(StopLoss*Point)) {
         PlaySound("alert.wav");
         if (!OrderClose(OrderTicket(),         OrderLots(),    
						Bid,
                        3, Red)) {
            Alert(  "EA OrderClose(ticket=", ticket, ", ...) failed: ", GetLastError() );
		  }
		}
		else...
 

Ok I tried your code sunday. I think the bug is not "sound alert", because I dont use in my original code. I put sound alert only to know if the condition are meet.


I don't know why, all functions are written with the same structure, but I have a problem only with OrderClose.


What is the difference between this next 2 codes?


Function with OrderClose: don't work

//==== Function StopLoss
void MaxStopLoss()
{ 
  for(int i=OrdersTotal()-1;i>=0;i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
    {
      if(OrderType() == OP_SELL && OrderSymbol() == Symbol())
	   {
	   //---- Max Stop Loss OP_SELL
        if(Bid >= OrderOpenPrice()+(StopLoss*Point))
        {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
		  }
		}
		else if(OrderType() == OP_BUY && OrderSymbol() == Symbol())
		 {
		 //---- Max Stop Loss OP_BUY
		   if(Ask <= OrderOpenPrice()-(StopLoss * Point))
         {
          OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
         }
   	 }
     }
  }
}
//---- End Function


Function with OrderModify: this one work

//==== Function Move to Breakeven
void MoveToBE()
{
  for(int i=OrdersTotal()-1;i>=0;i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
    {
      if(OrderType() == OP_SELL && OrderSymbol() == Symbol())
	   {
      //---- Modify when hit First Target OP_SELL
        if((Bid <= OrderOpenPrice()-(FirstTarget*Point)) && (OrderStopLoss() != OrderOpenPrice() - (FirstStop*Point)))
        {
         OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - (FirstStop*Point),0, 0, Blue);
        }
		}
		else if(OrderType() == OP_BUY && OrderSymbol() == Symbol())
		{
      //---- Modify when hit First Target      
        if((Ask >= OrderOpenPrice()+(FirstTarget*Point)) && (OrderStopLoss() != OrderOpenPrice()+(FirstStop*Point)))
        {
         OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()+(FirstStop*Point),0,0, Blue);   
        }
		}
    }
  }
}
//---- End Function

Thank's

 
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
Change to:
if (!OrderClose(OrderTicket(), OrderLots(), Bid, 3, red)) Alert(
   "OrderClose(ticket=", OrderTicket(), ", ...) failed: ", GetLastError() );

So you find out why. Do you have any sleeps or modifies before the close, then you must refresh or Bid won't be valid.

Also 3 won't be valid on a 5 digit broker

 

Ok, if I understand, this code refresh the bid value.


if(!)


Must I use it for "OrderModify"?


Thank's

 

Thank's so much WHRoeder, with your code I've got error 129(Invalid Price).


I've chage this:

if (!OrderClose(OrderTicket(), OrderLots(), Bid, 3, red)) Alert(
   "OrderClose(ticket=", OrderTicket(), ", ...) failed: ", GetLastError() );

To:

if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, red)) Alert(
   "OrderClose(ticket=", OrderTicket(), ", ...) failed: ", GetLastError() );

Bid and Ask don't work with my broker. I don't know if this code work with 5 digits broker, but now it's work for me.


Thank's again!

pgforex

 
pgforex:

if(!)


Must I use it for "OrderModify"?


if(!orderClose(...)) Alert(...)

This issues an alert when orderClose/orderModify fails (returns false)

Reason: