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 judysamnt7, 03-13-2023, 09:11 AM
    4 responses
    59 views
    0 likes
    Last Post DynamicTest  
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    36 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    7 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    21 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    10 views
    0 likes
    Last Post cre8able  
    Working...
    X