//+------------------------------------------------------------------------------------------------------------------------------------------+
//|                                                                                                                        StopLoss_only.mq4 |
//|                                                                                                                                     almo |
//|                                                                                                                                          |
//|  This EA will close an existing order If and only if Price closes Below the StopLoss line on a Buy, and Above the StopLoss line on a Sell|
//+------------------------------------------------------------------------------------------------------------------------------------------+
#property copyright "almo"
#property link      ""
#property version   "1.00"
#property strict
//--- input parameters
input double   dStopLossLine=0.0;
input int      iMagicNumber=12345;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(dStopLossLine == 0)
     {
      Comment("Set your Stop Loss Line");

     }

   if(dStopLossLine !=0)
     {
  //   Comment("We have a valid stop loss");
      if(OrdersTotal() > 0)
         for(int i = OrdersTotal(); i>=0; i--)
           {
            int chk = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == iMagicNumber)
              {
               switch(OrderType())
                 {
                  case OP_BUY:
                    {
                     if(Close[1] < dStopLossLine)
                       {
                        int chk2 = OrderClose(OrderTicket(),OrderLots(),Bid,0,clrRed);
                        if(chk2 !=0)
                           Print("Cannot close Buy ticket - Error code is: ",GetLastError());
                       }
                     break;
                    }
                  case OP_SELL:
                    {
                     if(Close[1] > dStopLossLine)
                       {
                        int chk2 = OrderClose(OrderTicket(),OrderLots(),Ask,0,clrRed);
                        if(chk2 !=0)
                           Print("Cannot close Sell ticket - Error code is: ",GetLastError());
                       }
                     break;
                    }
                 }  //end Switch
              }  //end If OrderSymbol()
           } // end for Loop

     }  //end if dStopLossLine != 0)





  }  //end OnTick()
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
