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

Rescue strategy to cancel all pending orders from my account

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

    Rescue strategy to cancel all pending orders from my account

    In some situations, I need to cancel all my pending orders from my account. I made a small scrips as follows. But this scrip is quite dangerous because this script can be triggered for any other strategy being called. I have to remember to comment out after using it. Is there any other method to safely cancel all pending orders? Thanks a lot.

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    ...
    lock (Account.All)
    myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");

    foreach (Order order in myAccount.Orders)
    {
    if (order.OrderState != OrderState.Cancelled)
    {
    myAccount.CancelAllOrders(order.Instrument);
    }
    }


    #2
    Hello localappleseed,

    I would highly not recommend that this code be in State.SetDefaults. The defaults are called when the Strategies window is opened. Every time it's opened. This would cancel all working orders everytime the Strategies window is opened when the defaults are pulled.

    A more effective place would be State.DataLoaded, which runs once in OnStateChange() as the strategy has finished loading data and is about to start running OnBarUpdate(), or State.Historical as historical data is about to begin processing, or even State.Realtime just before real-time data begins processing.

    Please see the help guide on OnStateChange() and the definitions of each State.
    https://ninjatrader.com/support/help...tatechange.htm

    That said, what do you mean by "safe"? Do you mean the code would not cancel working orders on the account?

    This would be the only code to place orders to an account outside of a NinjaScript Strategy. I am not aware of any other approach.
    Last edited by NinjaTrader_ChelseaB; 11-15-2020, 07:26 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by andrewtrades, Today, 04:57 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by chbruno, Today, 04:10 PM
    0 responses
    3 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Started by josh18955, 03-25-2023, 11:16 AM
    6 responses
    436 views
    0 likes
    Last Post Delerium  
    Started by FAQtrader, Today, 03:35 PM
    0 responses
    7 views
    0 likes
    Last Post FAQtrader  
    Started by rocketman7, Today, 09:41 AM
    5 responses
    19 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X