//#Property 

//#include <stdlib.mqh>
//#include <stderror.mqh>
//#include <WinUser32.mqh>
#property strict

enum COP
{
   a, // Close Individual Open Orders
   b, // Close All Open Orders 
   c // Close All Orders
}; 
#property strict
//+-------------------------------------------------------------------------------------------+

input  bool    use_hidden_stop_loss = true;
input  int      hidden_sl    = 200;
input  bool    use_hidden_take_profit = true;
input  int     hidden_tp    = 400;
input  COP     COPx = b; // Close Open Orders only or All Orders
input  int       Slippage = 5;
input  bool    COC = true; // Closed Order Arrow No Color   

color AB, AS;
//+-------------------------------------------------------------------------------------------+

void hidden_take_profit()
{
  int totalorders = OrdersTotal();
  for(int i=totalorders-1;i>=0;i--)
  {
    if(OrderSelect(i, SELECT_BY_POS)== true)
    {
      bool result = false;
      if( OrderSymbol()==Symbol())
      {
        if (OrderType() == OP_BUY &&  OrderOpenPrice()+hidden_tp*Point<=Bid )  
        {
          if(COPx ==a)
          {
            result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, AB  );
          }  
          else if(COPx == b)
          {
            CO(); 
          }  
           else if(COPx == c)
          {
            CA(); 
          }
        }
          
        if (OrderType() == OP_SELL &&  OrderOpenPrice()-hidden_tp*Point>=Ask  )  
        {
          if(COPx ==a) 
          {
            result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, AS );
          }  
          if(COPx == b)
          {
            CO(); 
          }  
           else if(COPx == c)
          {
            CA(); 
          }
        }  
      }
    }
  }
  return;
}
//+-------------------------------------------------------------------------------------------+
void hidden_stop_loss()
{
  int totalorders = OrdersTotal();
  for(int i=totalorders-1;i>=0;i--)
  {
    if(OrderSelect(i, SELECT_BY_POS)== true)
    {
      bool result = false;
      if(OrderSymbol()==Symbol())
      {
        if (OrderType() == OP_BUY &&  OrderOpenPrice()-hidden_sl*Point>=Bid )  
        {
          
          if(COPx == a)
          {
             result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, AB );
          }   
          else if(COPx == b)
          {
            CO();  
          }  
          else if(COPx == c)
          {
            CA();  
          }
        }    
        
        if (OrderType() == OP_SELL &&  OrderOpenPrice()+hidden_sl*Point<=Ask)  
        {
          if(COPx == a)
          {
             result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, AS );
          }   
          else if(COPx == b)
          {
            CO();  
          }  
          else if(COPx == c)
          {
            CA();  
          }
        }  
      }
    }
  } 
  return;
}
//+-------------------------------------------------------------------------------------------+
//| expert initialization function                                                            |
//+-------------------------------------------------------------------------------------------+

int init()
  {
   
   switch(COC)
   {
      case 0: AB = Green; AS = Red; break;
      case 1: AB = clrNONE; AS = clrNONE;
   }   
   
   
   return(0);
  }
  
//+-------------------------------------------------------------------------------------------+
//| expert deinitialization function                                                          |
//+-------------------------------------------------------------------------------------------+

int deinit()
{

   return(0);
}

//+-------------------------------------------------------------------------------------------+
//| expert start function                                                                     |
//+-------------------------------------------------------------------------------------------+

int start()
{
//----
if (use_hidden_stop_loss)    hidden_stop_loss();
if (use_hidden_take_profit)  hidden_take_profit();
//----
   return(0);
}


//+-------------------------------------------------------------------------------------------+

void CO()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    if(OrderSelect(i, SELECT_BY_POS)==true)
    {
      int type   = OrderType() ;

      bool result = false;
    
      if(Symbol() == OrderSymbol())
      {
        switch(type)
        {
          case OP_BUY   : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, AB ); break;
          case OP_SELL  : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, AS );
        }
    
     //   if(result == false)
     //  {
   //     Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
    //      Sleep(3000);
     //   }  
      }
    }
  } 
}     
      
void CA()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    if(OrderSelect(i, SELECT_BY_POS)==true)
    {
      int type   = OrderType() ;

      bool result = false;
    
      if(Symbol() == OrderSymbol())
      {
        switch(type)
        {
          case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, AB ); break;
          case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, AS ); break;
          case OP_BUYLIMIT  : result = OrderDelete( OrderTicket() ); break;
          case OP_BUYSTOP   : result = OrderDelete( OrderTicket() ); break;
          case OP_SELLLIMIT : result = OrderDelete( OrderTicket() ); break; 
          case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
        }
    
      //  if(result == false)
       // {
      //    Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      //    Sleep(3000);
     //  }  
      }
    }
  } 
  
}     
      
      