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

Cancel orders in only 1 strategy

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

    Cancel orders in only 1 strategy

    Dear team,
    I hope you are doing well.

    I have been successfully using the piece of code below OnBarUpdate to cancel orders in 1 strategy (let's call it Strat1) that I am using:

    Click image for larger version

Name:	image.png
Views:	32
Size:	7.3 KB
ID:	1292329

    However, I realized that if I have more than one active strategy in the same account (for example Strat1 and Strat2), the code above will cancel all the open orders in Strat1 and Strat2.

    I was wondering if there is a condition that i can add to just filter the orders in Account.Orders that are only related to Strat1?

    Thank you in advance for your guidance!
    Best,​

    #2
    Hello linkcou,

    Thank you for your post.

    Is this code being called within Strat1, or are you calling it in a different script? Have you tested this on a simulated account and observed it canceling orders from both strategies? What are the results if you print the order information, such as the order.Name, rather than calling CancelOrder(order) - do you see order names from both strategies or only from the script that is generating the prints?
    For example:
    Code:
                foreach (Order order in Account.Orders)    
                    if (order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted || order.OrderState == OrderState.CancelPending)
                    {
                        Print("Order name: " + order.Name + "Order state: " + order.OrderState);
                        // CancelOrder(order);
                    }​
    Once I have more clarification from you I will be able to assist more accurately and provide relevant resources.

    I look forward to your reply.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily,

      Thanks for the quick reply. Sharing below answers to your questions:
      - Yes, it's only called in Strat1.
      - It's being tested in a simulated account.

      I can try to do the printing tomorrow to find an example. In the meantime, is the strategy name tracked somewhere in the information of the order or accounts?

      I would like to add an IF condition after the foreach to only get the orders that have been generated from Strat1 name.

      Thanks!

      Comment


        #4
        Hello linkcou,

        Thank you for your reply.

        There is no way to tell which orders belong to a specific strategy or not. You could add some sort of identifier into the signalName of an order to tell the difference between which strategy submitted the order if you'd like.
        The CancelOrder() method is only able to cancel orders that were generated by that same strategy instance that is calling CancelOrder(). It would not be able to cancel orders generated from other strategy instances. For example, if you call CancelOrder() in Strat1, it can only cancel orders that were generated by Strat1. Any orders from Strat2 would not be able to be canceled by Strat1's logic.
        When it comes to using the CancelOrder() method, even if you'd like to cancel all orders from the strategy, it is best to work with Order object variables to keep a reference to each order object. Then, you could add a check for each order object you are managing in Strat1 to see if the object != null as well as check the state, and cancel that order as needed. We have a reference sample demonstrating the use of CancelOrder() here:


        That example also assigns orders to Order object variables. This is one of the Advanced Order Handling concepts:


        Other samples also manage Order object variables, such as the following:


        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank you, Emily!! i think this is already what I need it.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Haiasi, 04-25-2024, 06:53 PM
          2 responses
          17 views
          0 likes
          Last Post Massinisa  
          Started by Creamers, Today, 05:32 AM
          0 responses
          5 views
          0 likes
          Last Post Creamers  
          Started by Segwin, 05-07-2018, 02:15 PM
          12 responses
          1,786 views
          0 likes
          Last Post Leafcutter  
          Started by poplagelu, Today, 05:00 AM
          0 responses
          3 views
          0 likes
          Last Post poplagelu  
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,407 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Working...
          X