Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

{NinjaScript} - NT8 - Cancel Orders (Unmanaged approach)

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

    {NinjaScript} - NT8 - Cancel Orders (Unmanaged approach)

    Dear all, hope you are fine,
    I need to cancel stop orders that haven't been filled with 1 week of expiration.
    My code to create that order is:

    SubmitOrderUnmanaged(0, OrderAction.Sell, OrderTyp e.StopMarket, execution.Order.Filled, 0, execution .Order.AverageFillPrice - StopDistance * TickSize, oco, "StopLossLong");

    How can I cancel that order when isn't filled yet?
    Some sample code please?
    Thank you so much

    #2
    Hello fscabrera03,

    Thank you for your post.

    When using the Unmanaged Approach, you could cancel an order that has not filled by using OnExecutionUpdate(). In OnExecutionUpdate() you would check if the order object is not null and if the order object is equal to execution.Order. Then, check if the execution.Order.OrderState is not OrderState.Filled to check if the order has not been filled. After checking if the order has not been filled, CancelOrder() would be used to cancel the order by using the name of the order object.

    See the attached example script, UnmanagedTemplate, which demonstrates using OnExecutionUpdate() and the Unmanaged Approach.

    Also, see the help guide documentation below for more information about OnExecutionUpdate() and CancelOrder()
    OnExecutionUpdate() - https://ninjatrader.com/support/help...tionupdate.htm
    CancelOrder() - https://ninjatrader.com/support/help...ancelorder.htm

    Let us know if we may assist further.
    Attached Files
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you! I have a new question.
      How can I get an order object regardless the status? Can I access an Order Array or something like that?

      Comment


        #4
        Hello fscabrera03,

        Thank you for your note.

        OnOrderUpdate() could be used to get an order object in your script. You would need to create an order object variable in your script, check in OnOrderUpdate() if the order.Name is equal to the signal name of your entry order and assign the order to your order object variable. Then, you would check if the order object is not null and if the order object is equal to order followed by printing out the order object.

        See the help guide documentation below for an example and more information.
        Order - https://ninjatrader.com/support/help...nt8/?order.htm
        OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          I am trying to switch gears to the unmanaged approach after years of just using managed and I know there is an significant increase of sophistication doing this. I like that BrandonH has created the Unmanaged Template strategy to sort of use as a reference. I was wondering if it was possible for you Brandon to adjust your unmanaged strategy so I could be able to understand how to reference indicators in unmanaged strategies. A simple strategy that would help me understand the concept could be to use a simple crossing of fast EMA crossing above the slow EMA indicator to enter a long position. Its exit target would be when the fast EMA crossed back under the slow EMA. This would provide me with a stepping stone on how to apply other indicators in this fashion. If it a lot of trouble to adjust your strategy no worries.... Thanks... FuturesPhantom

          Comment


            #6
            Hello FuturesPhantom,

            Thanks for your note.

            Indicators could be referenced for crossover conditions in an Unmanaged Approach strategy similar to the crossover conditions made in the SampleMACrossOver strategy that comes with NinjaTrader.

            Please see the attached example script demonstrating how to reference indicators for entry/exit order conditions in an Unmanaged Approach strategy. In the example, we check if we are in a flat position and check if the fast SMA crosses above the slow SMA, and call SubmitOrderUnmanaged to submit our entry order. Then, we check if we are in a long position and if the fast SMA crosses below the slow SMA, and call SubmitOrderUnmanaged for our exit order.

            Let us know if we may assist further.
            Attached Files
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Thank you very much Brandon H. for creating this strategy example. This helped me understand how indicators can be attaching using unmanaged approach. My main reason to migrate to unmanaged was to have more control with the intra-granular trade and higher flexibility entering and exiting orders. I was wondering if it’s possible for you to show an example of how to add a StopMarket order that captures profit order utilizing maybe the BarsSinceEntryExecution method? It would be useful in being able to lock in the profit gained when a long entry position has gone 25 bars significantly into profit and then lock in the profit gained up to the low of the first 5 bars since the long entry was made. If it’s not a major headache to show how it’s possible within the strategy example you provided me I would appreciate it. I have spent quite a few hours trying to get it to work myself to no avail.
              Thanks FuturesPhantom
              Last edited by FuturesPhantom; 07-16-2021, 07:57 AM.

              Comment


                #8
                Hello FuturesPhantom,

                Thanks for your note.

                You would use BarsSinceEntryExecution() in an Unmanaged Approach strategy similar to how you would use it in a Managed Approach strategy. If you would like to check if 25 bars or more have passed since the entry execution and set a stop market order, you would create a condition that checks if BarsSinceEntryExecution() >= 25 and then call SubmitOrderUnmanaged() to place the stop market order.

                Please see the attached strategy which is a modified version of the previously attached strategy.

                Also, see the help guide documentation below for more information.

                BarsSinceEntryExecution(): https://ninjatrader.com/support/help...yexecution.htm
                SubmitOrderUnmanaged(): https://ninjatrader.com/support/help...runmanaged.htm

                Let us know if we may assist further.
                Attached Files
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Brandon H., you do a good job on keeping on top of things here!

                  Comment


                    #10
                    Originally posted by NinjaTrader_BrandonH View Post
                    Hello FuturesPhantom,

                    Thanks for your note.

                    You would use BarsSinceEntryExecution() in an Unmanaged Approach strategy similar to how you would use it in a Managed Approach strategy. If you would like to check if 25 bars or more have passed since the entry execution and set a stop market order, you would create a condition that checks if BarsSinceEntryExecution() >= 25 and then call SubmitOrderUnmanaged() to place the stop market order.

                    Please see the attached strategy which is a modified version of the previously attached strategy.

                    Also, see the help guide documentation below for more information.

                    BarsSinceEntryExecution(): https://ninjatrader.com/support/help...yexecution.htm
                    SubmitOrderUnmanaged(): https://ninjatrader.com/support/help...runmanaged.htm

                    Let us know if we may assist further.
                    Hello BrandonH... can you please suggest how can an OCO order be closed once filled if a certain condition is met, such as reaching session end or any other time of the day.

                    Thanks,

                    Bob Perez

                    Comment


                      #11
                      Originally posted by NinjaTrader_BrandonH View Post
                      Hello FuturesPhantom,

                      Thanks for your note.

                      You would use BarsSinceEntryExecution() in an Unmanaged Approach strategy similar to how you would use it in a Managed Approach strategy. If you would like to check if 25 bars or more have passed since the entry execution and set a stop market order, you would create a condition that checks if BarsSinceEntryExecution() >= 25 and then call SubmitOrderUnmanaged() to place the stop market order.

                      Please see the attached strategy which is a modified version of the previously attached strategy.

                      Also, see the help guide documentation below for more information.

                      BarsSinceEntryExecution(): https://ninjatrader.com/support/help...yexecution.htm
                      SubmitOrderUnmanaged(): https://ninjatrader.com/support/help...runmanaged.htm

                      Let us know if we may assist further.
                      Hello BrandonH... can you please suggest how can an OCO order be closed once filled if a certain condition is met, such as reaching session end or any other time of the day?

                      Thanks,

                      Bob Perez

                      Comment


                        #12
                        Hello bobperez,

                        Thank you for your inquiry.

                        We have an example script available that demonstrates using time filters to limit trading hours:



                        With the unmanaged approach, you can use CancelOrder() to cancel a specified order.





                        Please let us know if you have any further questions.
                        Gaby V.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Gaby View Post
                          Hello bobperez,

                          Thank you for your inquiry.

                          We have an example script available that demonstrates using time filters to limit trading hours:



                          With the unmanaged approach, you can use CancelOrder() to cancel a specified order.





                          Please let us know if you have any further questions.
                          Thank you Gaby,

                          What I understood is that an open (filled) OCO position cannot be closed with CancelOrder(), but rather with an opposite SubmitOrderUnmanaged()​. Am I right?

                          Bob Perez

                          Comment


                            #14
                            Hello Bob,

                            Correct, my apologies I missed that you specified if the order is already filled.

                            If the order is already filled, yes you can instead submit an order in the opposite direction with SubmitOrderUnmanaged().

                            To close an order when using the Unmanaged approach when the order status is Filled or PartFilled, you could check if order.OrderState == OrderState.Filled || order.OrderState == OrderState.PartFilled in OnOrderUpdate followed by calling SubmitOrderUnmanaged to place an order opposite of the entry order.

                            Checking the OrderState in OnOrderUpdate - https://ninjatrader.com/support/help.../nt8/order.htm

                            OnOrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm

                            Please let us know if you have any further questions.
                            Gaby V.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by ccrone2024, Today, 05:13 AM
                            0 responses
                            3 views
                            0 likes
                            Last Post ccrone2024  
                            Started by ttodua, Today, 03:40 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post ttodua
                            by ttodua
                             
                            Started by ttodua, Today, 03:18 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post ttodua
                            by ttodua
                             
                            Started by TraderKnG, Today, 01:30 AM
                            0 responses
                            12 views
                            0 likes
                            Last Post TraderKnG  
                            Started by morrnel, 05-12-2024, 06:07 PM
                            8 responses
                            157 views
                            1 like
                            Last Post nobchi
                            by nobchi
                             
                            Working...
                            X