Sequence StopLoss Trailing

 

Hello,


I am creating a code, which trail all the order to same price at ones with magic number. Let say, there are 20 Open Order with Magic number 100, i want to trail all the order at same time at same price. To do this, i am trying like this

double gPips = Point;

void SequenceTrailing(int SequenceTrailing_magic)
  {
   double TrailingBuy_Price = LastOrderPrice(OP_BUY, SequenceTrailing_magic) + Trailing_Gap * gPips;
   double TrailingSell_Price = LastOrderPrice(OP_SELL, SequenceTrailing_magic) - Trailing_Gap * gPips;

   for(int pos_8 = OrdersTotal() - 1; pos_8 >= 0; pos_8--)
     {
      OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == SequenceTrailing_magic)
        {
         if((Bid - TrailingBuy_Price > gPips * Trailing_Gap) && (OrderStopLoss() < Bid - gPips * Trailing_Gap || OrderStopLoss() == 0.0))
           {
            ResetLastError();
            gOrderModifyCheck = OrderModify(OrderTicket(), TrailingBuy_Price, Bid - gPips * Trailing_Gap, OrderTakeProfit(), 0, clrNONE);
            if(!gOrderModifyCheck)
               Print(__FUNCTION__ + " Sequence_Trailing Order Error Code : " + GetLastError());
           }
        }
      else
         if(OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == SequenceTrailing_magic)
           {
            if((TrailingSell_Price - Ask > gPips * Trailing_Gap) && (OrderStopLoss() > Ask + gPips * Trailing_Gap || OrderStopLoss() == 0.0))
              {
               ResetLastError();
               gOrderModifyCheck = OrderModify(OrderTicket(), TrailingSell_Price, Ask + gPips * Trailing_Gap, OrderTakeProfit(), 0, clrNONE);
               if(!gOrderModifyCheck)
                  Print(__FUNCTION__ + " Sequence_Trailing Order Error Code : " + GetLastError());
              }
           }
     }
  }


Problem with this code, it not perfectly trailing all order at same Stop Loss Price at ones and several time it gives error 130

 
  1.             gOrderModifyCheck = OrderModify(OrderTicket(), TrailingBuy_Price, Bid - gPips * Trailing_Gap, OrderTakeProfit(), 0, clrNONE);
    

    You can't modify the price of open orders.

  2.          if((Bid - TrailingBuy_Price > gPips * Trailing_Gap) && (OrderStopLoss() < Bid - gPips * Trailing_Gap || OrderStopLoss() == 0.0))
    

    Make sure you are moving the SL at least a tick. You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On some ECN type brokers, the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

    The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)

 
William Roeder #:
You can't modify the price of open orders.

I am not trying to modify price of open orders. That is a price to keep the distance between Order Entry Price + 10, so it will start trailing from 10 point above/below the entry price.


William Roeder #:
Make sure you are moving the SL at least a tick. You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).

Yes, StopLevel is distance for SL/TP/Pending orders from Order Entry price - Right?.

Here, i modified my code :

//+------------------------------------------------------------------+
//| Trailing Buy for Martingale (Type : Sequence_Trailing)           |
//+------------------------------------------------------------------+
void SequenceTrailing_Buy()
  {
   double TrailingBuy_Price = LastOrderPrice(OP_BUY, BuyLimitMagic) + Trailing_TP * gPips;
   double StopLevel_TrailingGap = Trailing_Gap < gStopLevel ? gStopLevel + 2 :  Trailing_Gap;
   
   for(int pos_8 = OrdersTotal() - 1; pos_8 >= 0; pos_8--)
     {
      OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == BuyLimitMagic)
        {
         if(OrderType() == OP_BUY)
           {
            if(Bid - TrailingBuy_Price > gPips * StopLevel_TrailingGap)
              {
               if(OrderStopLoss() < Bid - gPips * StopLevel_TrailingGap || OrderStopLoss() == 0.0)
                 {
                 ResetLastError();
                  gOrderModifyCheck =OrderModify(OrderTicket(), TrailingBuy_Price, Bid - gPips * StopLevel_TrailingGap, OrderTakeProfit(), 0, clrNONE);
                  if(!gOrderModifyCheck)
                  Print(__FUNCTION__ + " => Error Code : " + GetLastError());
                  return;
                 }
              }
           }
        }
     }
  }

//+------------------------------------------------------------------+
//|  Sell Sequence Trailing                                          |
//+------------------------------------------------------------------+
void SequenceTrailing_Sell()
  {
   double TrailingSell_Price = LastOrderPrice(OP_SELL, SellLimitMagic) - Trailing_TP * gPips;
   double StopLevel_TrailingGap = Trailing_Gap < gStopLevel ? gStopLevel + 2 :  Trailing_Gap;
   for(int pos_8 = OrdersTotal() - 1; pos_8 >= 0; pos_8--)
     {
      OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == SellLimitMagic)
        {
         if(OrderType() == OP_SELL)
           {
            if(TrailingSell_Price - Ask > gPips * StopLevel_TrailingGap)
              {
               if(OrderStopLoss() > Ask + gPips * StopLevel_TrailingGap || OrderStopLoss() == 0.0)
                 {
                 ResetLastError();
                  gOrderModifyCheck = OrderModify(OrderTicket(), TrailingSell_Price, Ask + gPips * StopLevel_TrailingGap, OrderTakeProfit(), 0, Red);
                  if(!gOrderModifyCheck)
                  Print(__FUNCTION__ + " => Error Code : " + GetLastError());
                  return;
                 }
              }
           }
        }
     }
  }


Problem : I want to change all order to same StopLoss Price at same time but i am getting different price.I believe it happening due to Ask + gPips * StopLevel_TrailingGap  because Ask change every tick so each order having different StopLoss price closer to each other, How can i make it does not change the newAsk = Ask value until it changed same StopLoss order to all Open order and than change it get new value for newAsk = Ask?


 
anuj71 #: I am not trying to modify price of open orders.

That is exactly what you are trying to do. The second parameter of OrderModify. One variable value can not be the open price of multiple orders.

 
William Roeder #:

That is exactly what you are trying to do. The second parameter of OrderModify. One variable value can not be the open price of multiple orders.

No, it a distance from where Trailing should start.


Problem : I want to change all order to same StopLoss Price at same time but i am getting different price.I believe it happening due to Ask + gPips * StopLevel_TrailingGap  because Ask change every tick so each order having different StopLoss price closer to each other, How can i make it does not change the newAsk = Ask value until it changed same StopLoss order to all Open order and than change it get new value for newAsk = Ask?

Reason: