Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue with SL not moving to BE

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

    Issue with SL not moving to BE

    I'm working on an automated strategy for NinjaTrader 8, but I'm encountering an issue where the stop loss isn't being moved to breakeven after a certain number of ticks. I've set up the strategy to take a trade with a specific stop loss and target, but I want it to trail the stop to breakeven once the price has moved in my favor by a set number of ticks. However, it seems that this functionality isn't triggering as expected. Could anyone provide insight into what might be causing this or suggest how I can implement the trailing stop to breakeven correctly?

    Thanks in advance!

    #2
    Is your calculations set to OnBarClose, OnPriceChange or OnEachTick?

    How does the code look for setting the amount of ticks for the trigger and the logic for changing it to breakeven?
    Generally, I would say to add some print statements to make sure everything is acting how it's supposed to, and to let you figure out when and where exactly is the code not acting as intended.

    Posting the code would be the most helpful way for others to help you figure this out.

    Comment


      #3
      Hello Buttony,

      Below is a link to examples of custom stop order modification you may find helpful.


      rockmanx00 is asking good questions. Use Calculate.OnPriceChange (or .OnEachTick) to run logic intra-bar.
      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.


      If this is in historical, 1-tick intra-bar granularity and TickReplay would also be necessary.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by rockmanx00 View Post
        Is your calculations set to OnBarClose, OnPriceChange or OnEachTick?

        How does the code look for setting the amount of ticks for the trigger and the logic for changing it to breakeven?
        Generally, I would say to add some print statements to make sure everything is acting how it's supposed to, and to let you figure out when and where exactly is the code not acting as intended.

        Posting the code would be the most helpful way for others to help you figure this out.

        Thank you for responding! I'm not sure if I posted the code right, but I think I did. I have it on OnBarClose, but I've tried it on price change and on each tick and nothing changed.
        Attached Files

        Comment


          #5
          Hello Buttony,

          Did you change the Calculate setting in the Strategies window for the configured instance?

          Or did you change the default value assigned to Calculate in State.SetDefaults of OnStateChange()?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello Buttony,

            Did you change the Calculate setting in the Strategies window for the configured instance?

            Or did you change the default value assigned to Calculate in State.SetDefaults of OnStateChange()?
            I only tried from the strategies window. I'll change the OnStateChange method to OnEachTick and OnPriceChange

            Comment


              #7
              Assuming you can get the strategy to enter a long or short position, and it's not moving the stoploss after the 50 ticks, I would say try to put a print statement to see what is happening with the stop order. You have if (stopOrder != null). The first thing I would check if that is working properly. If so, then the problem might lie within the logic of the changeOrder, which seems fine to me.

              Comment


                #8
                Originally posted by rockmanx00 View Post
                Assuming you can get the strategy to enter a long or short position, and it's not moving the stoploss after the 50 ticks, I would say try to put a print statement to see what is happening with the stop order. You have if (stopOrder != null). The first thing I would check if that is working properly. If so, then the problem might lie within the logic of the changeOrder, which seems fine to me.
                Okay, I went through some market replay to test it with the print() and it seems like it is identifying when the stop loss needs to be moved. However, it doesn't place the order until after the trade is over, whether it hits TP or original SL. So almost as if it's executing too late.

                Comment


                  #9
                  ok. So it IS working? Was it still on OnBarClose? Or did you change it to OnPriceChange/ OnEachTick?

                  Comment


                    #10
                    Originally posted by rockmanx00 View Post
                    ok. So it IS working? Was it still on OnBarClose? Or did you change it to OnPriceChange/ OnEachTick?
                    No, it isn't working. It only says "moved to breakeven" after the trade is closed, whether it hit my TP or 50 tick SL. It still doesn't actually place in order for breakeven. It shows this after the trades are already closed. "Setting breakevenPrice for Short: 21391
                    Changing stop order for Short to breakeven: 21391​"

                    Also, I changed it to OnEachTick.

                    Comment


                      #11
                      Ok. Where are you putting those print statements? I'm assuming there isn't an issue with "if (Position.MarketPosition == MarketPosition.Long && !isBreakevenLong)" and the short variant. I'm guessing there is no issue for "double breakevenPrice = Position.AveragePrice" either. Is it setting the breakeven price to the price it's supposed to be?
                      have you tried adding Print(stopOrder); as well to see if stopOrder is changing from null to execution.Order properly?

                      Comment


                        #12
                        Originally posted by rockmanx00 View Post
                        Ok. Where are you putting those print statements? I'm assuming there isn't an issue with "if (Position.MarketPosition == MarketPosition.Long && !isBreakevenLong)" and the short variant. I'm guessing there is no issue for "double breakevenPrice = Position.AveragePrice" either. Is it setting the breakeven price to the price it's supposed to be?
                        have you tried adding Print(stopOrder); as well to see if stopOrder is changing from null to execution.Order properly?
                        No, from the debugging. It seems to be the OnExecutionUpdate. It recognizes the breakeven price, then ChangeOrder puts in a request at breakeven price. After that OnExecutionUpdate, it places places a new stop loss, but at my original stop loss (50 Ticks) instead of putting the new order to breakeven.

                        Comment


                          #13
                          Ok. So the code for breakeven is putting the correct price? Is this triggering after 50 ticks? I would try to put Print(stopOrder) and see what that says. I'm wondering if something in that part is not working. I would also put a Print(breakevenPrice) in OnExecutionUpdate, just to make sure it's that's doing what it's supposed to as well.

                          Comment


                            #14
                            I tried to copy the code, taking out the time restrictions and the waveTrend portions. When the strategy enters a position, it enters OnExecutionUpdate and the execution.Order.Name == "short". So the code you have in OnExecutionUpdate does not execute and stopOrder is not assigned. At this point it is still null. However when price hits the 50 tick SL, it then creates a new execution.Order, this time the name is "Stop loss". The code you have then gets triggered. This is why your code seems to be triggering AFTER the trade closes.

                            Comment


                              #15
                              I think I got it to work. I tried to use stopOrder = execution.Order but that didn't seem to work for me. I took a short and didn't close until the end of day. So I did this instead...

                              I added a targetOrder variable and replaced the code within OnExecutionUpdate with
                              if (execution.Order != null && execution.Order.Name == "Long")
                              {
                              stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Price - 50 * TickSize, "buyStop", "Long");
                              targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Price + 100 * TickSize, "buyTP", "Long");
                              }​

                              Same logic for Shorts.

                              This allowed the breakeven portions to trigger properly. I turned on traceOrders to true and it does confirm that the stoploss seems to be moving, at least historically. The execution markers confirm this as well.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              63 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              139 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              75 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              45 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              50 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X