Announcement

Collapse
No announcement yet.

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);

    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);

        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);
                }​
            }​

            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 Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              633 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              364 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              105 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              567 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              568 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X