Stop Loss не переходит в Trailing Stop, а затем не срабатывает переход в Breakeven.

 

Я уже всю голову поломал, почему Trailing Stop и Breakeven вообще не тригерятся. Ладно бы делали это с ошибками, но нет ни ошибок, ни исполнения. Может есть кто-то с опытным глазом, кто сможет заметить очевидную ошибку, которую я в упор не замечаю.


//+------------------------------------------------------------------+
//| Manage Open Orders: Breakeven and Trailing Stop                  |
//+------------------------------------------------------------------+
void ManageOpenOrders()
{
    for (int i = 0; i < PositionsTotal(); i++)
    {
        // Select the position by index
        ulong ticket = PositionGetTicket(i); // Get the position ticket by index
        if (PositionSelect(ticket))
        {
            // Check if the position is on the current symbol
            if (PositionGetString(POSITION_SYMBOL) != _Symbol)
                continue; // Skip positions not opened on this symbol

            double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Get the current price
            double newStopLoss;

            // For Buy Positions
            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
            {
                // Immediate Trailing Stop Logic
                newStopLoss = currentPrice - TrailingStep * _Point;
           if (newStopLoss > PositionGetDouble(POSITION_SL))
           {
                trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP));
           }

                // Breakeven logic
            if ((currentPrice - entryPrice) >= BreakevenTrigger * _Point)
           {
                newStopLoss = entryPrice + BreakevenOffset * _Point;
                trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP));
           }
            }
            // For Sell Positions
            else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
            {
                // Immediate Trailing Stop Logic
                newStopLoss = currentPrice + TrailingStep * _Point;
                if (newStopLoss < PositionGetDouble(POSITION_SL))
               {
                   trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP));
               }

                // Breakeven logic
                if ((entryPrice - currentPrice) >= BreakevenTrigger * _Point)
                {
                  newStopLoss = entryPrice - BreakevenOffset * _Point;
                  trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP));
                }
            }
        }
    }
}
 
//+------------------------------------------------------------------+
//| Manage Open Positions: Breakeven and Trailing Stop   |
//+------------------------------------------------------------------+
void ManageOpenPositions()
  {
   for(int i = 0; i < PositionsTotal(); i++)
     {
      if(PositionGetSymbol(i)==_Symbol) // Select and check if the position is on the current symbol
        {
         ulong  ticket = (ulong)PositionGetInteger(POSITION_TICKET);      // Get the position ticket
         double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN);  // Get the position open price
         double SL_Price = PositionGetDouble(POSITION_SL);                    // Get the position StopLoss price
         double TP_Price = PositionGetDouble(POSITION_TP);                   // Get the position TakeProfit price
         double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Get the current price
         double newStopLoss=0.0;
         // For Buy Positions
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
           {
            // Immediate Trailing Stop Logic
            newStopLoss = currentPrice - TrailingStep * _Point;
            if(newStopLoss > SL_Price)
              {
               trade.PositionModify(ticket, newStopLoss, TP_Price);
              }
            // Breakeven logic
            if((currentPrice - entryPrice) >= BreakevenTrigger * _Point)
              {
               newStopLoss = entryPrice + BreakevenOffset * _Point;
               trade.PositionModify(ticket, newStopLoss, TP_Price);
              }
           }
         // For Sell Positions
         else
           {
            if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
              {
               // Immediate Trailing Stop Logic
               newStopLoss = currentPrice + TrailingStep * _Point;
               if(newStopLoss < SL_Price)
                 {
                  trade.PositionModify(ticket, newStopLoss, TP_Price);
                 }
               // Breakeven logic
               if((entryPrice - currentPrice) >= BreakevenTrigger * _Point)
                 {
                  newStopLoss = entryPrice - BreakevenOffset * _Point;
                  trade.PositionModify(ticket, newStopLoss, TP_Price);
                 }
              }
           }
        }
     }
  }
 
Ruslan Khasanov #:

Огромное. Трижды огромное спасибо.

 
Vadym Dubenko #:

Огромное. Трижды огромное спасибо.

возможно при селл такая строка актуальна вместо предложенной:

if(newStopLoss < SL_Price || SL_Price == 0)

добавил трал в ф-ии по символ и магику с профита (не проверял):

//+------------------------------------------------------------------+
//| Manage Open Positions: Trailing Stop   |
//+------------------------------------------------------------------+
void ManageOpenPositions(string Sym, int mn)
  {
   if(TrailingStop > 0)
    for(int i = 0; i < PositionsTotal(); i++)
     {
      if(PositionGetSymbol(i)==Sym) // Select and check if the position is on the current symbol
      if(PositionGetString(POSITION_SYMBOL) == Sym)
      if(PositionGetInteger(POSITION_MAGIC)==mn || mn == -1)
        {
         ulong  ticket = (ulong)PositionGetInteger(POSITION_TICKET);      // Get the position ticket
         double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN);  // Get the position open price
         double SL_Price = PositionGetDouble(POSITION_SL);                    // Get the position StopLoss price
         double TP_Price = PositionGetDouble(POSITION_TP);                   // Get the position TakeProfit price
         double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Get the current price
         double currentPrice_sell = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Get the current price
         double newStopLoss=0.0;
         // For Buy Positions
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
           {
            // Immediate Trailing Stop Logic
            newStopLoss = currentPrice - TrailingStop * _Point;
            if(newStopLoss > SL_Price && entryPrice < newStopLoss)
              {
               trade.PositionModify(ticket, newStopLoss, TP_Price);
              }
            // Breakeven logic
            //if((currentPrice - entryPrice) >= BreakevenTrigger * _Point)
            //  {
           //    newStopLoss = entryPrice + BreakevenOffset * _Point;
           //    trade.PositionModify(ticket, newStopLoss, TP_Price);
           //   }
           }
         // For Sell Positions
         else
           {
            if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
              {
               // Immediate Trailing Stop Logic
               newStopLoss = SymbolInfoDouble(_Symbol, SYMBOL_ASK) + TrailingStop * _Point;
               if(newStopLoss < SL_Price || SL_Price == 0)
               if(entryPrice  > newStopLoss)
                 {
                  trade.PositionModify(ticket, newStopLoss, TP_Price);
                 }
               // Breakeven logic
              // if((entryPrice - currentPrice) >= BreakevenTrigger * _Point)
              //   {
              //    newStopLoss = entryPrice - BreakevenOffset * _Point;
              //    trade.PositionModify(ticket, newStopLoss, TP_Price);
              //   }
              }
           }
        }
     }
  }
 
Заменить _Symbol на Sym