Download MetaTrader 5

Close pending orders when new candle started

To add comments, please log in or register
Having questions? Ask them on the Forum - traders will help you!
Garble_buble
16
Garble_buble 2016.10.24 08:12 

Hi All,

i created a simple script, which cover closing unrealized pending orders when new candle started, but it doesn´t work. Can you please help me with this? I need to solve this problem.

Thanks for any help :-)

int Objem1 = Volume[0];

//+------------------------------------------------------------------+
//| Close pending orders                             |
//+------------------------------------------------------------------+
 for(p=0; p <= OrdersTotal(); p++)     
if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES)==true && Objem1<1 && OrderMagicNumber()==Magic_number)
{if(OrderType()==OP_BUYSTOP)
           {

            Delete = OrderDelete(OrderTicket(),0);}} 

Keith Watford
Moderator
9601
Keith Watford 2016.10.24 08:54  

Objem1<1 will never be true.

Don't use volume to detect a new bar, use time.

     static datetime bar_time=Time[0];
     datetime now_time=Time[0];
     if(now_time!=bar_time)
        {
        bar_time=now_time;
        //A new bar has opened
        //Place code here to be executed only when a new bar opens
        //
        }

.

Garble_buble
16
Garble_buble 2016.10.24 09:11  

Hi,

thank you for your help. I am using "volume", because "time" work only when is price moving at specific time and that is poblem, because price can be static, when i need to close pending orders.

Keith Watford
Moderator
9601
Keith Watford 2016.10.24 09:19  
Garble_buble:

Hi,

thank you for your help. I am using "volume", because "time" work only when is price moving at specific time and that is poblem, because price can be static, when i need to close pending orders.

Time[0] changes when a new bar opens, Volume[0] can never be <1
whroeder1
14470
whroeder1 2016.10.24 11:59  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
  3. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
Garble_buble
16
Garble_buble 2016.10.31 08:07  
GumRai:
Time[0] changes when a new bar opens, Volume[0] can never be <1
But this function is working correctly, when i want open order. The difference is, that i want to delete pending orders.
peter.MT4Web
158
peter.MT4Web 2016.10.31 08:56  

If it works your way, then do it your way and you don't need any help.

Keith Watford
Moderator
9601
Keith Watford 2016.10.31 10:38  

Garble_buble:

GumRai:
Time[0] changes when a new bar opens, Volume[0] can never be <1


But this function is working correctly, when i want open order. The difference is, that i want to delete pending orders.
I don't believe you. I can see no way that Volume[0]<1 can be true.
honest_knave
Moderator
2159
honest_knave 2016.10.31 12:06  
Garble_buble:

Hi All,

i created a simple script, which cover closing unrealized pending orders when new candle started, but it doesn´t work. Can you please help me with this? I need to solve this problem.

You're doing this in a script?

GumRai is correct - a new bar does not form unless a tick has come in. Therefore volume should never be less than 1.

kaibetc
3
kaibetc 2016.10.31 15:34  

//Copy and paste and test, hope it helps

static datetime _lastBarTime = 0;

int OnInit()
  {
   _lastBarTime = iTime(Symbol(),0,0);
   return(INIT_SUCCEEDED);

  }

bool NewBar()

{
 if (iTime(Symbol(),Period(),0) != _lastBarTime)
  {
  _lastBarTime = iTime(Symbol(), Period(),0); 
 
  return (true);
 }
  else
  return (false);
}

int start()
  {   
if(NewBar()==true)

   {

closePendingOrder();

  }

void closePendingOrder()

{

for(int a=0;a<OrdersTotal();a++)
   {

     if(OrderSelect(a,SELECT_BY_POS)==true)

     {

if(OrderType()==OP_SELLSTOP || OrderType==OP_BUYSTOP)
        {
          
           OrderDelete(OrderTicket(),Violet);
         
        }

}

   }

}

whroeder1
14470
whroeder1 2016.10.31 17:39  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
To add comments, please log in or register