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

How to use namespaces?

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

    How to use namespaces?

    I have an NT sample code as follows:
    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {[INDENT]public partial class MySharedMethods : Indicator[/INDENT][INDENT]{[/INDENT][INDENT=2]// This double can be accessed from within an indicator with MySharedMethods.CalculateDelta()
    // or can be accessed from any script using NinjaTrader.NinjaScript.Indicators.MySharedMethods.CalculateDelta()
    public static double CalculateDelta(double firstPrice, double secondPrice)
    {
    return Math.Abs(firstPrice - secondPrice);
    }[/INDENT][INDENT]}[/INDENT]
     }
    ​
    Notice how long it is ...?

    How can I shorten the method call? I've tried using the namespace: NinjaTrader.NinjaScript.Indicators but I get a compile time error of: NinjaTrader.NinjaScript.Indicators.MySharedMethods () is a method which is not valid in the given context.

    I can access the method using the very long method call: NinjaTrader.NinjaScript.Indicators.MySharedMethods .CalculateDelta()

    #2
    Hello Skechers,

    In general you should be using the full namespace which points to where you put your code. that helps the platform during exports to allow the code to be exported.

    The way you defined the partial class is defined as an indicator rather than being a partial class of an indicator. You could define it like the following if you want to use this code on your local machine, this would not be good if you plan to export the code.


    Code:
    public partial class Indicator {
        public double CalculateDelta(double firstPrice, double secondPrice)
       {
            return Math.Abs(firstPrice - secondPrice);
       }​
    }
    In your indicator you can then just use CalculateDelta(price1, price2);
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi but this is for code in the same file right? I want to have the functions in a separate file so I can reuse it.

      Many Thanks, Caesar.

      Comment


        #4
        Hello Skechers,

        The code I provided is how you would share code between multiple indicators on your local machine. If you wanted to export the code its better to use a custom class similar to what you had but not inheriting from indicator.

        Code:
        public class MySharedMethods
        {
            public double CalculateDelta(double firstPrice, double secondPrice)
           {
              return Math.Abs(firstPrice - secondPrice);
           }​​
        }
        In your other script:

        Code:
        Your.Full.Namespace.To.Class.MySharedMethods myMethod = new Your.Full.Namespace.To.Class.MySharedMethods();
        myMethod.CalculateDelta(price1, price2);
        If you are comfortable using C# and understand how to use static in C# you can also make a static class with static methods if you don't want to create a instance to the class.

        Code:
        public class MySharedMethods
        {
            public static double CalculateDelta(double firstPrice, double secondPrice)
           {
              return Math.Abs(firstPrice - secondPrice);
           }​​
        }
        
        Your.Full.Namespace.To.Class.MySharedMethods.CalculateDelta(price1, price2);
        JesseNinjaTrader Customer Service

        Comment


          #5
          Many Thanks Jesse, I have tried the partial class and static method, but when I call MACD from the static method, I get an error as attached picture.
          Attached Files

          Comment


            #6
            Hello Skechers,

            Right a static method in a separate class is not a NinjaScript object which relates to bar data like an indicator. You would need to create the MACD inside your script and pass it as a parameter to that method.

            Code:
             public static double MyCustomMethod(MACD inputMacd)
            Code:
            protected override void OnBarUpdate()
            {
            MACD myMacd =  MACD(12, 26, 9);
            ​
            Your.Full.Namespace.To.Class.MySharedMethods.MyCustomMethod(myMacd);

            To use the MACD DIRECTLY in your extra method class it would need to be a partial class of the type you are using like Indicator;

            Code:
            public partial class Indicator {
                public double CalculateDelta(double firstPrice, double secondPrice)
                {
                MACD myMacd = MACD(12, 26, 9);
            ​
                return Math.Abs(firstPrice - secondPrice);
                }​
            }​
            JesseNinjaTrader Customer Service

            Comment


              #7
              I see, I like the partial class, but I believe i've done that before but ran into an error. I will try that again.

              Many Thanks, Caesar.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by rtwave, 04-12-2024, 09:30 AM
              2 responses
              20 views
              0 likes
              Last Post rtwave
              by rtwave
               
              Started by tsantospinto, 04-12-2024, 07:04 PM
              5 responses
              67 views
              0 likes
              Last Post tsantospinto  
              Started by cre8able, Today, 03:20 PM
              0 responses
              6 views
              0 likes
              Last Post cre8able  
              Started by Fran888, 02-16-2024, 10:48 AM
              3 responses
              49 views
              0 likes
              Last Post Sam2515
              by Sam2515
               
              Started by martin70, 03-24-2023, 04:58 AM
              15 responses
              115 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X