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

Cancel GTCs When Strategy is Canceled

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

    Cancel GTCs When Strategy is Canceled

    Hello,

    Is there a way to pull orders when a running strategy file is removed? I am already using CancelOrder() for certain events, just am unsure how to have a strategy pull orders upon being deleted.

    Thank you!

    Last edited by ozziyhtomit; 11-03-2020, 10:32 AM.

    #2
    Hello ozziyhtomit,

    Thank you for your post.

    You could certainly tell the strategy to exit any open positions when it reaches State.Terminated when the strategy is disabled. Here's a very simple example:

    Code:
     protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleMACrossOver;
    Name = "ExampleCloseStrategy";
    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;
    }
    else if (State == State.DataLoaded)
    {
    
    }
    else if(State == State.Terminated)
    {
    if(Position.MarketPosition == MarketPosition.Long)
    ExitLong();
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (State == State.Historical)
    return;
    if (CurrentBar < BarsRequiredToTrade)
    return;
    
    if(Close[0] > Open[0])
    EnterLong();
    
    }
    This would enter a trade on the first up bar in real time. It will only exit the trade when the strategy is disabled and reaches State.Terminated.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Actually I'm talking about deleting resting UNFILLED orders. But this looks like a solution for exiting positions AND pulling resting orders as well, yes?

      Thanks

      Comment


        #4
        Hello ozziyhtomit,

        Thank you for your reply.

        Yes, you should be able to call CancelOrder() in State.Terminated as well to cancel any working orders.

        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 George21, Today, 10:07 AM
        0 responses
        4 views
        0 likes
        Last Post George21  
        Started by Stanfillirenfro, Today, 07:23 AM
        8 responses
        23 views
        0 likes
        Last Post Stanfillirenfro  
        Started by DayTradingDEMON, Today, 09:28 AM
        2 responses
        16 views
        0 likes
        Last Post DayTradingDEMON  
        Started by navyguy06, Today, 09:28 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by cmtjoancolmenero, Yesterday, 03:58 PM
        8 responses
        32 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X