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

Access Custom Methods from another strategy

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

    Access Custom Methods from another strategy

    Hi!

    I have custom methods in a strategy and want to use them also in other strategies. I don't want to duplicate all the code for every strategy.

    For testing reasons I just tried to access a custom method DoPrint() in my MyMethods Strategy which only prints the actual Time.

    Here is the code of my MyMethods Stragtegy:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MyMethods : Strategy
    {
    public void DoPrint()
    Print(String.Format("{0}", Time[0]));
    }
    }
    I want to use the DoPrint Method in my Test Strategy as follows:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class TestStrategy : Strategy
    {
    MyMethods mm = new MyMethods();
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 20)
    return;
    mm.DoPrint();
    }
    }
    }​
    I am getting "Object reference not set to an instance of an object" when running my TestStrategy. What am I doing wrong?

    Thanks!

    ​​
    Last edited by KirkHammett; 09-16-2022, 10:59 AM.

    #2
    Hello KirkHammett

    What you have shown would not be the way to make custom methods for use by multiple scripts. Each of your classes inherit from Strategy therefore they are both individual strategies that cannot communicate.

    If you wanted to make a custom class of methods you would need to use a standard C# class for that:
    Code:
    public class MyMethods
    {
    
    }
    In the strategy that uses that class it would need to create an instance of that class:

    Code:
    MyMethods myMethods = new MyMethods();
    Keep in mind the custom class is not a NinjaScript object so you can't use items like Close[0] or Print inside that class, you would have to pass anything you needed to the method inside that class.

    Code:
    public class MyMethods
    {
       public void DoPrint(Strategy myStrategy)
       {
    
          myStrategy.Print(String.Format("{0}", myStrategy.Time[0]));
       }​​
    }​
    Please note that inheriting from strategy on your custom class won't automatically give it access to items like Print or Time[0] even if you create an instance as shown above, that is not a valid approach. Using a standard C# class and passing in what you need is the correct approach.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you Jesse! Is there a simple way to use Ninjascript code like Close[0] in my custom class or do I have to pass it to the method (eg. myStrategy.Close[0]) every time I use it in my custom class? That would be quite burdensome.

      Comment


        #4
        Hello KirkHammett,

        You can make a partial class in another file but that would only be good for use on your local machine. If you plan to export the code you should create a class and use it like above.

        In the strategies folder create a new strategy and delete all the code besides the using statements.

        Code:
        public partial class Strategy
        {
        public void DoPrint()
        {
        
        Print(String.Format("{0}", Time[0]));
        }​​​
        }
        You can read about partial classes here:

        https://ninjatrader.com/support/help...ightsub=partia l+class
        Last edited by NinjaTrader_Jesse; 09-16-2022, 02:11 PM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Good reading here.

          Comment


            #6
            Hi Jesse!

            It doesn't work with the partial class. I did exactly what you say but I get an error that Print and Time does not exist in this context. When I inherit from Strategy I have the same problem as in my first post.

            I also tried the partial class example in the link you provided. There I am also getting an error. When I declare the void method as static it requires an object reference so the example doesn't work.

            Can you provide an example how to use the partial class?

            Thanks for your help!
            Last edited by KirkHammett; 09-17-2022, 01:15 AM.

            Comment


              #7
              Originally posted by bltdavid View Post
              Good reading here.
              Thanks a lot! This works really as I imagined. I am just unsure if I should use it because it is unspported. Did you face any problems with using abstract classes?

              Comment


                #8
                Unsupported does not mean 'this does not work'.

                It usually means NinjaTrader support 'will not comment on it' or
                'provide any code examples' or 'provide any documentation' or
                'help you figure it out'.

                However...
                There are exceptions to this. Many times I have seen NT support
                provide special code solutions to complex questions with the claim
                the code works but it is 'not supported'. In these cases, that means
                'this code example may or may not break in the next release. But,
                if it does break, we are under no obligation to fix it', which is in addition
                to the other secret meanings.

                In summary...
                'Not supported' just means 'use at your own risk' and 'you better know
                what you're doing'.

                -=o=-

                The ONLY issue I have found with an abstract base class is the internal
                NinjaTrader C# compiler does not recognize the 'abstract' keyword and
                will auto generate the magic code at the bottom of an indicator's source
                file -- despite the fact such magic code is never needed for a class that
                is defined as abstract.

                Fortunately, this stupidity only affects abstract classes for indicators,
                you shouldn't have a problem with strategies or addons. (Only indicators
                have auto generated magic code).

                My point is, the C# language supports the use of an abstract base class
                just fine. The only issues are caused by NinjaTrader's own design decisions.
                They're tripping over their own feet, creating their own bugs, they make
                negative comments on this forum in an effort to hobble and undermine any
                superior NinjaScript development technique that makes use of an abstract
                base class.

                So, is there a problem with using abstract base classes?

                Not really.

                ​Any issues NinjaTrader wants to 'warn' you about are issues of their
                own making. That is, NinjaTrader could update their C# compiler to
                recognize the 'abstract' keyword and silence the magic code for such
                classes -- why they haven't done that is difficult to comprehend.
                Last edited by bltdavid; 09-17-2022, 11:46 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Karado58, 11-26-2012, 02:57 PM
                8 responses
                14,828 views
                0 likes
                Last Post Option Whisperer  
                Started by Option Whisperer, Today, 09:05 AM
                0 responses
                1 view
                0 likes
                Last Post Option Whisperer  
                Started by cre8able, Yesterday, 01:16 PM
                3 responses
                11 views
                0 likes
                Last Post cre8able  
                Started by Harry, 05-02-2018, 01:54 PM
                10 responses
                3,204 views
                0 likes
                Last Post tharton3  
                Started by ChartTourist, Today, 08:22 AM
                0 responses
                6 views
                0 likes
                Last Post ChartTourist  
                Working...
                X