Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calling Custom Namespaces within Strategy

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

    Calling Custom Namespaces within Strategy

    I've created my own set of indicators delineated within their own namespace. When I plot these indicators on a chart, everything works as expected. When I try to use them within a strategy, the strategy compiles, but then I get an error in the Log tab of NinjaTrader: "Error on calling 'OnStateChange' method: Object reference not set to an instance of an object". How do I use the namespace within OnStateChange?

    This is what I'm doing. Up top, I am including the namespace:
    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;
    using NinjaTrader.NinjaScript.Indicators.MyIndicators;
    #endregion
    Next I declare my indicator instance:
    Code:
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class StrategyTest : Strategy
    {
    private myCustomIndicator indy;
    And here:
    Code:
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    
    // Initialisation snipped out
    
    period = 8;
    
    indy = myCustomIndicator(period);
    
    }
    else if (State == State.Configure)
    {
    }
    }
    This all compiles, but does not work - it errors out.

    What should I do? Am I using the indicator incorrectly in OnStateChange?

    Confession: I've been using NT 7, and have only started using NT 8 recently. This may be a NT 8 nuance that I don't yet understand.

    Thanks!

    #2
    Hello Serac,

    Thank you for the post.

    One problem here is the state where you are creating the indicator, you would need to use State.DataLoaded or later for that.

    I would suggest to use the strategy builder here to just generate the syntax, you can create a new strategy and then use the indicator in any condition then click view code. That will generate the correct syntax and placement for the code.

    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    79 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    63 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X