Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Event for order submission and canceling

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

    Event for order submission and canceling

    Is there an event I can attach to find out if an order is being submitted?

    I would imagine it would have to be part of the Ninjatrader.Cbi.Account object?

    Also is there a property with the account to disable all traders for an account or do I have to listen for an order to be place and then cancel it right away?



    I see the functionality of the "AccountData:OrderGrid" and it is what I am trying to make.
    Is there any place I can find the source code for that UI object.

    #2
    Hello archieoi, thanks for your question.

    We have the OnOrderUpdate event method that you can subscribe to:
    https://ninjatrader.com/support/help...rderupdate.htm

    There is an example use case here as well:
    https://ninjatrader.com/support/help...and_onexec.htm

    To stop a strategy from trading you would program that into the strategy yourself. We have a couple of examples on doing this here:

    https://ninjatrader.com/support/help...nce_user_d.htm
    https://ninjatrader.com/support/help...to_limit_t.htm

    To access data from the grid itself is not supported, unfortunately. The source code is not available for this module.

    Please let me know if you have any questions on this material.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks this is great thanks for the help!

      There is nothing like canceling all orders for a catch-all that will shut down any open position and close all orders?

      I am trying to make something that will prevent all orders from being submitted or being automatically canceled once submitted once some criteria will be met.

      Comment


        #4
        Hello archieoi, thanks for your reply.

        We have the CloseStrategy() method which will cancel all working orders, closes any existing positions, and disable the strategy.

        If you want to restart the strategy, it would need to be enabled again.

        Best regards.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          After trying to implement and reviewing "OnOrderUpdate" I see its an override for object subclassed from Strategy.
          Being that I am trying to make an AddOn how do I get access to the class or method.

          There is nothing under AddOn object subclasses from NTWindow?

          There is no Event that I can attach to NinjaTrader Controls, I see in the AccountSelector object there is a way to get "accountSelector.SelectedAccount.OrderUpdate", is this what I need?

          or I see (I tried to past the URL, but the website navigation seems off) this?
          Navigation: NinjaScript > Language Reference > Add On > Account >OrderUpdate

          or do I override OnStateChange() to see if any change has occurred to the Account and see if something has changed under myAccount.Strategies?
          Last edited by archieoi; 09-09-2020, 07:50 PM.

          Comment


            #6
            Hello archieoi, thanks for your reply.

            You can make an Account object in your addon and subscribe to the OrderUpdate hook. That will function in the same way the Strategy OnOrderUpdate event works.

            Kind regards,
            -ChrisL
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              I was able to use the OnOrderUpdate event to cancel a submitted order via the method of OnOrderUpdate part of the Account object.
              Code:
              private void OnOrderUpdate(object sender, OrderEventArgs e)
              {
                Dispatcher.InvokeAsync(() => {
                  accountSelector.SelectedAccount.CancelAllOrders(e.Order.Instrument);
                });
              
              }
              Using this code I was able to cancel any order coming in. But this caused a few issues with the account.
              When selecting a "Buy" or "Sell" button in the Chart Trader and once it is canceled the opposite order is disabled.
              Also if I was to submit an order via the chart and after it gets canceled, but still sticks around as a gold color order.

              More over all the orders are still present in the Orders tab of the Control Center. The cancel button is still active but not clickable and the order state is set as Cancel pending; also when closing or disconnecting NinjaTrader it claims the orders are still active.

              Is there a way to fix this and what is the proper way to close order in code once it is submitted but before it is filled? As suggested before the CloseStrategy() option will not work because this code is via an AddOn and not a Strategy.

              Click image for larger version  Name:	OrderIssues.PNG Views:	0 Size:	6.8 KB ID:	1130734
              Click image for larger version  Name:	ActiveOrders.PNG Views:	0 Size:	6.5 KB ID:	1130735
              Click image for larger version  Name:	Pending.PNG Views:	0 Size:	3.7 KB ID:	1130736
              Last edited by archieoi; 12-04-2020, 02:52 AM.

              Comment


                #8
                Hello archieoi, thanks for the follow up.

                I would need a reduced example of how this snippet you posted is used to know what the specific problem is using CancelAllOrders. Consider keeping a List<Order> to keep track of every order in the account, then you can loop through that list and cancel all orders that are in OrderState.Accepted or OrderState.Working.

                MyOrder.Account.Cancel(new[] {MyOrder}); //Cancels a single order.

                I look forward to hearing from you.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks again for all the help, I am so close to what I am developing

                  The code snippet is part of an AddOn that once some conditions are met, all future orders placed by NT will be automatically canceled.

                  By using OnOrderUpdate I am listening for any orders being placed and will cancel them as soon as they come in with the line

                  Code:
                  accountSelector.SelectedAccount.CancelAllOrders(e. Order.Instrument);
                  This cancels the order but sets the order in a half canceled state of "Cancel Pending"

                  Checking a bit later, it seems all the orders are still in there and have not been fully canceled by GTC. Hope it fixes by tomorrow.
                  Is there some property I need to change to fully close them after calling CancelAllOrders?

                  I will try the MyOrder.Account.Cance, I guess I can access all orders from Account.Orders

                  Click image for larger version  Name:	WorkingOrders.PNG Views:	0 Size:	27.8 KB ID:	1130861

                  Comment


                    #10
                    As a follow up I was able to finally fully cancel out the "Cancel pending" by forcing the order back to OrderState.Working and then using the Cancel method.

                    Also "Cancel pending" orders never seem to leave the Control Center's Orders tab even the next day.

                    I also tried to set the OrderState of the order to Canceled but it still stayed as "Cancel pending" in the Control Center's Orders tab.

                    I finally canceling the orders this clears out the Orders tab after a day.



                    Code:
                    foreach (Order order in accountSelector.SelectedAccount.Orders)
                    {
                    
                      order.OrderState = OrderState.Working;
                      order.Account.Cancel(new[] { order });
                      //accountSelector.SelectedAccount.Cancel(new[] { order }); // Unsure if this call is the same as above
                    }
                    Click image for larger version  Name:	Cancledorders.PNG Views:	0 Size:	29.4 KB ID:	1130910

                    Comment


                      #11
                      Hello archieoi, thanks for your reply.

                      A test script would be needed for me to investigate the proposed issue. If you could provide a test script that reproduces the "Cancel Pending" problem I will be happy to test it on my end. If I can consistently reproduce this Cancel Pending issue it would be unexpected and something support would need to look into.

                      Kind regards,
                      -ChrisL
                      Chris L.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by PaulMohn, Today, 05:00 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post PaulMohn  
                      Started by ZenCortexAuCost, Today, 04:24 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post ZenCortexAuCost  
                      Started by ZenCortexAuCost, Today, 04:22 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post ZenCortexAuCost  
                      Started by SantoshXX, Today, 03:09 AM
                      0 responses
                      16 views
                      0 likes
                      Last Post SantoshXX  
                      Started by DanielTynera, Today, 01:14 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post DanielTynera  
                      Working...
                      X