Announcement

Collapse
No announcement yet.

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:	74
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.

    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.

        Comment


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

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          66 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          141 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          46 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          51 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X