Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Loss Reverts After Moving to Breakeven – How to Fix?

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

    Stop Loss Reverts After Moving to Breakeven – How to Fix?

    Hi,
    I’m trying to move the stop loss to breakeven when the condition Close[0] <= priceToMove is met. However, when the price closes above priceToMove, the stop loss reverts back to its original position, which is stopLoss.

    How can I ensure that once the condition Close[0] <= priceToMove is met, the stop loss doesn’t move back to its original position?

    Thanks

    The code is in OnBarUpdate
    Code:
    double breakevenPrice = Position.AveragePrice - (10 / tickValue) * TickSize;
    double priceToMove = Position.AveragePrice - (80 / tickValue) * TickSize;
    
    if (Position.MarketPosition == MarketPosition.Flat && TriggerCondition
    )
    {
    EnterShort(Convert.ToInt32(1), "ShortEntry");
    }
    
    ExitShortStopMarket(Convert.ToInt32(1), stopLoss, "SL", "ShortEntry");
    
    if (Position.MarketPosition == MarketPosition.Short && Close[0] <= priceToMove
    )
    {
    ExitShortStopMarket(Convert.ToInt32(1), breakevenPrice, "SL", "ShortEntry");
    }​

    #2
    I have been using ChangeOrder().
    Syntax

    ChangeOrder(Order order, int quantity, double limitPrice, double stopPrice).

    so for this case, instead of another ExitShortStopMarket(), I would put ChangeOrder(stopOrder, entryOrder.Quantity, 0, breakEvenPrice);

    Comment


      #3
      Originally posted by rockmanx00 View Post
      I have been using ChangeOrder().
      Syntax

      ChangeOrder(Order order, int quantity, double limitPrice, double stopPrice).

      so for this case, instead of another ExitShortStopMarket(), I would put ChangeOrder(stopOrder, entryOrder.Quantity, 0, breakEvenPrice);
      Hi rockmanx00, I tried to implement this, but when both the entry and the SL orders are placed in the market, the SL order gets canceled after just one bar closes, while the entry order remains active and continues to run.

      Variante
      Code:
      private Order stopLossOrder;
      then just in case I use:
      Code:
      if (State == State.Realtime)
      {
      if (stopLossOrder != null)
      stopLossOrder = GetRealtimeOrder(stopLossOrder);
      }
      and after that in onbarupdate:
      Code:
      double breakevenPrice = Position.AveragePrice - (10 / tickValue) * TickSize;
      double priceToMove = Position.AveragePrice - (80 / tickValue) * TickSize;
      
      Print("breakevenPrice calculado: " + breakevenPrice.ToString());
      Print("priceToMove calculado: " + priceToMove.ToString());
      
      if (Position.MarketPosition == MarketPosition.Flat && TriggerCondition)
      {
      EnterShort(Convert.ToInt32(1), "ShortEntry");
      }
      
      if (Position.MarketPosition == MarketPosition.Short && stopLossOrder == null)
      {
      stopLossOrder = ExitShortStopMarket(Convert.ToInt32(1), stopLoss, "SL", "ShortEntry");
      Print("Initial Stop Loss set at: " + stopLoss.ToString());
      
      if (Close[0] <= priceToMove)
      {
      ChangeOrder(stopLossOrder, stopLossOrder.Quantity, breakevenPrice, stopLossOrder.LimitPrice);
      Print("Stop Loss moved to breakevenPrice: " + breakevenPrice.ToString());
      }
      }
      ​​

      Comment


        #4

        Hi, I did it. but I didnt resolve at all... I guess the problem is because I am using onbarclose... for that reason jump the condition before to touch the stop loss... anyway I try to find the way to keep using calculate onbarclose

        here is where I continue the thread just if someone need it: https://forum.ninjatrader.com/forum/...d-at-breakeven

        Comment


          #5
          There is nothing preventing your original Stop Loss code from executing AFTER your breakeven trigger has been reached. This is why it keeps reverting back.
          The easiest solution would be to add some sort of a bool to switch the StopLoss order you want executed.

          Try something like this:


          Code:
          private bool BreakevenSwitch = false;
          
          =========
          
          double breakevenPrice = Position.AveragePrice - (10 / tickValue) * TickSize;
          double priceToMove = Position.AveragePrice - (80 / tickValue) * TickSize;
          
          Print("breakevenPrice calculado: " + breakevenPrice.ToString());
          Print("priceToMove calculado: " + priceToMove.ToString());
          
          if (Position.MarketPosition == MarketPosition.Flat && TriggerCondition)
          {
          BreakevenSwitch = false
          EnterShort(Convert.ToInt32(1), "ShortEntry");
          }
          
          if (Position.MarketPosition == MarketPosition.Short && stopLossOrder == null && BreakevenSwitch == false)
          {
          stopLossOrder = ExitShortStopMarket(Convert.ToInt32(1), stopLoss, "SL", "ShortEntry");
          Print("Initial Stop Loss set at: " + stopLoss.ToString());
          }
          
          if (Close[0] <= priceToMove)
          {
          ChangeOrder(stopLossOrder, stopLossOrder.Quantity, breakevenPrice, stopLossOrder.LimitPrice);
          BreakevenSwitch = true;
          
          Print("Stop Loss moved to breakevenPrice: " + breakevenPrice.ToString());
          }
          }​
          TradeSaber Inc.NinjaTrader Ecosystem Vendor - TradeSaber Inc.

          Comment


            #6
            Hello TradeSaber,

            Thank you for the suggestion.

            One note, orders should only be assigned to variables from the order object of OnOrderUpdate().

            "OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated."
            Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


            Below is a link to an example of this.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            48 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            126 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            66 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X