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

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.
    Kate W.NinjaTrader Customer Service

    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.
        Kate W.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by jpeep, 08-16-2020, 08:31 AM
        13 responses
        487 views
        0 likes
        Last Post notenufftime  
        Started by realblubb, 04-28-2024, 09:28 AM
        2 responses
        32 views
        0 likes
        Last Post realblubb  
        Started by bmarovets, Today, 06:33 PM
        0 responses
        11 views
        0 likes
        Last Post bmarovets  
        Started by ETFVoyageur, Yesterday, 07:05 PM
        4 responses
        39 views
        0 likes
        Last Post ETFVoyageur  
        Started by JGriff5646, Yesterday, 10:02 PM
        2 responses
        24 views
        0 likes
        Last Post JGriff5646  
        Working...
        X