//+------------------------------------------------------------------+
//|                                                 AutoStopLoss.mq4 |
//|                                           Oswaldo Vález Palmeiro |
//|                                      http://www.oswaldovalez.com |
//+------------------------------------------------------------------+
#property copyright "Oswaldo Vález Palmeiro"
#property link      "http://www.oswaldovalez.com"
//+------------------------------------------------------------------+
//| variables                                                        |
//+------------------------------------------------------------------+
int StopLoss = 20;   //Nivel de Stop Loss para todas las operaciones
double Pip = 0.0001; //Tamańo del pip para el par en el que se opera
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      int ordenes = OrdersTotal();
      
      if (ordenes == 0)
         return;
      
      for (int i = 0; i < ordenes; i++)
      {
         if(OrderSelect(i,SELECT_BY_POS)==false)
            continue;
         
         if(OrderStopLoss() == 0)
         {
            int tipoOrden = OrderType();
            
            if (tipoOrden == 0 || tipoOrden == 2 || tipoOrden == 4)
            {
               //Orden de compra
               OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - (Pip * StopLoss), OrderTakeProfit(), 0, Blue);
            }
            
            if (tipoOrden == 1 || tipoOrden == 3 || tipoOrden == 5)
            {
               //Orden de venta
               OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + (Pip * StopLoss), OrderTakeProfit(), 0, Blue);
            }  
         }
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+