Deleting a pending order

 

Ok.. i give up, I don't know what else I can do.. I have to SO many ways to do this, and i can't... so,, hopefully you guys can help me out on this one..

The thing is, I have and EA that opens 2 pending orders ( OP_BUYSTOP and OP_SELLSTOP) and all I want is to delete 1 of the pending order once the other one is filled...

So.. If the EA buy the BUY_STOP, delete the SELL_STOP, if it buy the SELL_STOP, delete the BUY_STOP.

What's wrong with my code?

Thanks!!!


int delinverse()

{


string comentario,par;
int i;
for(i=0;i< OrdersTotal() ;i++)
{
if(OrderSelect(i, SELECT_BY_POS)==true)

//OP_BUY 0 Buying position.
//OP_SELL 1 Selling position.
//OP_BUYLIMIT 2 Buy limit pending position.
//OP_SELLLIMIT 3 Sell limit pending position.
//OP_BUYSTOP 4 Buy stop pending position.
//OP_SELLSTOP 5 Sell stop pending position.

if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{

for(i=0;i< OrdersTotal() ;i++)
{
if(OrderSelect(i, SELECT_BY_POS)==true)
{
comentario=OrderComment();
par=StringSubstr(comentario,0,6);
Print("Coment1:",comentario," Pair:",par," Ordertype:",OrderType());
//if(OrderType()>3 && par==Symbol())
if(OrderType()==OP_BUYSTOP)// && par==Symbol())
{
OrderDelete(OrderTicket());
Print("Deleting BUY_STOP"," Ordertype:",OrderType());
return(1);
}
if(OrderType()==OP_SELLSTOP)// && par==Symbol())
{
OrderDelete(OrderTicket());
Print("Deleting SELL_STOP"," Ordertype:",OrderType());
return(1);
}

}
}
return(1);
}
return(0);
}
}
 

For a start (and God only knows how many times this has had to be repeated on this forum) change your loop from an incrementing loop to a decrementing loop.

In other words, make it start at "total number of orders minus one" and stop at "zero".

The reason is that every time you delete an order, the pool is re-indexed, so you will miss some orders in your loop.


CB

 
cloudbreaker:

For a start (and God only knows how many times this has had to be repeated on this forum) change your loop from an incrementing loop to a decrementing loop.

In other words, make it start at "total number of orders minus one" and stop at "zero".

The reason is that every time you delete an order, the pool is re-indexed, so you will miss some orders in your loop.


CB

Ok, thanks for the help, I did what you told me to do and the problem was not solved...

Any other tip?

thanks

 

OK. A question (which will determine how simple we can make this): will you only ever have 1 of either buystop and sellstop per ea/chart/symbol - ie. max 2 orders of which neither, one or both could be filled at any point in time?


A really simple way to do it would be as follows:

- A loop which merely counts the number of orders of each type in the order pool.

- Then a switch block which decides upon which type of order to close (if any).

- Finally a loop which closes the order which matches the type you've decided to close.


CB

 
cloudbreaker:

OK. A question (which will determine how simple we can make this): will you only ever have 1 of either buystop and sellstop per ea/chart/symbol - ie. max 2 orders of which neither, one or both could be filled at any point in time?


A really simple way to do it would be as follows:

- A loop which merely counts the number of orders of each type in the order pool.

- Then a switch block which decides upon which type of order to close (if any).

- Finally a loop which closes the order which matches the type you've decided to close.


CB

The answer for your question is YES, the EA open 2 pendings at 00:00, if one of them is filled, then, immediately the opposite pending order is deleted!

Look at my code..

1) I search of all orders, if 1 of them is OP_BUY or OP_SELL, then it means that it found a filled order, then, the next "for" is to find the pending order and delete the order.. it should be simple, but i don't know what is going on, i have some outputs from the "ordertype()" and it does not make sense... i wonder what i am doing wrong.

 

Index counts starting at zero

First IF has no brackets.

Two nested loops using the same index "i" (once the second loop starts, you can never resume the outer one.)

 
airwolfnh:

The answer for your question is YES, the EA open 2 pendings at 00:00, if one of them is filled, then, immediately the opposite pending order is deleted!

Look at my code..

1) I search of all orders, if 1 of them is OP_BUY or OP_SELL, then it means that it found a filled order, then, the next "for" is to find the pending order and delete the order.. it should be simple, but i don't know what is going on, i have some outputs from the "ordertype()" and it does not make sense... i wonder what i am doing wrong.

Ok, can I suggest that you re-code it the way I suggested?


- A loop which merely counts the number of orders of each type in the order pool.

- Then a switch block which decides upon which type of order to close (if any).

- Finally a loop which closes the order which matches the type you've decided to close.


CB

 
cloudbreaker:

Ok, can I suggest that you re-code it the way I suggested?


- A loop which merely counts the number of orders of each type in the order pool.

- Then a switch block which decides upon which type of order to close (if any).

- Finally a loop which closes the order which matches the type you've decided to close.


CB

Ok, WORKING FINALY, thanks everyone.

The code is this :


int new_del()
{
int i,a;
int total = OrdersTotal();
string comentario,par;
for (i=total-1; i >=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderType()==OP_BUY || OrderType()==OP_SELL)
{
for (a=total-1; a >=0; a--)
{
OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
comentario=OrderComment();
par=StringSubstr(comentario,0,6);
if(OrderType()==OP_SELLSTOP)// && comentario==Symbol())
{
OrderDelete(OrderTicket());
Print("Deleting SELL_STOP"," Ordertype:",OrderType());
return(1);
}
if(OrderType()==OP_BUYSTOP)// && par==Symbol())
{
OrderDelete(OrderTicket());
Print("Deleting BUY_STOP"," Ordertype:",OrderType());
return(1);
}
}
}
}

}

 
airwolfnh:

Ok, WORKING FINALY, thanks everyone.

The code is this :


int new_del()
{
int i,a;
int total = OrdersTotal();
string comentario,par;
for (i=total-1; i >=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderType()==OP_BUY || OrderType()==OP_SELL)
{
for (a=total-1; a >=0; a--)
{
OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
comentario=OrderComment();
par=StringSubstr(comentario,0,6);
if(OrderType()==OP_SELLSTOP)// && comentario==Symbol())
{
OrderDelete(OrderTicket());
Print("Deleting SELL_STOP"," Ordertype:",OrderType());
return(1);
}
if(OrderType()==OP_BUYSTOP)// && par==Symbol())
{
OrderDelete(OrderTicket());
Print("Deleting BUY_STOP"," Ordertype:",OrderType());
return(1);
}
}
}
}

}

I have copied this code into my own EA  (which tactic is same as described in the first post), but he doesn't delete pending order after another is opened.

Please, look into attached EA and give some suggestions or solutions (don't laugh, it's my first EA :) ). Thanks.

 
int start(){
   new_del();
   if (last_bar == Bars) return(0);
   last_bar = Bars;
   if (OrdersTotal() == 0){
         OrderSend(Symbol(), OP_BUYSTOP, lots, Ask + INCREMENT * Point, 3, Ask + INCREMENT * Point - stop_loss * Point, Bid + INCREMENT * Point + take_profit * Point, IDENT, MAGIC, 0, Blue);
         OrderSend(Symbol(), OP_SELLSTOP, lots, Bid - INCREMENT * Point, 3, Bid - INCREMENT * Point + stop_loss * Point, Ask - INCREMENT * Point - take_profit * Point, IDENT, MAGIC, 0, Red);
   } 
   return(0);
}
In your code, you don't call the function to delete
 
Yes, now it works correct (I spent on this two weeks and already lost hope :) ). Thank you for quick and right answer, Keith Watford .
Reason: