Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Orders being cancelled at close of the bar

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

    Stop Orders being cancelled at close of the bar

    Hello all,
    I've been struggling to place my stop orders in my strategy for a bit, so decided to post this up and ask the pros for some guidance.

    I have a strategy that places a Stop Order at the HH/HL (Swing), each time there is a new swing the strategy places a new Stop Order at the new level. The problem I'm having is that if the price does not reach that level within the next bar after the level has been set, the order seems to get cancelled. I would like for the Stop Order to remain in place at the Swing High price level until it gets triggered or there is a new Swing High and move the order to the new price. I'm printing a green diamond to check if the swing is being detected at the correct time, and also placed at the desired price (swing high + tick). I would also like to clarify that this is a Long setup only, no short orders are being placed anywhere.

    Screenshot and code below, any help would be much appreciated.

    ---

    TimeInForce = TimeInForce.Gtc;

    ---

    I'm detect the new swing high by comparing it with the previous - the lookback is set to (4). Then place the stop order at the swing price + 1 tick, draw diamond at the same price level.


    if (Swing1.SwingHigh[5] != Swing1.SwingHigh[0])
    {
    EnterLongStopMarket(Convert.ToInt32(DefaultQuantit y), (Swing1.SwingHigh[0] + (1 * TickSize)) , "long" + CurrentBar); // STOP ORDER AT SWING
    Draw.Diamond(this, "LongDiamond" + CurrentBar, true, 0, (Swing1.SwingHigh[0] + (1 * TickSize)) , Brushes.Green); // DRAW DIAMOND AT SWING
    }

    #2
    Well, after a little digging and testing out for a while, I think I finally figured out how to solve my own problem.

    For anybody interested:
    - By default a simple stop orders get cancelled at the next bar close (something I did not know).
    - You have to create a more "advanced" order that has a setting called "IsLiveUntilCancelled" set to true. You can check it out here - https://ninjatrader.com/support/help...rlonglimit.htm
    - Then you have to do some order handling and cancel those orders if not executed (after a period of time, bars, or whatever method you want to use). You can check it out here - https://ninjatrader.com/support/help...r_handling.htm

    Peace.

    Comment


      #3
      Hello juannoguerol,

      Re-submit orders on each new bar to keep them alive when not using the IsLiveUntilCancelled overload parameter (which defaults to false), or use IsLiveUntilCancelled as true to keep the order alive.

      To understand when orders are being are being automatically cancelled use TraceOrders.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello juannoguerol,

        Re-submit orders on each new bar to keep them alive when not using the IsLiveUntilCancelled overload parameter (which defaults to false), or use IsLiveUntilCancelled as true to keep the order alive.

        To understand when orders are being are being automatically cancelled use TraceOrders.
        https://ninjatrader.com/support/help...raceorders.htm
        Hi Chelsea,

        Can you point me to an example of ExitLongStopMarket/ExitShortStopMarket being re-submitted?

        I'm having an issue where doing this pulls the stop back, the trail logic moves it again, and then the re-submission pulls it back again.

        Frustrating thing is I had this working on some old code that got overly complex, so now I'm trying to simplify things but it's breaking.

        I had changed from this type of stop (ExitLongStopMarket) to SetStopLoss while trying to reduce complexity but that brought an entirely different issues so I'm going with the one I've had success with in the past.

        Comment


          #5
          Hello WalterSkinner,

          Thanks for your post.

          Here is an example of it being resubmitted:


          if (Position.MarketPosition == MarketPosition.Long)
          {
          ExitLongStopMarket(your parameters here); // resubmit on each OnBarUpdate().
          }

          "I'm having an issue where doing this pulls the stop back, the trail logic moves it again, and then the re-submission pulls it back again."
          You would need to control your logic with bool variables to prevent that situation. You would encapsulate your stop logic with the true condition of Position.MarketPosition == MarketPosition.Long (or short) to ensure it is updated.


          Last edited by NinjaTrader_ChelseaB; 03-22-2022, 10:56 AM.

          Comment


            #6
            Hi guys me again.

            I have a strategy that uses ExitLongStopMarket and it has been doing fine resubmitting orders on each bar and everything has been going well.

            Today I noticed that, after entry and after the stop was placed, the stop was cancelled on the close of the first bar following entry. At the close of the next bar the stop was resubmitted.

            The log shows that the stop was cancelled (I emailed the log and trace files today).

            I cannot find any reason for this and was curious if this was a common thing you guys see?

            Since the strategy already has a tick data series in BIP 1, I was thinking about moving the resubmission from BIP 0 to BIP 1.

            Alternatively I was considering using IsLiveUntilCancelled, but I want to avoid making this simple situation more complex than it needs to be. If I do go that route it looks like I'll need to capture the order object in OnOrderUpdate and then use CancelOrder somewhere (possibly where I reset variables when flat).

            Thoughts?
            Last edited by WalterSkinner; 03-22-2022, 10:47 AM.

            Comment


              #7
              Hello WalterSkinner,

              If an order that is not using isLiveUntilCancelled as true is not re-submitted on every bar it will automatically be cancelled.

              If the script is unlocked using isLiveUntilCancelled as true and submitting the order once, is more simple that having to re-submit the order.

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

              Comment


                #8
                Hi Chelsea,

                That's the thing, it has been resubmitting stops on every bar with no problems for all other trades. My other trades today were fine aside from the first trade. The first trade did resubmit on each bar after that first post-entry bar saw the stop get cancelled. It was promptly resubmitted on next following bar that canceled the stop (on its close). It has successfully placed hundreds of trades and handled the bar-by-bar replacement on those so the logic has been working fine.

                I do start trailing the stop in different ways which is why it feels like it would be simpler to try to figure out why the stop was cancelled on this one trade. I could be wrong, but it seems like due to the trailing if I used isLiveUntilCancelled I'd need to cancel and then replace on each bar.

                In the situation today where the initial stop was cancelled, this was before any trail logic kicked in.
                A: Trigger bar, stop is placed correctly on execution update.
                B: Stop cancelled at the end of this bar, which does not normally happen on other trades either live or in replay
                C: Stop re-submitted like you would normally expect (in OnBarUpdate)

                Click image for larger version

Name:	EXAMPLE.png
Views:	940
Size:	12.1 KB
ID:	1194840

                I can definitely try to use isLiveUntilCancelled but was also hoping to figure out what is going on here since this base code has served me so well over the past few months without giving me this issue. I have recently changed from volume bars to range bars that's the only change I can think of that might be related to this.

                Comment


                  #9
                  Hello WalterSkinner,

                  To understand the behavior you would need to debug with Print() and TraceOrders.

                  Printing the time of the bar and all values used in the conditions that submit the order would let you know when the conditions are evaluating as true or false.
                  TraceOrders would let you know when NinjaTrader is seeing the order submission and will let you know if the order is submitted, ignored, rejected, or automatically cancelled.

                  Below is a link to a forum post that demonstrates using Print() and TraceOrders to understand behavior.


                  I am happy to assist with analyzing the output from print and trace orders if you would like to save this to a text file and attach this with your next post.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chelsea,

                    Thank you so much for your help!

                    I just ran the trade through market replay and there was no issue.

                    One thing that is worth noting is that the execution was five seconds after the close of the trigger bar (set to enter on bar close).

                    Perhaps there is some sort of connectivity issue with Rithmic?

                    Any ideas what might lead to this since there was no error in market replay?

                    Also, does it make sense for me to move the re-submission of the stop to BIP 1 (tick data series)? Or would that bog it down real time?

                    Comment


                      #11
                      Hello WalterSkinner,

                      If Calculate is OnBarClose, the order would be submitted after the trigger bar closes.

                      Below is a link to a forum post that discusses.


                      I am not seeing any error message in your post.
                      What is the full error message you have received?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChelseaB View Post

                        If Calculate is OnBarClose, the order would be submitted after the trigger bar closes.
                        I know I was saying that the execution took place five seconds after the close rather than immediately like it normally does. Generally five seconds do not need to pass before the entry order is submitted following the close of a bar. I pointed this out just to include more info in case it was helpful in identifying the origin of the canceled stop.

                        I am not seeing any error message in your post.
                        What is the full error message you have received?
                        I did not get an error.

                        Closest I can do to providing an error message is a screenshot of the log.


                        Just to make sure were on the same page here's a quick summary of what happened:

                        - Went long, stop was placed upon execution
                        - First bar after trigger bar closes, and on the close of that bar (8:31:57) the stop was cancelled
                        - Second bar after trigger bar closes, and on the close of that bar (8:32:04) the resubmission put the stop back out there so I was protected again
                        - Unable to replicate this in market replay has me thinking the issue may be related to Rithmic/connectivity or something else that I'm too ignorant to imagine

                        Comment


                          #13
                          Click image for larger version

Name:	log.png
Views:	865
Size:	64.3 KB
ID:	1194877

                          Comment


                            #14
                            Hello WalterSkinner,

                            I would need to see the output from prints and trace orders to provide any direction.

                            If you have an error, I would need the full error message.

                            It would not be necessary to resubmit the order on every tick unless the order is being submitted to the 1 tick series (for example for intra-bar granularity to improve historical fill accuracy).

                            If you want to keep an order alive and this is an unlocked script, use isLiveUntilCancelled as true.
                            EnterLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName)
                            Last edited by NinjaTrader_ChelseaB; 03-22-2022, 03:04 PM.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              I wish I had an error for you.

                              I tried to recreate this situation in market replay so I could debug with print statements but there was no issue in market replay so there is no error to print. The stop was placed normally and was not cancelled.


                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              64 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