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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by AH369, Yesterday, 05:37 AM
    3 responses
    17 views
    0 likes
    Last Post NinjaTrader_RyanS  
    Started by NinjaTrader_BrandonH, Today, 09:51 AM
    0 responses
    2 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by caveat_lector, Today, 08:55 AM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Kate  
    Started by aligator, Today, 09:02 AM
    2 responses
    6 views
    0 likes
    Last Post aligator  
    Started by memonolog, Today, 08:32 AM
    3 responses
    13 views
    0 likes
    Last Post NinjaTrader_PaulH  
    Working...
    X