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 NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        63 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        139 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
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X