Checking whether a order/position has a stoploss set

 

Hi

Is there a way to determine whether  a current/open position has an S/L set? e.g. true, false, and then extract what value is set?

I cant figure out a way to do it.

thanks
ian 

 
successful OrderSelect followed by

if( OrderStopLoss() != 0. )


was that so hard?
 

And while we are on the subject, here is some code that should look for a missing stop and take profit and if missing attempt to set a stop and take profit. I have not tested the code yet. (use at your own risk).

bool FindTradesMissingSLTP(int dir, double stop, double take, string comment)
{
   bool result = true;
   for (int ix = OrdersTotal()-1; ix >= 0; ix--) 
   {
      OrderSelect(ix, SELECT_BY_POS, MODE_TRADES);
      
      // look for one of the EAs trades
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) // per recomendation delete this -> && OrderComment() == comment) 
      {
         // if the trade has neither stop loss or take profit attempt to assign one
         if (OrderStopLoss() <= 0 && OrderTakeProfit() <= 0)
            if (!AssignSLTP(OrderTicket(), dir, stop, take))
               result = false;   // record whether AssignSLTP ever fails
      }
      
   }  // end for (int ix = OrdersTotal()-1; ix >= 0; ix--)
   
   // return result of our attempt
   return (result);
}
 
MisterDog:

And while we are on the subject, here is some code that should look for a missing stop and take profit and if missing attempt to set a stop and take profit. I have not tested the code yet. (use at your own risk).

 

It's a bad idea to use Order comments,  they can be changed or removed by your Broker . . .  do this to make your function a little more flexible . . .

bool FindTradesMissingSLTP(int dir, double stop, double take, string comment = "")

 . . .  then if Order comment is not used it does not need to be specified when calling your function.

 
Lol, thanks, I dont know how I missed that! thanks to others for input to, always appreciated.