Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Basic strategy script terminated

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

    Basic strategy script terminated

    Why is the output of this

    SetDefaults
    SetDefaults
    Configure
    Terminated
    Terminated

    OnBarUpdate() never gets called.

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MyCustomStrategy : Strategy
    {
    
    protected override void OnStateChange()
    {
    Print(State);
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MyCustomStrategy";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    Print(BarsInProgress + " " + CurrentBars[0] + " " + CurrentBars[1] + " < " + (BarsRequiredToTrade / 5f) + " " + CurrentBars[2] + " < " + (BarsRequiredToTrade / 30f));
    
    if (BarsInProgress != 0)
    return;
    
    
    }
    }
    }

    #2
    Hello clabough,

    Thanks for your post.

    There are essentially 3 instances that generate throughout the process for 3 different situations. All 3 of these cases run its termination throughout its lifetime:
    1. The earlier instance used to create the list of indicators by name
      Terminated when you close the indicators window
    2. The selected instance which pushes its default settings the UI
      Terminated when you "remove" the indicator from the configured menu, or after the user press OK/Cancels the indicators window
    3. The executed instance which runs against the user-configured settings.
      Terminated when the user performs some action which could cause the indicator to remove/reload (or it hits a fatal error
    Although, the termination logic executed during these 3 cases should not interfere with another, as long as your resources were properly set up after the state was configured. The first two instances never go beyond State.Defaults and will then be terminated.

    You could see in the example below that State.SetDefaults and State.Terminated occur multiple times when adding an indicator and then canceling the indicator dialog.

    Adding Indicator, Cancel Indicator Dialog
    Right click on Chart -> Select Indicators
    Output-> TestStates{d83a7d55-8ff3-4496-b1a1-7a5cbcc95c1e} State=SetDefaults
    Add "TestStates" indicator
    Output-> TestStates{e633ba70-4341-428b-b622-cace6a7c9a01} State=SetDefaults
    Press Cancel
    Output-> TestStates{d83a7d55-8ff3-4496-b1a1-7a5cbcc95c1e} State=Terminated
    Output-> TestStates{e633ba70-4341-428b-b622-cace6a7c9a01} State=Terminated


    Please see this forum post by my colleague Chelsea which details why you may see State.Terminated occur multiple times: https://ninjatrader.com/support/foru...ded#post726288

    Also, see this help guide documentation for a visual representation of how each instance goes through its States: https://ninjatrader.com/support/help...fecycle_of.htm

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    89 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    135 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
    119 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    69 views
    0 likes
    Last Post PaulMohn  
    Working...
    X