How to close all orders before weekend. - page 3

 

      void OnTick()

   {

      if(close_end_of_week())

      {

      Close_allpending(symbol_index,POSITION_TYPE_SELL);   

      Close_allpending(symbol_index,POSITION_TYPE_BUY);  

      }

}

//+------------------------------------------------------------------+

//| Close all positions by opposite positions                        |
//+------------------------------------------------------------------+
void Close_allpending(int symbol_index,ENUM_POSITION_TYPE type)
  {
   int total=PositionsTotal(); // number of open positions   
//--- iterate over all open positions
   for(int i=total-1; i>=0; i--)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);                                    // ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL);                      // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);            // ticket of the position
      ulong  magic=PositionGetInteger(POSITION_MAGIC);                                // MagicNumber of the position

      //--- if the MagicNumber matches
      if(magic==EXPERT_MAGIC)
        {
          if(position_symbol==PositionGetSymbol(i)) // symbol of the opposite position
          {
            //--- if the symbols of the opposite and initial positions match
            if(position_symbol==symbol_list[symbol_index] && PositionGetInteger(POSITION_MAGIC)==EXPERT_MAGIC)
             {
               
               //--- leave, if the types of the initial and opposite positions match
               if(type==PositionGetInteger(POSITION_TYPE))
               {
               // close end off week
               trade.PositionClose(position_ticket);
               }
             }
           }
        }
     }
  }
//+------------------------------------------------------------------+

bool close_end_of_week()
{

MqlDateTime Gmt_time;
TimeGMT(Gmt_time);
if(Gmt_time.hour>=hour_end_week && Gmt_time.day_of_week>=end_week)
{
return true;
}
return false;

}



RESULT:

BEFORE:


AFTER:


 

before


after: