Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

best c# Namespace for user functions??

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

    best c# Namespace for user functions??

    I have multiple functions that I have developed to use in my strategies..
    What namespace should they be defined in so I can reference them in any strategy (and indicators) ?
    Thanks!

    #2
    Hello waltFX,

    If you put your functions inside a custom class that you create an instance of then it doesn't matter, you can use any namespace you want. That would work for both indicators or strategies.

    If you are trying to use the C# partial class feature you would need to define your methods inside a partial class for the type you want the methods to apply to and in the namespace for that type. This only works for the one type you specify like Strategy as an example. Using partial classes also does not work well if you plan to export the scripts so if this is something you will distribue you should just use a normal C# class and define it in a custom namespace of your choosing.

    If this is just for your personal use on that pc you can do something like the following:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy
        {
    
          public double CalculateDelta(double firstPrice, double secondPrice)
          {
                return Math.Abs(firstPrice - secondPrice);
          }
       }
    }​
    Then each strategy could just call the method like any other NinjaScript function.

    Code:
    CalculateDelta(Close[0], Open[0]);
    Last edited by NinjaTrader_Jesse; 03-10-2023, 03:39 PM.

    Comment


      #3
      Thanks, I need to be able to see some strategy properties/objects (without passing them in) so I guess the "partial" method will work...
      >> I am the only user and transfer source code from my dev system to my prod system so that should work...

      Comment


        #4
        In my experience, using an abstract base class is
        much better than partial classes.

        Good reading here, here, and here.

        I provide a very useful example of an abstract
        base class for Strategies here.

        Comment


          #5
          thanks, In other languages, multiple inheritance could be used but, the last time I looked, c# does not support it...and I moved to DB work (and didn't keep up)
          Abstract class might be a better way to implement...
          Thanks for the tip!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, 03-13-2026, 05:17 AM
          0 responses
          93 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          152 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          80 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          53 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          65 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X