Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Global Methods

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

    Global Methods

    I have a half dozen methods that I want to use in all of my strategies. I have seen some posts talking about base classes, some talking about partial classes and some talking about some global libraries to add them to.

    What is the BKM if I want have some methods that all my strategies use? I can make them static and pass everything in if I need to.

    Thanks,
    Erik

    #2
    Hello,

    What is BKM?

    If it helps, NinjaScript C#. So anything that applies to C#, that you are used to, will apply to NinjaScript.
    DenNinjaTrader Customer Service

    Comment


      #3
      BKM - Stands for Best Known Method.

      What do you recommend as the best way to do this?

      If there is no supported and/or prefered method I will just figure one out.

      Thanks,
      Erik

      Comment


        #4
        Erik, if just for your personal use, you could put the 'global methods' into the UserDefinedMethods.cs file -





        Comment


          #5
          Erik,

          You may want to reference this post as well.

          The problem with using the UserDefinedMethods is that if you want to export any indicators that reference methods / function / etc from UserDefinedMethods, you can't. Best to make an empty indicator and strip it as described in the post i referenced above.

          mrLogik
          mrlogik
          NinjaTrader Ecosystem Vendor - Purelogik Trading

          Comment


            #6
            public static variables and methods are globally available,

            in some .cs file:

            namespace MyName {

            public class Globals {
            public static int I;
            public static string GetString(){ return "mystring";}
            }
            }


            then from within your code you can access this
            ..
            MyName.Globals.I = 7;
            string s = MyName.Globals.GetString();

            ...
            that's it.
            Be careful when different threads call into your class Globals. Then you should use lock.

            Partial classes are very useful and I use polymorphism extensively.
            So I have MyStrategy:Strategy

            and then MySP500Strategy:MyStrategy.

            so MySP500Strategy inherits all MyStrategy stuff and again all Strategy stuff.

            The nice side of this approach is that you can define cleanup procedures in Dispose().

            Just do:

            MyClass.Dispose(){

            cleanup();
            base.Dispose();
            }


            and then cleanup is properly defined for each layer.

            Splitting/distributing your code into the different classes (into Strategy or MyStrategy or MySP500Strategy) is arbitrary and dipends on how you want to organize your code.

            Good luck

            Andreas

            Comment


              #7
              All,

              Thanks for the help. I think I am going to start with the UserDefinedMethods.cs and the base class approaches first.

              Erik

              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