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

Common structs

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

    #16
    Originally posted by poseidon_sthlm View Post
    What would a class for common structs that could be used between different indicators and strategies look like?

    /P
    Your question lacks a bit of clarity. It seems to me that what you are seeking is to declare a struct type. Whereas you can declare a type in a class, if you want to declare a type that is available to other classes, you should declare the type in a namespace.

    In this case, the best place to declare the type would be in a namespace that is by default in scope in both Strategies and Indicators. Typically, NinjaTrader support and Development both will tell you to declare the type in the global namespace. (There is a reason for why they do, but it is still not a good idea). As all strategies bring the NinjaTrader.NinjaScript.Indicators namespace into scope, that might be one place where you could declare your new type. Of course, all Indicators reside in that namespace and so will always be in scope.

    Once declared this way, you would then simply use your struct as the type that you are declaring in any class. If you put that in a partial class, then a copy of it will be available to anything that inherits from that partial class.

    ref: https://msdn.microsoft.com/en-us/library/0taef578.aspx

    Comment


      #17
      Thank you, kognam, for your explanation and your patience.

      I have understood so far that:
      1) I should declare the common structs types in a partial class
      2) The partial class should be put within a namespace that by default is in scope in both Strategies and Indicators, for instance: namespace NinjaTrader.NinjaScript.Indicators

      I have tested this approach:
      Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
        //Common structs used between different indicators
        partial class Indicator
        {
      
         public struct DataPoint
         {
           public int barNo;
           public double value;
      
           public DataPoint( int dpBarNo, double dpValue)
           {
             this.barNo = dpBarNo;
             this.value = dpValue;
           }
        }
      
      }

      If I name the partial class "Indicators" then I can use the type DataPoint directly in my indicators like this:

      Code:
      public class MyIndicator : Indicator
      	{
      		//Variables
      		private DataPoint myDataPoint;
      But if I want to use the type DataPoint in a Stratgey then I have to name the variable like this:
      Code:
      public class MyStrategy : Strategy
      	{
      		//Variables
      		private [COLOR="Red"]Indicators.[/COLOR]DataPoint myDataPoint;
      The good thing is that there seemes to be a way to use the same struct type between Indicators and strategies.

      My last question is if there is a way to name the partial class so that I can use the struct type in both indicators and strategies without using the name of the partial class as a prefix for neither stratgies nor indicators?
      Last edited by poseidon_sthlm; 04-26-2017, 03:28 AM.

      Comment


        #18
        The C# programming language does not support multiple inheritance and, as a result, you can not create an object that is both an Indicator and a Strategy. The best I believe you can do is create a partial class for both Indicators and Strategies, and have both of these classes in turn rely on a third common class. This way your Indicators and Strategies would still have access to a common code base you only have to edit and maintain in one place. I have attached an example which accomplishes this.
        Attached Files
        Jessica P.NinjaTrader Customer Service

        Comment


          #19
          Originally posted by poseidon_sthlm View Post
          Thank you, kognam, for your explanation and your patience.

          I have understood so far that:
          1) I should declare the common structs types in a partial class
          2) The partial class should be put within a namespace that by default is in scope in both Strategies and Indicators, for instance: namespace NinjaTrader.NinjaScript.Indicators
          I am not sure where I was unclear, but that is definitely not what I wrote. I wrote that you should declare the type in the namespace, not in the partial class.
          I have tested this approach:
          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          {
            //Common structs used between different indicators
            partial class Indicator
            {
           
             public struct DataPoint
             {
               public int barNo;
               public double value;
           
               public DataPoint( int dpBarNo, double dpValue)
               {
                 this.barNo = dpBarNo;
                 this.value = dpValue;
               }
            }
           
          }

          If I name the partial class "Indicators" then I can use the type DataPoint directly in my indicators like this:

          Code:
          public class MyIndicator : Indicator
              {
                  //Variables
                  private DataPoint myDataPoint;
          But if I want to use the type DataPoint in a Stratgey then I have to name the variable like this:
          Code:
          public class MyStrategy : Strategy
              {
                  //Variables
                  private [COLOR=red]Indicators.[/COLOR]DataPoint myDataPoint;
          The good thing is that there seemes to be a way to use the same struct type between Indicators and strategies.

          My last question is if there is a way to name the partial class so that I can use the struct type in both indicators and strategies without using the name of the partial class as a prefix for neither stratgies nor indicators?
          Here is how you would go about it.
          Code:
          [COLOR=#006400]//This namespace holds Indicators in this folder and is required. Do not change it. [/COLOR]
          [COLOR=#0000ff]namespace[/COLOR][COLOR=#080808]NinjaTrader[/COLOR][COLOR=#000000].[/COLOR][COLOR=#080808]NinjaScript[/COLOR][COLOR=#000000].[/COLOR][COLOR=#080808]Indicators[/COLOR]
          {
          [COLOR=#0000ff]public[/COLOR] [COLOR=#0000ff]struct[/COLOR] [COLOR=#080808]DataPoint[/COLOR]
          {
          [COLOR=#0000ff]public[/COLOR] [COLOR=#0000ff]int[/COLOR] [COLOR=#080808]barNo[/COLOR];
          [COLOR=#0000ff]public[/COLOR] [COLOR=#0000ff]double[/COLOR] [COLOR=#080808]value[/COLOR];
           
          [COLOR=#0000ff]public[/COLOR] [COLOR=#080808]DataPoint[/COLOR]( [COLOR=#0000ff]int[/COLOR] [COLOR=#080808]dpBarNo[/COLOR], [COLOR=#0000ff]double[/COLOR] [COLOR=#080808]dpValue[/COLOR])
          {
          [COLOR=#0000ff]this[/COLOR].[COLOR=#080808]barNo[/COLOR] = [COLOR=#080808]dpBarNo[/COLOR];
          [COLOR=#0000ff]this[/COLOR].[COLOR=#080808]value[/COLOR] = [COLOR=#080808]dpValue[/COLOR];
          }
          }
          }
          You would save that as a file, with any name you care to choose, such as poseidonCustomObjects.cs. The name of the file is irrelevant: only its contents matter, but you will notice that I gave the file a name that tells me what the file holds. You might want to put it in the AddOns folder, but as I am defining it in the NinjaTrader.NinjaScript.Indicators namespace, I keep it in the Indicators folder. Again it is a matter of how you want to organize your environment.

          Then in either Strategy or Indicator, you can do this:
          Code:
          [COLOR=#0000ff]private[/COLOR] [COLOR=#080808]DataPoint[/COLOR] [COLOR=#080808]myDataPoint1[/COLOR] = [COLOR=#0000ff]new[/COLOR] [COLOR=#080808]DataPoint[/COLOR]();
          [COLOR=#0000ff]private[/COLOR] [COLOR=#080808]DataPoint[/COLOR] [COLOR=#080808]myDataPoint2[/COLOR] = [COLOR=#0000ff]new[/COLOR] [COLOR=#080808]DataPoint[/COLOR]([COLOR=#ff8c00]0[/COLOR], [COLOR=#ff8c00]0.0[/COLOR]);
          Again, if you choose to declare these DataPoint in partial classes, as protected access, then every class that inherits from the partial class in which these are declared, would immediately, ipso facto, have a copy of these objects. Of course, you can hide the inherited object by redefining it if you want, but you get it just because of how inheritance works in C Sharp.
          Last edited by koganam; 04-26-2017, 01:45 PM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by cre8able, Today, 01:01 PM
          0 responses
          2 views
          0 likes
          Last Post cre8able  
          Started by manitshah915, Today, 12:59 PM
          0 responses
          3 views
          0 likes
          Last Post manitshah915  
          Started by ursavent, Today, 12:54 PM
          0 responses
          3 views
          0 likes
          Last Post ursavent  
          Started by Mizzouman1, Today, 07:35 AM
          3 responses
          17 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by RubenCazorla, Today, 09:07 AM
          2 responses
          13 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X