trailing Stop Loss mql4. Hi everyone please check me why my code doen't work. Thanks - page 2

 
OrderClosePrice(), I have also tried it, that's why I don't use it
Fernando Carreiro:

Also, instead of using Bid or Ask for calculating the trailing stop, consider using the orders current closing price "OrderClosePrice()".

That way, you can combine the Buy and Sell logic into a single logic by using +ve or -ve to represent the direction in the calculations, making your code more robust without having to duplicate logic in reverse order for the two types.

 
Fernando Carreiro:
One more thing, it might be best to use OrderOpenPrice() for the price value in OrderModify() for Market Orders. I have never tried it by using a price of "0.0", so it might fail. Not sure about it though.

I have also tried it.

 
hanife issa: Even with less than 0.it's not working. What's your suggest
 Not working says nothing! What is not working? Look in the logs for errors. Add some Prints to see where the logic is failing!
 
hanife issa: OrderClosePrice(), I have also tried it, that's why I don't use it

Then you are not using it correctly!

 
Fernando Carreiro:
 Not working says nothing! What is not working? Look in the logs for errors. Add some Prints to see where the logic is failing!
Any error in the journal
 
hanife issa:I have also tried it.

You keep saying you "tried" but you don't seem to know how to use things properly, so "tried" has no meaning here!

See the following code (untested as you haven't provided a complete code to test).

bool trailingstop_check_order(int ticket,int trail)
{
   if( ( ticket >= 0 ) && OrderSelect( ticket, SELECT_BY_TICKET ) )
   {
      int direction = 0;

      switch( OrderType() )
      {
         case OP_BUY:  direction = +1; break;
         case OP_SELL: direction = -1; break;
         default:                      return false;
      };
      
      double
         point   = MarketInfo( OrderSymbol(), MODE_POINT ),
         newSL   = OrderClosePrice() - trail * point * direction,
         deltaSL = ( newSL - OrderStopLoss()  ) * direction,
         deltaOP = ( newSL - OrderOpenPrice() ) * direction;
         
      if( ( deltaSL > 0 ) && ( deltaOP > 0 ) )
         return OrderModify( ticket, OrderOpenPrice(), newSL, OrderTakeProfit(), OrderExpiration() );
      
      return true;   
   };
   
   return false;
};
 
hanife issa: Any error in the journal
Check the the expert log too!
 
Fernando Carreiro:

You keep saying you "tried" but you don't seem to know how to use things properly, so "tried" has no meaning here!

See the following code (untested as you haven't provided a complete code to test).

not working again!!!

 
hanife issa:

not working again!!!

First of all, you are not using any Print() command. So if u just check using if and return true or false, you'll never find the issue. Experienced developers may avoid it, but the beginners, mainly those who can't find his own mistake, must always use Print function.

Example:

if(condition)
  {
   Print(__FUNCTION__,", line ",__LINE__,", description of the event.");
   return false
  }
DoSomething();
return true;

Try it and check the log again.

 
hanife issa: not working again!!!

Then you have problems elsewhere. We cannot help you if don't provide a fully functioning code! So you will have to debug it yourself with the help of Print functions, just as suggested before and as in post #19 by @Samuel Manoel De Souza

Reason: