Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Proper Structure to Delay StopLoss Execution

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

    Proper Structure to Delay StopLoss Execution

    What is the recommended structure to delay the application of StopLoss and ProfitTarget orders in order to avoid synchronization issues when a gap occurs between the closing price of the signal candle and the opening of the next bar?

    I'm currently using SetStopLoss() and SetProfitTarget() inside OnBarUpdate, with a delay of one or two candles after the execution is confirmed (execution price captured in OnExecutionUpdate). However, I still experience occasional problems where positions are closed incorrectly, especially during gap openings, and the results are registered as stop losses even when price hasn't touched the stop level.

    Is there a more reliable way to structure this logic to ensure the stop/target orders are placed only after a proper confirmation, and avoid these sync issues in live trading?

    Thanks in advance for any guidance!

    #2
    Hello Pezerra,

    When using SetStopLoss that will be submitted at the time of the entry fill if you had called that at the same time as the entry or in OnStateChange. If you wanted to delay the targets you would have to wait to call that method when your logic determines. There is not a specific guideline for that, it would depend on your logic.

    Comment


      #3
      Thanks, Jesse. I’m already calling SetStopLoss() inside OnBarUpdate, delayed by 1 or 2 candles after the entry is filled (capturing the price in OnExecutionUpdate).

      However, I still notice stop executions even when the price hasn't touched the stop level, or worse, profitable trades being marked as stopped out.

      Is there any known issue with this behavior? Or any best practice to ensure stops are applied correctly only after being sent?

      Comment


        #4
        Hello Pezerra,

        For a stop to work the price would have to have traded at the price based on how the order type being used works. If you are trading in realtime and the stop is not working as expected you would have to refer to your brokers statement to see what price the order filled at and compare that with the platform. If the order filled on the brokers end that would mean the market traded at that price. If you use StopMarket orders those are also market orders so you can see some slippage with that as well.

        Comment


          #5
          I believe I might not be expressing my issue clearly enough, and I’d like to clarify that this is not related to slippage or broker-side behavior.

          Please follow my reasoning and help me with a practical question:

          I’m using a delayed approach for submitting the stop loss. Specifically, I only call SetStopLoss() two bars after the entry is executed. I store the bar number inside OnExecutionUpdate(), and then apply the stop conditionally inside OnBarUpdate():

          // Inside OnExecutionUpdate
          if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
          {
          if (execution.Order.Name == "MyLongEntry")
          candleOfEntryLong = CurrentBar;
          }

          // Inside OnBarUpdate
          if (candleOfEntryLong > 0 && CurrentBar >= candleOfEntryLong + 2)
          {
          double entryPrice = Position.AveragePrice;
          SetStopLoss("MyLongEntry", CalculationMode.Price, entryPrice - 12 * TickSize, false);
          candleOfEntryLong = -1;
          }

          Based on this logic, I expect the position to remain open and no stop loss to be active during those 2 bars following the entry. However, I’m seeing the position being closed during that period, and marked as a stop loss — even though SetStopLoss() has not been called yet.

          Can you please help me understand what might be causing this?

          Comment


            #6
            Hello Pezerra,

            You would need to use prints to see how your logic is working in that use case. I can't tell from the code how it may have worked to know why it did that.

            In general that is not a valid way to wait 2 bars, you need to put that logic in OnBarUpdate. OnExecutionUpdate is only called for executions.

            Comment


              #7
              I think this is happening because you set the stop-loss order dynamically. In this case you MUST reset the stop order when the account is flat.
              adtsystems
              NinjaTrader Ecosystem Vendor - ADTS Systems

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              111 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              59 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              38 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              42 views
              0 likes
              Last Post TheRealMorford  
              Started by Mindset, 02-28-2026, 06:16 AM
              0 responses
              78 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Working...
              X