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 Mindset, 04-21-2026, 06:46 AM
        0 responses
        90 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        137 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        68 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        120 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        71 views
        0 likes
        Last Post PaulMohn  
        Working...
        X