//+------------------------------------------------------------------------------------------------------------------------------------------+
//|                                                                                                                        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
//|  ver 1.02 - added hidden Take Profit line, as well as two boolean flags.  
//|  amitesting boolean - default setting is false.  If changed to true, this will place a trade (either buy or sell, depending on value of buyorsell boolean
//|  With this, you can use the system tester, look at the chart and get the high /low of the chart and then enter these values for dStopLoss and dTakeProfit
//|  ver 1.03 - added Horizontal lines that can be used for visual reference for Stop Loss and Take Profit
//|  Modified to take Ticket Number, not by MagicNumber
//+------------------------------------------------------------------------------------------------------------------------------------------+
#property copyright "almo/BlueRain"
#property link      ""
#property version   "1.03"
#property strict

enum Operation
{
   BUY=OP_BUY,
   SELL=OP_SELL,
   BOTH=OP_BUY+OP_SELL
};


//--- input parameters
input double   dStopLossLine=0.0;
input double   dTakeProfit =0.0;
input string  sTicketNumber = "12345,2345";
input Operation    Ops =  BOTH;  
input color   SLlinecolor = clrYellow;
input color   TPlinecolor = clrAqua;


int iTickets[];


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
    string temp[];
   StringSplit(sTicketNumber,',',temp);
   for ( int i = 0; i < ArraySize(temp); i++)
   {
      iTickets[i] = StringToInteger(temp[i]);
   }
   
   
   
//--- 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 || dTakeProfit ==0)
     {
      Comment("Check Your Stop Loss Line and Take Profit line");

     }

   if(dStopLossLine !=0)
     {
      ObjectCreate(0,"StopLoss",OBJ_HLINE,0,Time[80],dStopLossLine,Time[0],dStopLossLine);
      ObjectSet("StopLoss",OBJPROP_COLOR,SLlinecolor);
      Comment("We have a valid stop loss");
      
       for ( int n = 0; n < ArraySize(iTickets); n++)
       {
          CloseStopLoss(iTickets[n]);
       }     
      
   }
   
   
   if(dTakeProfit !=0)
     {
     ObjectCreate(0,"TakeProfit",OBJ_HLINE,0,Time[80],dTakeProfit,Time[0],dTakeProfit);
     ObjectSet("TakeProfit",OBJPROP_COLOR,TPlinecolor);
     Comment("We have a valid TakeProfit located at : ", DoubleToStr(dTakeProfit,5), " and Stop Loss at : " , DoubleToStr(dStopLossLine,5));
     
         for ( int n = 0; n < ArraySize(iTickets); n++)
         {
             CloseTakeProfit(iTickets[n]);
         }     
      
         
     }
 


  }  //end OnTick()
  
 
 
 
  
  void CloseStopLoss(int ticketnumber)
  {
  
     if(OrdersTotal() > 0)
        for(int i = OrdersTotal(); i>=0; i--)
           {
            int chk = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
           
            if(OrderSymbol() == Symbol() && OrderTicket() == ticketnumber )
              {
               switch(OrderType())
                 {
                  
                   case OP_BUY:
                   {
                     if ( Ops == BUY || Ops == BOTH ) 
                     {
                        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 ( Ops == SELL || Ops == BOTH ) 
                       {
                        
                        if(Close[1] > dStopLossLine)
                          {
                           int chk2 = OrderClose(OrderTicket(),OrderLots(),Ask,0,clrRed);
                           if(chk2 !=0)
                              Print("Cannot close Sell ticket - Error code is: ",GetLastError());
                          }
                          
                    
                        }
                    }
                 }  //end Switch
              }  //end If OrderSymbol()
         
            
            
           } // end for Loop
 }  

 void CloseTakeProfit(int ticketnumber)
 {
   
    if(OrdersTotal() > 0)
         for(int i = OrdersTotal(); i>=0; i--)
           {
           
            int chk = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          
            
            if(OrderSymbol() == Symbol() && OrderTicket() == ticketnumber )
              {
               switch(OrderType())
                 {
                  case OP_BUY:
                    {
                       
                      if ( Ops == BUY || Ops == BOTH ) 
                      {
                        if(Close[0] >= dTakeProfit)
                          {
                           int chk2 = OrderClose(OrderTicket(),OrderLots(),Bid,0,clrRed);
                           if(chk2 !=0)
                              Print("Cannot close Buy ticket for Take Profit- Error code is: ",GetLastError());
                          }
                        break;
                      }
                    }
                  case OP_SELL:
                    {
                    
                    if ( Ops == SELL || Ops == BOTH ) 
                      {
                      
                        if(Close[0] <= dTakeProfit)
                          {
                           int chk2 = OrderClose(OrderTicket(),OrderLots(),Ask,0,clrRed);
                           if(chk2 !=0)
                              Print("Cannot close Sell ticket for Take Profit - Error code is: ",GetLastError());
                          }
                        break;
                     }
                    }
                 }  //end Switch
              }  //end If OrderSymbol()
            
           } // end for Loop


     }  //end if dTakeProfit != 0)
     


//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
