Close pending orders when new candle started

 

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);}} 

 

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
        //
        }

.

 

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.

 
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
 
  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)
 
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.
 

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

 

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.
 
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.

 

//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);
         
        }

}

   }

}

 
  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)
Reason: