How to determine the last trade closed by stoploss or not? - page 2

 

Brokers can change comments, including complete replacement. They can also not do anything with them so the above is bogus.

bool ClosedByStopLoss = MathAbs( OrderClosePrice()-OrderStopLoss() ) 
                      < MathAbs( OrderClosePrice()-OrderTakeProfit() ) 
or
double  Direction(int op_xxx){  return( 1. - 2. * (op_xxx%2) );                }
:
double DIR = Direction( OrderType() );
bool closedByStopLoss = (OrderClosePrice() - OrderStopLoss)*DIR <= 0;
 
int SonIslem()
{
    int sonIslemDurum=0;
    for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
    {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
            continue;
        if (OrderSymbol() != Symbol())
            continue;
        if (OrderProfit() == 0)
            continue;
        if (OrderProfit() > 0)
            sonIslemDurum = 1;
        if (OrderProfit() < 0)
            sonIslemDurum = -1;
        break;
    }
    return sonIslemDurum;
}
 

Based on what was written above, I ended up using the absolute value between SL price and close price in order to get any possible slipage. 

   bool closebysl=false;

   for(int i=OrdersHistoryTotal()-1; i>=0; i--)  //Cycles through the orders starting from the most recent one

     {
      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
      if(OrderMagicNumber() == magicNB)  //Determines if this order was on this MagicNb
        {
         i=-1; //Kills the for loop
         if(MathAbs(OrderClosePrice()-OrderStopLoss())<0.00004) closebysl=true; //Here I take 4 points but this could easily be 1 pip (0.00010)
         break;
        }

     }
Reason: