Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need Help with secondary classes

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

    Need Help with secondary classes

    Hi ninja Trader,

    I am trying to develop a custom strategy that has many different working pieces...and I want to separate these pieces into their own classes so it does not interefere with these pieces for when I change or alter the strategy in any way, but I am having trouble declaring and initializing the variables and data types. I compile without any errors, but then when i run the strategy it says (Calculating...) at the top and doesn't display anything. It works great when its all in one class ("MyStrategy" class for example), but when I separate out objects like moving average, support, resistance, supply, demand classes, things get complicated really quickly.

    To give you an idea of an example, let's say I want to create a multi-timeframe strategy with a moving average class: (I am typing this from memory to give you an idea, so some syntax may not be completely accurate, but I want you to get the idea of what I am trying to achieve)

    namespace NinjaTrader.NinjaScript.Strategy
    {
    //declare a new method where everything related to moving average will be stored
    public class MovAvg : IDisposable
    {
    //declare constructor to instantiate objects within the MyStrategy class
    public MovAvg(){}

    //declare variables to use within MyStrategy class
    public SMA mySMA;
    public Brush mySMABrush = Brushes.Blue;

    //methods
    //method to set Values of the plot equal to the value of the SMA variable.
    public void ExecuteMovAvg()
    {
    Values[1][0] = mySMA;
    }
    }

    public class MyStrategy : Strategy
    {
    //onstatechange
    if(State == State.SetDefaults)
    {
    AddPlot(new Plot(PlotStyleType.Line, "MyMovAvg"); //Values[1][0]

    }

    if(State == State.DataLoaded)
    {
    MovAvg m = MovAvg(); //instantiate the movingAverage object
    m.mySMA = SMA(BarsArray[1], 20); //initialize the SMA variable from the moving average class and set to the SMA(BarsArray[1], 20) moving average.
    m.Dispose();
    }

    //onBarUpdate
    //section Moving Average
    if(BarsInProgress == 1)
    {
    MovAvg m = new MovAvg();
    m.ExecuteMovAvg(); //set the plot to the values of the SMA variable to plot the line on the chart
    m.Dispose();
    }
    //properties
    }
    }

    I also have Lists that I create to hold the objects and properties for each class. So for example, I have a Support and Resistance Class where I create a List<Support> SupportListName and a List<Resistance> ResistanceListName that I add to and alter throughout the strategy that I use to create plots, zones, and lines from.

    Is this possible to do, or am I going about this the wrong way? Again, I don't get any compile-time errors, only run-time errors where I cannot see any of the plots or lines appear, only a (Calculating...) at the top of the screen appears.

    Thanks so much for any insight you may give me.
    --Jake--

    #2
    You might try putting all your class definitions inside your MyStrategy class.

    I mean, how does ExecuteMovAvg() know anything about Values unless
    the MovAvg class is moved inside of your MyStrategy class?

    Just my 2˘.

    Last edited by bltdavid; 07-27-2023, 09:15 AM.

    Comment


      #3
      Hello Jake,

      Thanks for your post.

      You would not be able to make a class and use normal NinjaScript code within that class. This is just a C# class and not a strategy or indicator class.

      In the class you shared, you are assigning an indicator directly to the plot which is not correct. You would need an indicator value being assigned to the plot but that plot is in the strategy, not in your custom class.

      We always recommend using custom classes for only C# logic. If you need NinjaScript values or properties, they should be passed in with the method overloads.

      Further, you do not need to make that object a disposable object. C# has a garbage collector for classes that don't require disposing of native resources. This means calling .Dispose() on that class would not do anything since you did not implement the Dispose override and there is nothing to dispose of.

      Since the code you shared in those classes is specifically NinjaScript code, you should add that logic inside the public class MyStrategy : Strategy as it would be placed normally instead of putting NinjaScript code inside custom C# classes.
      <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


        #4
        Thank you bltDavid and Brandon for your insights. The advice you give is very valuable to me and I will make the changes you recommend! Thanks again and have a wonderful afternoon!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        62 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        134 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X