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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    75 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    146 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    79 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    50 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    54 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X