Closing a Pending Order

 

I wrote this code to close a pending order. When i run the EA the orders are not closed.

Can anybody give me any help? What have i done wrong?

Here is the code (part of my programme)

for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if (OrderType()==OP_BUYSTOP)
{
if (OrderOpenPrice() <= Ask-StopLoss*Point)
{
OrderDelete(OrderTicket());
}
return(0);
}

 
Dropje:

I wrote this code to close a pending order. When i run the EA the orders are not closed.

Can anybody give me any help? What have i done wrong?

Here is the code (part of my programme)

Well, for starters there aren't enough } to match {. Secondly, you seem to be doing one check for (OrderType() <= OP_SELL) and another check for (OrderType() == OP_BUYSTOP). These can't simultaneously be true - the first is checking to see if the order has been filled, and the second is checking to see if it's a pending buy-stop. And thirdly, you ought to decrement cnt down from total-1 to 0 rather than up from 0 to total-1. Otherwise you've got a potential bug if there's more than one pending order which needs to be deleted. (See countless other posts on this forum for the reason why.)

 

for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol()) // check for symbol
{
if (OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP) //check for buy stops/limits

{
if (OrderOpenPrice() <= Ask-StopLoss*Point)
{
OrderDelete(OrderTicket());
}

}

else if (OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP) //check for sell stops/limits

{
if (OrderOpenPrice() <= Bid-StopLoss*Point)
{
OrderDelete(OrderTicket());
}

}

}
return(0);
}

This should do it. It would also be a good idea to insert magic numbers in there so that your EA can know which order it placed.

.

Jon

 
Archael:

[...]

if (OrderOpenPrice() <= Ask-StopLoss*Point)

[...]

if (OrderOpenPrice() <= Bid-StopLoss*Point)

This should do it. [....]


It's not entirely clear to me why the current price versus the trigger forms part of Dropje's criteria for deleting a pending order, but "if (OrderOpenPrice() <= Bid-StopLoss*Point)" is unlikely to be what he/she wants on sells. Should almost definitely be +Stoploss not -Stoploss. And Dropje seems to be using stops rather than limits, and therefore it should probably by >=Bid, not <=Bid.

 
Oops, I just copy/pasted and edited quickly. Forgot to change that line hehe.
 

Yeah jjc, those lines don't make sense to me either actually. I think that Dropje doesn't know that pending orders actually become buy/sell orders once activated. Shouldn't be checking a stoploss on a pending order, should just delete it. Here:

.

1 . OrderDelete for pending orders

2. OrderClose for open orders

for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol()) // check for symbol
{
if (OrderType()==OP_BUY || OrderType()==OP_SELL) //check for open orders

{

//Check for stoploss/takeprofits here
//OrderClose(...);

}

else if (OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP) //check for buy stops/limits

{
OrderDelete(OrderTicket());

}

else if (OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP) //check for sell stops/limits

{
OrderDelete(OrderTicket());

}

}
return(0);
}

.

Jon

 
Archael wrote >>

Yeah jjc, those lines don't make sense to me either actually. I think that Dropje doesn't know that pending orders actually become buy/sell orders once activated. Shouldn't be checking a stoploss on a pending order, should just delete it. Here:

.

1 . OrderDelete for pending orders

2. OrderClose for open orders

for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol()) // check for symbol
{
if (OrderType()==OP_BUY || OrderType()==OP_SELL) //check for open orders

{

//Check for stoploss/takeprofits here
//OrderClose(...);

}

else if (OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP) //check for buy stops/limits

{
OrderDelete(OrderTicket());

}

else if (OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP) //check for sell stops/limits

{
OrderDelete(OrderTicket());

}

}
return(0);
}

.

Jon

Hello mates,

would be possible to post the full source code of this here, please?

Thank You In Advance,

Mihail Mihaylov

 
Thomark:

Hello mates,

would be possible to post the full source code of this here, please?

Thank You In Advance,

Mihail Mihaylov

Why?


CB