Detecting Stop Loss

 

I am coding an Expert Advisor and I need a good way to detect stop loss.

I have written this function but I'm not sure whether it would work correctly. Can someone help me with this?

The function returns and integer to specify whether the order is BUY or SELL and whether stop loss is TRUE or FALSE. 

int StopLossHit (int OrderNumber)   //0: buy,true //1: buy,false //2: sell, true //3: sell, false

{

   int StopLossValue;

   OrderSelect(OrderNumber, SELECT_BY_POS);

   StopLossValue = OrderStopLoss();

   if (OrderType() == OP_BUY)

   {

      if(MathAbs(StopLossValue - Bid) < //error ) 

         return (0);

      else

         return (1);

   }

   

   else if (OrderType() == OP_SELL)

   {

      if(MathAbs(StopLossValue - Ask) < //error )

         return (2);

      else

         return (3);

   }

} 
 

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. What are Function return values ? How do I use them ? - MQL4 forum
  3. For a Buy, when the Bid goes below the SL, the order is closed. Your OrderSelect fails. There is no true or false.
 

Thanks for your reply WHRoeder. 

If this would not work because the order would be closed, how would I detect a stop loss then?

 
mtarek93:

I am coding an Expert Advisor and I need a good way to detect stop loss.

I have written this function but I'm not sure whether it would work correctly. Can someone help me with this?

The function returns and integer to specify whether the order is BUY or SELL and whether stop loss is TRUE or FALSE. 

If you are OrderSelect() ing your order by position then use a position not a ticket number.  To select an Order by ticket it will be selected regardless of it being open or closed,  if you want to select by position you need to specify the Order pool,  read the documentation for OrderSelect()

For a closed order check the OrderClosePrice() compared to the OrderStopLoss(),  if they are close then it's a good probability that the order was closed by SL. 

 
Can you please elaborate on the difference between position and ticket number?
 
mtarek93:
Can you please elaborate on the difference between position and ticket number?

The ticket number is a unique number given to the Order by your Broker,  the position number is the position of the Order in your pool of orders,  if you have 2 open orders they will be at positions 0 and 1 in the Order pool,  there is also the History pool that holds your closed orders,  if you have 100 closed orders they will be at positions 0, 1, 2, 3, 4,  . . . 98, 99  they will all also have their own unique ticket numbers.

Reason: