[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 67

 
//+------------------------------------------------------
   for (i=0; i<OrdersTotal(); i++)
   {
      OrderSelect(i, SELECT_BY_POS );
      if (OrderSymbol() == Symbol())
      if (OrderMagicNumber() == 1000 && OrderType() == OP_BUY  && Bid-OrderOpenPrice() > 30*Point)
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid-15*Point, OrderTakeProfit(), 0);
      if (OrderMagicNumber() == 2000 && OrderType() == OP_SELL && OrderOpenPrice()-Ask > 30*Point)
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask+15*Point, OrderTakeProfit(), 0);
   }

   return(0);
//+------------------------------------------------------
 
MaxZ:
forgot to check that SL can only be moved to one side ;)
 
ilunga:
forgot to check that SL can only be moved in one direction ;)

And let it move to both! :)))))

An example of how huge, hard-to-read code can be optimised down to ten lines...

Although:

//+------------------------------------------------------
   for (i=0; i<OrdersTotal(); i++)
   {
      OrderSelect(i, SELECT_BY_POS );
      if (OrderSymbol() == Symbol())
      if (OrderMagicNumber() == 1000 && OrderType() == OP_BUY  && Bid-OrderOpenPrice() > 30*Point && Bid-OrderStopLoss() > 15*Point)
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid-15*Point, OrderTakeProfit(), 0);
      if (OrderMagicNumber() == 2000 && OrderType() == OP_SELL && OrderOpenPrice()-Ask > 30*Point && OrderStopLoss()-Ask > 15*Point)
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask+15*Point, OrderTakeProfit(), 0);
   }

   return(0);
//+------------------------------------------------------

p.s.: I would never use such Tralling, but it fits the logic of Dimka-novitsek.

 
Dimka-novitsek:
Hello! I do not understand something. I have decided to add a trailing stop to my EA, but it keeps showing error 130. I cannot understand anything, and yesterday I couldn't either.

Does your EA open more than one BUY position or more than one SELL position?
 
Thank you!
 
Yes, there are a lot of positions, but they are pending orders, buy and sell stops. When the price crosses the order, it is triggered, then the price passes another 30 pips, then the condition is triggered
f (price-OrderOpenPrice( )>30*Point)
 

In order to prevent the stop from going backwards, I have introduced a variable, declaring it above the start JUNpomnim, and the variable JUN is the difference between the price and the order's opening point.

I will take a look at the code posted here.

Thanks!!!!

The code is cool, I like it very much as an example. Of course, I try it right away. But the modification doesn't happen.

 
Now, I'm going to see the mistake somehow.
 
Carried over:
Timoxa75 11.08.2011 13:45

My name is Igor and I am new here. I am asking for help.

I am working with the EA ilan = 1.6 and put 19 here's the question. i have a good working experience on the demo and it all looks great, i returns to profitability, but i want to check it for test but i have not put it to test either on the demo or on the real site. I have a big drawdown and I want to close the order but I got a message that trades are closed.

 
Modifies, but not every order closed on profit, but the seventh, sixteenth, nineteenth, twenty-fourth, thirty-second-whenever it wants!!!
Reason: