Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel stop limit order it is not executed during the following 4No. bars

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

    Cancel stop limit order it is not executed during the following 4No. bars

    Hi,

    I am developing a strategy within the Strategy Builder wizard which will place a stop limit order 1 tick above the high of the signal bar (i.e the bar when a signal is received from a set of conditions).

    I would like to cancel the stop limit order if it is not executed during the following 4No. bars (i.e the price does not reach the stop limit order value).

    I am using the Strategy Builder wizard and I can't see any obvious way of adding logic once the stop limit order is made.

    Any help is appreciated, thanks.

    Neil

    #2
    Hello burtoninlondon,

    Thanks for your post and welcome to the NinjaTrader forums!

    There are two issues to overcome, one is to maintain the order and the other is to cancel after 4 bars (if I understand correctly)

    To maintain the order: In a Strategy Builder strategy, when you place a limit type of order, if the limit order is not filled on the bar that it is submitted, the order is automatically canceled. In order to keep the limit type order in place what you can do is to create your entry conditions and when true have that set save the limit order price into a double variable. In another set, you would check to see that the market position is flat and that the double variable is greater than 0. Once the order fills, the market position changes, and the limit order is no longer submitted and to prevent it from being submitted when you next go flat, when you are in a position reset the double variable back to 0. Note: You would also want to add a check to your entry set to check for a flat market position as that will prevent the double variable from being changed once you are in position. Here is a link to check for the market position: https://ninjatrader.com/support/help...ionComparisons

    So now we have the limit type order in place and as is it would maintain that until filled however if I understand correctly you want to cancel it after 4 bars if not filled. Now we need to create a counter so that we know when 4 bars have passed to have a condition that no longer resubmits the limit type order. Regrettably, this is not easily done but I can provide an example that shows you how to create and work with a counter. Please see this post: https://ninjatrader.com/support/foru...on#post1145605

    To use the counter with your strategy, you would need to create a bool variable that is true by default and then use that bool (check for = true) in the set that maintains the limit order. When the bar counter has reached 4, set the bool to false causing the limit order to auto cancel. The counter would need to be started by your entry set that saves the limit price (and also reset the bool back to true.

    I would suggest download and testing the example strategy so that you can understand set by set what is happening and then copy what you need to your strategy (or start new).






    Comment


      #3
      Hi Paul, thanks for your reply. I've managed to figure out the bar counter and incorporate that into my strategy but I'm not 100% clear on the logic for maintaining the order. Is the following a correct interpretation of your logic?

      Variables:
      LimitOrderPrice = type double with 0 as default value


      Conditions and Actions:
      Set 1:
      IF ALL
      entry conditions are met
      DO
      Enter long with a stop market order
      LimitOrderPrice = stop market order value

      Set 2:
      IF ALL
      Position.MarketPosition = MarketPosition.Flat
      LimitOrderPrice >0
      DO
      I haven't added any logic here


      I've tested this within Strategy Builder and it works for as long as the entry conditions are valid however as soon as the entry conditions are no longer valid then the stop market order gets cancelled at the close of the current bar. I need the stop market order to remain in place even if the entry conditions are no longer valid.

      I then added a new bool variable OrderPlaced which was set to FALSE by default, and would be set to TRUE when the order is placed to try to maintain the order when the entry conditions changed.

      Variables:
      LimitOrderPrice = type double with 0 as default value
      OrderPlaced = type bool with FALSE as default value


      Conditions and Actions:
      Set 1:
      IF ALL
      entry conditions are met
      DO
      Enter long with a stop market order
      LimitOrderPrice = stop market order value
      OrderPlaced = TRUE
      Set 2:
      IF ALL
      Position.MarketPosition = MarketPosition.Flat
      LimitOrderPrice >0
      OrderPlaced = TRUE
      DO
      Enter long with a stop market order at value = LimitOrderPrice

      If the order is filled then should I add the following logic?
      Set 3:
      IF ALL
      Position.MarketPosition != MarketPosition.Flat
      DO
      LimitOrderPrice = 0
      OrderPlaced = FALSE

      If I manually close the order then I'm a bit lost as to what I should do.

      I understand if I was manually coding this then I could use the IsLiveUntilCancelled variable but I have limited coding experience so want to complete as much of the logic in Strategy Builder.

      Any help would be appreciated.

      thanks,

      Neil
      Last edited by burtoninlondon; 08-25-2021, 09:40 AM.

      Comment


        #4
        Hello Neil,

        Thanks for your reply.

        The entry order is not placed until (your) set #2.

        Using your clever descriptors I'll try to clarify:

        Set 1

        IF ALL entry condition are met.

        DO : save the stop order price into a double type variable LimitOrderPrice = stop market order value, set OrderPlaced = true

        Set 2

        IF ALL:
        Position.MarketPosition = MarketPosition.Flat
        LimitOrderPrice >0
        OrderPlaced = true

        Do

        EnterLongStopMarket using LimitOrderPrice

        Comment


          #5
          Hi Paul,

          Thanks for your help - that worked in the Strategy Builder.

          I have had to unlock the code so I can modify it in the editor as I need to add logic which the Strategy Builder cannot accommodate therefore I was wondering if there was a more efficient way to implement the logic above in the editor now that I am not bound by the constraints of the Strategy Builder?

          Or would this be the logic if you were manually coding in the editor? If not, are you able to able to point me in the right direction of a simpler way to maintain the stop market order for 4No. bars if it is not filled.

          Thanks,

          Neil

          Comment


            #6
            Hello burtoninlondon,

            Thanks for your post.

            You can obviously continue to do the same in unlocked code and my recommendation would be to stay with what works.

            Alternately you can submit an order with the order parameter IsLiveUntilCanceled set to true and then you can cancel the order when x bars have passed.
            Here are references you will need to be familiar with:
            https://ninjatrader.com/support/help...er_methods.htm (review each order to see the method overload that offers the IsLive bool)
            To cancel orders you will need a bit more work as shown in the example here: https://ninjatrader.com/support/help...ancelorder.htm

            Here is a link to a reference strategy https://ninjatrader.com/support/help...thod_to_ca.htm

            Comment


              #7
              burtoninlondon May I request you post a sample of the finished "Cancel Stop Limit" with these steps. I do not need your primary logic if it is secretive but would appreciate these steps to ensure I have it right. Thank you for asking this question. It just came up in my Bot planning today and was glad to find it already answered.

              Comment


                #8
                JMont1 - I didn't end up changing my code to use the IsLiveUntilCancelled order parameter and I kept with the original logic poster earlier in this thread.

                I've attached the strategy which you can open up in Strategy Builder, it is based on a simple SMA crossover which enters a short stop order at a price 1 tick below the low of the previous bar.

                The stop order will remain active for as many bars as you define in the "BarsToCancel" variable (albeit due to the way the counter works it counts 1 more than the number you set).

                Hope this helps.
                Attached Files

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                55 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                132 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                73 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
                49 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X