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 funk10101, Today, 12:02 AM
        0 responses
        2 views
        0 likes
        Last Post funk10101  
        Started by gravdigaz6, Yesterday, 11:40 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by MarianApalaghiei, Yesterday, 10:49 PM
        3 responses
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by XXtrader, Yesterday, 11:30 PM
        0 responses
        4 views
        0 likes
        Last Post XXtrader  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        28 views
        0 likes
        Last Post love2code2trade  
        Working...
        X