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

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.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Javierw.ok, Today, 04:12 PM
    0 responses
    2 views
    0 likes
    Last Post Javierw.ok  
    Started by timmbbo, Today, 08:59 AM
    2 responses
    10 views
    0 likes
    Last Post bltdavid  
    Started by alifarahani, Today, 09:40 AM
    6 responses
    40 views
    0 likes
    Last Post alifarahani  
    Started by Waxavi, Today, 02:10 AM
    1 response
    18 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by Kaledus, Today, 01:29 PM
    5 responses
    15 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X