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

 
void trailingstop_check(int trail,int magic=-1)
 { for(int i=0; i<OrdersTotal();i++)
   { if(OrderSelect(i,SELECT_BY_POS))
      { if(magic == -1 || magic == OrderMagicNumber())
      trailingstop_check_order(OrderTicket(),trail);}} }
      

bool trailingstop_check_order(int ticket,int trail)

  {
  trail = 10;
   if(ticket>=0) return true;
   if(!OrderSelect(ticket,SELECT_BY_TICKET)) return false;
   int digits=(int) MarketInfo(OrderSymbol(),MODE_DIGITS);
   double point=MarketInfo(OrderSymbol(),MODE_POINT);
   double price = 0;
   bool result=true;
   if(OrderType()==OP_BUY)
     {
      double newsl=Bid-trail*point;
      if (newsl>OrderStopLoss() && newsl>OrderOpenPrice()){
     result=OrderModify(OrderTicket(), price, newsl, OrderTakeProfit(), OrderExpiration());}
     }
   else if(OrderType()==OP_SELL)
     {
      double newsl=Ask+trail*point;
      if (OrderStopLoss()>newsl && OrderOpenPrice()>newsl){ 
         result= OrderModify(OrderTicket(), price, newsl, OrderTakeProfit(), OrderExpiration());}
        }
     return result;
  }      
 
hanife issa:

Please re-edit your post and place your code properly by using the "</>" icon from the posting toolbar or use Alt+S. Use proper styling, by using MetaEditor's Styler functionality.

Also, explain in detail what are the problems that you are facing and what do you expect your code to do (that it is not doing). Post also the log output of any errors it reports.

 
Also, make sure your code sample can be compiled (without errors or warnings) and that it is self-contained and can be tested as is, otherwise we will not be able test and debug it on our end if needed.
 
hanife issa:
****

Do not double post!

I have deleted your duplicate topic.

Post code properly as Fernando has already said.

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Fernando Carreiro:
Also, make sure your code sample can be compiled (without errors or warnings) and that it is self-contained and can be tested as is, otherwise we will not be able test and debug it on our end if needed.
I compiled it successfully without any error. this is only the part of trailing 
 
if(ticket>=0) return true;

What is the purpose of this?

What do you think it does?

 
Keith Watford:

What is the purpose of this?

What do you think it does?

Checking if I have opened orders
 
hanife issa: Checking if I have opened orders

Then your trailing stop code will not work! If you call the function "trailingstop_check_order" with a valid ticket number (which is greater than 0), for an obviously open order, and then return from the function because it is valid, then nothing more will be done by the function. What is the logic of that?

 

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:

Then your trailing stop code will not work! If you call the function "trailingstop_check_order" with a valid ticket number (which is greater than 0), for an obviously open order, and then return from the function because it is valid, then nothing more will be done by the function. What is the logic of that?

Even with less than 0.it's not working. What's your suggest
 
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.