Close at specified Time

 

Hi all, I need help with a code.

I need to trigger at every end of bar (currently testing on 15 bars) a code that closes my trades.

So far my code works, but only sometimes. Can´t figure out why.

Can anybody help please?

Thank you

void OnTick()
{
      if (OrdersTotal() > 0) {                             
            datetime BarCloseTime = Time[0]+(60*15)-1;
            datetime time = TimeCurrent();
            if(time == BarCloseTime){
            CloseAllTradesThisPair();     
            }
            return;
      }


//+------------------------------------------------------------------+
//Close at the end of candle
//+------------------------------------------------------------------+

// We create a function to close all trades for this pair
void CloseAllTradesThisPair()
{  
	// go through all open orders
	for (int i=OrdersTotal(); i>=0;i--) 
	{
		// select a trade
		if(OrderSelect(i,SELECT_BY_POS)==true)
		if(OrderSymbol()==Symbol())

		{
        if ( OrderType() == OP_SELL)
            OrderClose( OrderTicket(), OrderLots(),Ask,10,NULL);
        if ( OrderType() == OP_BUY)
            OrderClose( OrderTicket(), OrderLots(),Bid,10,NULL);
                datetime time = TimeCurrent();

      } // end IF
   }//end FOR
   
}//End Function 

 
SoulShatter:

Hi all, I need help with a code.

I need to trigger at every end of bar (currently testing on 15 bars) a code that closes my trades.

So far my code works, but only sometimes. Can´t figure out why.

Can anybody help please?

Thank you


such code can simply be written.

datetime prevtime;
bool NewBar()
  {
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return (true);
     }
   else
     {
      return(false);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CloseBarControls()
  {
   if(iTime(Symbol(),15,0) == prevtime)
      return(0);
   prevtime = iTime(Symbol(),15,0);
    if(NewBar()) CloseAllTradesThisPair();
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   CloseBarControls();

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CloseAllTradesThisPair()
  {
   int total = OrdersTotal();
   for(int i=total-1; i>=0; i--)
     {
      bool tsd=OrderSelect(i, SELECT_BY_POS);
      int type   = OrderType();

      bool result = false;

        {
         switch(type)
           {
            //Close opened long positions
            case OP_BUY       :
               result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
               break;

            //Close opened short positions
            case OP_SELL      :
               result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE);
               break;

            //Close pending orders

            case OP_BUYLIMIT  :
            case OP_BUYSTOP   :
            case OP_SELLLIMIT :
            case OP_SELLSTOP  :
               result = OrderDelete(OrderTicket());

           }
        }
      if(result == false)
        {
         if(GetLastError()>0)
            Alert("Order ", OrderTicket(), " failed to close. Error:", GetLastError());
         Sleep(3000);
        }
     }

   return(0);
  }



//+------------------------------------------------------------------+
 
  1.             if(time == BarCloseTime){
    

    This will be true only if you get a tick during that exact second. There can be minutes between ticks during the Asian session, think M1 chart.

  2.             datetime BarCloseTime = Time[0]+(60*15)-1;
                datetime time = TimeCurrent();
    
    Assuming that you are running on the M15 chart. They will never be equal.
 

@Mehmet Bastem - Much appreciate it - works like a charm - does not produce the results I was expecting, but works none the less

@William Roeder - Thank you for confirming that was my suspicion

Reason: