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

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by cocoescala, 10-12-2018, 11:02 PM
                  7 responses
                  939 views
                  0 likes
                  Last Post Jquiroz1975  
                  Started by oviejo, Today, 12:28 AM
                  1 response
                  11 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by geddyisodin, Yesterday, 05:20 AM
                  7 responses
                  46 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by gbourque, Today, 06:39 AM
                  2 responses
                  5 views
                  0 likes
                  Last Post gbourque  
                  Started by cre8able, Yesterday, 07:24 PM
                  1 response
                  13 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X