algorithmic trading - MQL5: how do I automatically delete all un-triggered pending orders before placing new orders? -


i working on project requires me place buystop , sellstop pair of orders , on next bar if orders not triggered, delete them , place fresh ones.

here code:

if(logic == true && orderstotal() == 0)   {bool res = ordersend(....);} if(orderstotal() != 0)   {   if(ordertype == op_buy || ordertype == op_sell)      {       bool del = orderdelete(....);      }   } 

this code placing orders , deleting them when testing.

but when ea active on live server, not open orders because platform has orders of other instruments open.

i'm sure there quite easy way around since novice, i'm not able figure out.

old proverb says:
measure twice before cut once
there 4 values ( 3 pendings ) check before orderdelete()

as definition states handle { op_{buy|sell}stop } orders, there following 3 items check:

  • symbol() match ( not causing unwanted side-effects deleting other ea's or manual orders )
  • ordertype() match ( not ignoring actual state { pending | at_market } , direction { buy | sell } of order )
  • ordermagicnumber() match ( not ignoring uuid selector utility available set each individual ordersend() )

so, let's sketch detection process:

int myeacontextawaremagicnumber = ...;  ( int ii  = orderstotal();           ii >= 0;           ii--       )       if orderselect( ii, select_by_pos, mode_trades )       {           if (      ordersymbol()       != _symbol              &&     ordermagicnumber()  != myeacontextawaremagicnumber              &&     orderopentime()     >= time[1]                    // prev. bar               && !(  ordertype()         == op_buystop                  || ordertype()         == op_sellstop                  )              ) continue;                // __^ __^ __^ __^ __^ __^ loop next test            // -------------------------------------------+           // process matching orderdelete() |           // -------------------------------------------+              ...              ..              .           // -------------------------------------------+       }       else print( "warn: orderselect() failed @ db.pool.select(), record_number == ", ii ); 

so how delete un-triggered pendings is done.


next comes remark

"... when ea active on live server, not open orders because platform has orders of other instruments open."

there hardly advice provided without delivering exact { getlasterror() | _lasterror } values.

some brokers account types indeed restrict ordersend() acceptance policies, , besides getlasterror() value respective broker terms , conditions apply.

do not hesitate ask more & may enjoy other questions/answers in mql4 domain.


Popular posts from this blog