Deleting pending orders

User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Deleting pending orders

Post by traderuk »

Hi,

Can you help please I'm getting some pending orders not deleting (usually when a trade is initiated on opposite side)?

On each bar I'm trying to close all pending orders...

Code: Select all

if(Volume[0]==1) newbar=true;
   // close previous pending orders
   ClosePending(NULL);
Here's close pending..

Code: Select all

void ClosePending(int MagicNumber)
{
if(OrderType()== OP_BUYSTOP || OrderType()==OP_SELLSTOP ){OrderDelete(OrderTicket());}
}
I'm not sure how to remove( int MagicNumber) from ClosePending, as I know it's not being called at the moment?

Also not sure how to call the function when it is removed, from what I understand it would simply be ClosePending(); ?

I did try counting the pending orders to determine the magic number...

Code: Select all

int CountPending(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
   continue;
   
  if(OrderType()==OP_BUYSTOP || OrderType()==OP_BUYSTOP)
  count++;
 }//for
 return(count);
}
And then counting through the orders incrementing each time but that didn't work either...

Code: Select all

 int i = OrdersTotal();
 while( CountShorts(MagicNumber) != 0 && i >= 0 )
 { 
....
}
Thanks

Stefan
User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

traderuk wrote:
I did try counting the pending orders to determine the magic number...

Code: Select all

int CountPending(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
   continue;
   
  if(OrderType()==OP_BUYSTOP || OrderType()==OP_BUYSTOP)
  count++;
 }//for
 return(count);
}
Think I just realized what I did... checking only for OP_BUYSTOP. Doh! :lol:

Although I'd have thought the orders should still have closed while not referencing the count and just doing ClosePending direct with the OrderDelete.

If anyone can help with the other questions or if there's better ways to do what i'm trying to do in any of the code I'd be very grateful.

Would rather do this properly to some uber standard then just mish mash :)
User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

Simplified in to one block of code. Found that I need to count down when deleting pendings but some trades are still being missed. I tried splitting the order types as figured it might be returning 1 before the next ticket was found. I'm stuck. :(

Code: Select all

void ClosePendings(int MagicNumber)
{
int count=0;
int trade;
int trades=OrdersTotal();
   for(trade=trades-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_BUYSTOP || OP_SELLSTOP){
         OrderDelete(OrderTicket());
      }
      return(1);
   }
}
phil_trade

Re: Deleting pending orders

Post by phil_trade »

traderuk wrote:Simplified in to one block of code. Found that I need to count down when deleting pendings but some trades are still being missed. I tried splitting the order types as figured it might be returning 1 before the next ticket was found. I'm stuck. :(

Code: Select all

void ClosePendings(int MagicNumber)
{
int count=0;
int trade;
int trades=OrdersTotal();
   for(trade=trades-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_BUYSTOP || OP_SELLSTOP){
         OrderDelete(OrderTicket());
      }
      return(1);
   }
}
Hi

Why do you pass MagicNumber as parameter without using it ?
Why do you call a Return(1) Inside the For loop ?
Why don't you also select OP_SELLLIMIT and BUY_LIMIT ?

Philippe
User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

phil_trade wrote: Hi

Why do you pass MagicNumber as parameter without using it ?
Why do you call a Return(1) Inside the For loop ?
Why don't you also select OP_SELLLIMIT and BUY_LIMIT ?

Philippe
Hi Philippe

- Have fixed the MagicNumber... I didn't know how to create the function without passing, realized now that it's just void ClosePendings().

- I thought the return(1) would set the value of trade to 1, making the loop run through. I'm not really sure if that's what return() does or if I'm doing it the correct way at all?

- Am only working with buy/sell stops at the moment.

Thanks for your time and help.

Stefan
phil_trade

Re: Deleting pending orders

Post by phil_trade »

traderuk wrote:
phil_trade wrote: Hi

Why do you pass MagicNumber as parameter without using it ?
Why do you call a Return(1) Inside the For loop ?
Why don't you also select OP_SELLLIMIT and BUY_LIMIT ?

Philippe
Hi Philippe

- Have fixed the MagicNumber... I didn't know how to create the function without passing, realized now that it's just void ClosePendings().

- I thought the return(1) would set the value of trade to 1, making the loop run through. I'm not really sure if that's what return() does or if I'm doing it the correct way at all?

- Am only working with buy/sell stops at the moment.

Thanks for your time and help.

Stefan
A return operator terminates the current function execution and returns the control to the calling program

so your return stop the For loop ! you don't need it !
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Deleting pending orders

Post by dietcoke »

traderuk wrote:Hi,

Can you help please I'm getting some pending orders not deleting (usually when a trade is initiated on opposite side)?

On each bar I'm trying to close all pending orders...

Stefan

Are you wanting to close all your pendings at the start of each bar?
Image
User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

dietcoke wrote:
traderuk wrote:Hi,

Can you help please I'm getting some pending orders not deleting (usually when a trade is initiated on opposite side)?

On each bar I'm trying to close all pending orders...

Stefan

Are you wanting to close all your pendings at the start of each bar?
Yes please.

I'm learning to code but I can't find any useful enough documentation on how to do this.
User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

I think the issue is with ticket numbers and how they're incremented as a trade triggers from buy/sell stop to buy/sell.

Attached is the code i'm working on. There's not much of it and it's tidy. If you could have a quick look and tell me if I'm doing anything a non-standard way that would be very much appreciated.

I feel like I'm starting to bodge it together and would rather know if it's wrong before it ends up a complete bodge :lol:

- In particular the stoploss and trailingstop of -1 to activate the auto stop movement
- ticket counting to be able to properly closing pending orders
- current buy/sell trades if they match criteria e.g if(Close[1]<Open[1]) CloseLongs(MagicNumber);not closing properly, probably ticket counting again.

Thanks lots!
You do not have the required permissions to view the files attached to this post.
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Deleting pending orders

Post by dietcoke »

traderuk wrote:
dietcoke wrote:
Are you wanting to close all your pendings at the start of each bar?
Yes please.

I'm learning to code but I can't find any useful enough documentation on how to do this.
If you know the time at which you want to close a pending order, ie at the end of a particular bar or at a certain time of day,

the best way to do this is to set the expiry time of the order when you open it.

So, lets say it's 12:30 and you want to open a pending to be cancelled at the end of the current H1 bar(ie. 13:00), you can use the code below to calculate the expiration time of your order.

You then include the expiration ou have calculated in your ordersend call.

Code: Select all

datetime expiration =iTime(symbol,PERIOD_H1,0)  + 3600;// Start time of the currenbt H1 bar + 1 hours 
Of course, you don't have to use H1, you can substitute any Time period you like, or,

you can just set your orders to expire at a particular time of day.

Code: Select all

                  //get the start of the day          //add 20 hours converted to seconds
datetime expiration = iTime(Symbol(),PERIOD_D1,0) + (20 * 60 * 60);
which will allow you to set an expiry time of 20:00
Image
Post Reply

Return to “Coders Hangout”