reliable delete pending code

Post Reply
Spectre
Trader
Posts: 39
Joined: Mon Nov 05, 2012 6:18 am
Location: Norway, the most taxed place on earth(maybe)

reliable delete pending code

Post by Spectre »

hello

i am growing increasingly frustrated with myself. i cannot get my code to work right.

i want the code to close all pending orders at bar close, but whatever i try it sometimes leaves trades behind. it goes on offline/renko charts, so using expiration on the pending is unfortunately not going to work. i have tried
Time[0] > Time[1], Close[] value coding,

Code: Select all

 static int OldBar = Time[0]
if (Time[0] != OldBar) 
anyone have a good solution that will work everytime?? (preferably copy paste code , i am sick and tired of pending orders :roll: )

i bet if someone here gives me the answer or something closer to a solution, i will slap my head and cry myself to sleep in shame over its simplicity

Thomas
" I'm not an alcoholic, i'm australian " - Lance
magft
Trader
Posts: 195
Joined: Tue Nov 15, 2011 9:59 pm
Location: East Midlands, UK

Re: reliable delete pending code

Post by magft »

Spectre wrote:hello

i am growing increasingly frustrated with myself. i cannot get my code to work right.

i want the code to close all pending orders at bar close, but whatever i try it sometimes leaves trades behind. it goes on offline/renko charts, so using expiration on the pending is unfortunately not going to work. i have tried
Time[0] > Time[1], Close[] value coding,

Code: Select all

 static int OldBar = Time[0]
if (Time[0] != OldBar) 
anyone have a good solution that will work everytime?? (preferably copy paste code , i am sick and tired of pending orders :roll: )

i bet if someone here gives me the answer or something closer to a solution, i will slap my head and cry myself to sleep in shame over its simplicity

Thomas
Why not try

Code: Select all

static int OldBars=Bars;
if(OldBars!=Bars)
{
    OldBars=Bars
    //delete pendings etc
}
This is time independent and works on offline charts as well. You could replace

Code: Select all

Bars
with

Code: Select all

iBarShift(Symbol(),tf)
to check new bar on a different tf.

Regards

Mike
User avatar
slipshod
Trader
Posts: 404
Joined: Tue Dec 27, 2011 9:14 am
Location: Australia

Re: reliable delete pending code

Post by slipshod »

Not sure if Spectre's found a solution to this problem yet, but its one I ran into as well. A couple of things to check:-

Firstly are you looping from 0 to OrdersTotal() in a for loop? If so, OrdersTotal() will vary with each trade deleted & you're highly likely to get some left behind. This happens even if you do: int total=OrdersTotal() and then for (i=0; i<total; i++). The solution is for (i=total-1; i>=0; i--)

Secondly its possible that even that doesn't get the job done. You might want to try something like this:-

Code: Select all

int total = OrdersTotal(), i;
static int orders[];
int deltotal = 0;
ArrayResize(orders, total);

for (i=total-1; i>=0; i--)
{
   OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
   if statement checking to ensure its your order, magic # ordertype etc etc
   {
      orders[deltotal] = OrderTicket();
      deltotal++;
   }
}

for (i=0; i<deltotal; i++)
   OrderDelete(orders[i]);
Btw this might prove useful for deleting live orders as well. Never hurts to be paranoid :)
Post Reply

Return to “Coders Hangout”