Help with check to close on new bar

 

I'm trying to make sure I have the right coding to check and execute a possible close of position After a new bar from opening a position. 

Can anyone confirm if this code is good.  It seems as if each time it checks for close, it would set the "currentbar" again to datetime?


void CheckForClose()
  {
//   double ma;
//--- go trading only after new bar

datetime currentbar;


RSI0=iRSI(NULL,0,period_RSI,PRICE_CLOSE,0);
sto_main_curr = iStochastic (Symbol() ,PERIOD_CURRENT ,k_period ,d_period ,slowing ,ma_method ,price_field ,MODE_MAIN   ,0);

//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
//here is the close buy condition  
         if  (RSI0 >= exitbuy) 
          {
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Black))
               Print("OrderClose error ",GetLastError());
           }

         if(currentbar!=iTime(Symbol(),PERIOD_CURRENT,0)) // new candle 
           {
            if (sto_main_curr <= over_sold && RSI0 <= over_sold) 
            {          
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Black))
               Print("OrderClose error ",GetLastError());
            currentbar=iTime(Symbol(),PERIOD_CURRENT,0);    // overwrite old with new value   
               }
           }
          break;
        }
      if(OrderType()==OP_SELL)
        {
      // here is the close sell condition  
         if (RSI0 < exitsell)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Sienna))
               Print("OrderClose error ",GetLastError());
           }

         if(currentbar!=iTime(Symbol(),PERIOD_CURRENT,0)) // new candle 
         
           {
            if (sto_main_curr >= over_bought && RSI0 >= over_bought)
            {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Sienna))
               Print("OrderClose error ",GetLastError());
            currentbar=iTime(Symbol(),PERIOD_CURRENT,0);    // overwrite old with new value               
               }
           }  
         break;
        }
     }
//---
  }
 
static datetime currentbar = 0;
 

It is good but there can be an issue when you switch the timeframe on the chart manually.

If you want ultimate control over that you can use a global variable instead. (https://www.mql5.com/en/docs/globals/globalvariableset)

Documentation on MQL5: Global Variables of the Terminal / GlobalVariableSet
Documentation on MQL5: Global Variables of the Terminal / GlobalVariableSet
  • www.mql5.com
Global Variables of the Terminal / GlobalVariableSet - Reference on algorithmic/automated trading language for MetaTrader 5
 
Marco vd Heijden:

It is good but there can be an issue when you switch the timeframe on the chart manually.

If you want ultimate control over that you can use a global variable instead. (https://www.mql5.com/en/docs/globals/globalvariableset)

I saw info about global variables, but wasn't sure how to put that in place.
 
Konstantin Nikitin:
thanks
 
Marco vd Heijden:

It is good but there can be an issue when you switch the timeframe on the chart manually.

If you want ultimate control over that you can use a global variable instead. (https://www.mql5.com/en/docs/globals/globalvariableset)

I'm in MT4 looks like this is for MT5?
 

When you switch timeframe the bar time changes so it will trigger your code.

If you dont want that you'd have to use a fixed candle like PERIOD_H1 in stead of PERIOD_CURRENT, or use a global var.

Also if you set the static datetime currentbar = 0; to zero it will be set to zero each time the program is reinitialized which can be undesirable as well.