Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to cancel orders and positions of a certain strategy when disabling the Strategy

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

    How to cancel orders and positions of a certain strategy when disabling the Strategy

    How to cancel orders and positions of a certain strategy when disabling the strategy?

    Account.FlattenEverything() or Account.Flatten() i couldn't do it. I don't want the whole account, just the one instance that (State == State.Terminated) was called.

    Help me please?

    #2
    Hello janiodesouza,

    Thank you for your post.

    You could set up a bool that gets set to true once the strategy has gotten to State.DataLoaded that will tell you that you're running the strategy on data, not just setting it up, and check for that bool when the strategy reaches State.Terminated (since it can do so more than once), and if that bool is true, then check the position you're in and submit Market orders to exit the open position.

    Code:
     bool IsDataLoaded;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Test strategy exiting positions on disable";
    Name = "TestDisableStrategy";
    Fast = 10;
    Slow = 25;
    // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = false;
    IsDataLoaded = false;
    }
    else if (State == State.DataLoaded)
    {
    IsDataLoaded = true;
    }
    else if (State == State.Terminated)
    {
    if(IsDataLoaded)
    {
    if(Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong();
    }
    if(Position.MarketPosition == MarketPosition.Short)
    {
    ExitShort();
    }
    }
    }
    
    }
    Please let us know if we may be of further assistance to you.

    Comment


      #3
      OK it worked for positions and orders that are not: Profit target and Stop loss. These remained active.

      And now, how do I delete OCO Profit target and Stop loss orders?

      Comment


        #4
        Hello janiodesouza,

        Thank you for your reply.

        You can make certain working orders will be cancelled on a disable with settings in the Tools > Options > Strategies menu. Under the NinjaScript heading, ensure that both "Cancel entry orders when a strategy is disabled" and also "Cancel Exit orders when a strategy is disabled" are checked.

        Please let us know if we may be of further assistance to you.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        47 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        23 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        33 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        50 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        42 views
        0 likes
        Last Post CarlTrading  
        Working...
        X