Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

I need help with traiing stop, not work! urgent please!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    I need help with traiing stop, not work! urgent please!

    Hi Boys,
    I have the following trailing stop code in my strategy in which the stop loss does not move with the trailing stop and always remains static and I do not know what the error is in the code to solve it.


    #region TRAILING STOP POR PUNTOS
    if (EnableTrailingStopByPoints && Position.MarketPosition == MarketPosition.Long)
    {
    // Calcular el nuevo precio del stop loss
    double newStopPrice = Close[0] - (TicksToAdjustTrailing * TickSize);

    // Verificar si el precio ha avanzado lo suficiente para mover el stop loss
    if (Close[0] >= entryPrice + (TicksToMoveTrailing * TickSize))
    {
    // Asegurarse de que el nuevo stop loss esté por debajo del precio de entrada y por encima del stop loss actual
    if (newStopPrice > entryPrice && newStopPrice > stopLossPrice)
    {
    // Cancelar la orden de stop loss existente si existe
    foreach (var order in Account.Orders)
    {
    if (order.Name == "myStop" && order.OrderState == OrderState.Working)
    {
    CancelOrder(order);
    Print("Orden de stop loss cancelada: " + stopLossPrice);
    break;
    }
    }

    // Actualizar el stop loss
    stopLossPrice = newStopPrice;
    ExitLongStopMarket(0, true, Position.Quantity, stopLossPrice, "myStop", "longEntry");

    // Imprimir un mensaje en el log para depuración
    Print("Trailing stop movido a: " + stopLossPrice);
    }
    }
    }
    #endregion


    THANK YOU!!!!

    #2
    Hello gustavobp,

    Have you tried using a Print statement to verify that code is being executed and that a new price is being generated? You are using a live until cancelled order so if that logic is not being called that would leave the order stationary and working until filled.

    Comment


      #3
      Hi Jesse, no, I'm not specifying that the order is GTC, could that be the error why the trailing stop is not moving?

      Comment


        #4
        GTC is the TIF for the order, that is different than live until cancelled. Live until cancelled means the order won't expire after 1 bar, that is a NinjaTrader specific feature. Your order has true in its parameters which is why I mentioned that.

        Using a print would be the first step here to make sure that your trailing logic and conditions are executing. If you don't see a print happening you would have to look at why that code is not being called by exploring the conditions and logic you made. I would suggest adding a print right before the order method to confirm that logic is being called.

        Comment


          #5
          Hi Jesse,
          Following your advice, he made a print on the trailing stop code, and in fact the price at which the trailing stop theoretically moves is modified in the Prnt, but on the chart the stop loss does not actually move,
          What would be the error then?​
          code printed was this:
          Print("Trailing stop movido a: " + stopLossPrice);

          Really thank for your help, i wait for your help again, please

          Comment


            #6
            Hello gustavobp,

            Thank you for confirming that.

            Are you calling the order method in any other locations in the script? If there is another location that may be the cause, if the order is not changing that could be if the other order method is being called and resetting the price.

            Comment


              #7
              H Jesse !!!!
              I have already solved it
              Jesse, the error was in the identifier of the Long command, which was not really longEntry, but was a different one, it now works perfectly. Thank you very much for your great help, I always appreciate your help friend.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              54 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              130 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              72 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              44 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              49 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X