Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Partial Class for all of NinjaScript

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

    Partial Class for all of NinjaScript

    How do I create a Partial Class (or some equivalent construct) that will apply to all NinjaScript?

    For example, I declare some constants and some generic methods in that Partial Class and can then, with an appropriate using statement for the entire strategy or indicator or whatever, use the objects declared in that "global" Partial Class in any of them.

    Would it also be possible to use this approach to declare variables that could effectively be "global"?

    For example, I want a variable to be shared between a strategy and an indicator. Could I declare it in this "global" Partial Class and start using it as a "global" NinjaScript variable?

    And if what I am enquiring about is not (currently?) possible, can it be made possible, please?

    Thanks.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    #2
    Hello jeronymite,

    Thank you for writing in.

    You can create a new AddOn script and then create variables and methods that can be accessed from all indicators and strategies.

    Here's an example:
    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    	public partial class Indicator
    	{
    		public int SomeValue { get; set; }
    		
    		public void SomeMethod()
    		{
    			
    		}
    	}
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public partial class Strategy
    	{
    		public int SomeValue { get; set; }
    		
    		public void SomeMethod()
    		{
    			
    		}
    	}
    }
    You will be able to access SomeValue and SomeMethod() from all indicators and strategies.

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Partial Class for all of NinjaScript

      Thanks, Zachary.

      I already do this. The issue is that I must have multiple copies of the objects in a multitude of locations and that leads to maintenance overheads and a higher risk of inconsistent copies.

      I want one place, all objects declared once only, globally accessible to all other NinjaScript namespaces.

      So, something like
      Code:
      namespace NinjaTrader.NinjaScript.Global
      {
      ...
      }
      where that namespace then becomes globally accessible within all NinjaScript namespaces with a single "using" statement.

      Thanks.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Hello jeronymite,

        You can make a static class with static variables.

        Code:
        namespace MyNamespace
        {
        	public static class MyClass
        	{
        		public static int MyValue { get; set; }
        		
        		public static void MyMethod()
        		{
        		}
        	}
        }
        In my example above, add a using MyNamespace; to the script.

        To access the variable from another script, you will need to use the class name and then the variable name.

        For example:

        Code:
        Print(MyClass.MyValue);
        You'll also be able to modify the variable as well.

        Example:

        Code:
        MyClass.MyValue = 42;
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by jeronymite View Post
          Thanks, Zachary.

          I already do this. The issue is that I must have multiple copies of the objects in a multitude of locations and that leads to maintenance overheads and a higher risk of inconsistent copies.

          I want one place, all objects declared once only, globally accessible to all other NinjaScript namespaces.

          So, something like
          Code:
          namespace NinjaTrader.NinjaScript.Global
          {
          ...
          }
          where that namespace then becomes globally accessible within all NinjaScript namespaces with a single "using" statement.

          Thanks.
          You would use a class not a partial class.If you are going to use multiple copies of said class, you do not want it to be a static class.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Waxavi, Today, 02:00 AM
          0 responses
          2 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Started by elirion, Today, 01:36 AM
          0 responses
          4 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by gentlebenthebear, Today, 01:30 AM
          0 responses
          4 views
          0 likes
          Last Post gentlebenthebear  
          Started by samish18, Yesterday, 08:31 AM
          2 responses
          9 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by Mestor, 03-10-2023, 01:50 AM
          16 responses
          391 views
          0 likes
          Last Post z.franck  
          Working...
          X