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

    Common structs

    Hello!

    I'm in the process of converting my scripts from NT7 to NT8. In NT7 I declared all my structs in the "MyCustomIndicatorMethods" file since this made is possible to use a struct in any indicator and also to pass an instance of a struct between indicators. What is the best way to this in NT8?

    I have tried to create an Add On like this, but it doesn't seem to work as it did in NT7.

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    	//Common structs used between different indicators and strategies
    	public partial class Structs
    	{
    		public struct DataPoint
    		{
    			public int barNo;
    			public double value;
    						
    			public DataPoint( int dpBarNo, double dpValue)
    			{
    				this.barNo = dpBarNo;
    				this.value = dpValue;
    			}
    		}
    	}
    }
    Last edited by poseidon_sthlm; 04-22-2017, 08:32 AM.

    #2
    Originally posted by poseidon_sthlm View Post
    Hello!

    I'm in the process of converting my scripts from NT7 to NT8. In NT7 I declared all my structs in the "MyCustomIndicatorMethods" file since this made is possible to use a struct in any indicator and also to pass an instance of a struct between indicators. What is the best way to this in NT8?

    I have tried to create an Add On like this, but it doesn't seem to work as it did in NT7.

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    	//Common structs used between different indicators and strategies
    	public partial class Structs
    	{
    		public struct DataPoint
    		{
    			public int barNo;
    			public double value;
    						
    			public DataPoint( int dpBarNo, double dpValue)
    			{
    				this.barNo = dpBarNo;
    				this.value = dpValue;
    			}
    		}
    	}
    }
    So how are you using your setup, and how is it failing? Any error messages? Related to what use?

    Comment


      #3
      Hello,

      Thank you for the post.

      With more details what is going wrong in NT8 with what you are trying that would be helpful. I am unsure if this would be of help but potentially using a static class in an addon like the one shown here would be of use: http://ninjatrader.com/support/forum...24&postcount=3

      Please let me know if i may be of additional assistance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Poseidon,

        I use a similar approach. While I am sure my solution is not the best, with an AddOn try:
        Code:
        /// <summary>
        /// For Struct to be accessible from all namespaces: Strategy and Indicator,
        /// it must be located outside namespace NinjaTrader.Strategy and .Indicator
        /// </summary>
        public struct DataPoint
        {
          public int barNo;
          public double value;
        
          public DataPoint( int dpBarNo, double dpValue)
          {
            this.barNo = dpBarNo;
            this.value = dpValue;
          }
        }
        Shannon

        Comment


          #5
          First thank you for all replies and suggestions!

          The approach described in my inital post does work as expected and I apologize for any confusion. A new instance of a struct has to be declared like this:
          Code:
          private Structs.DataPoint myDataPoint;
          But now I have some follow-up questions:
          1. What is the "supported approach" in NT8 to create structs that can be shared between indicators as was the case in NT7 with structs declared in the "MyCustomIndicatorMethods" file?

          2. I would prefer to be able to use my structs with both indicators and strategies. Would this be possible if I cange the namespace from

          Code:
          NinjaTrader.NinjaScript.Indicators
          {
          }
          to
          Code:
          NinjaTrader.NinjaScript
          {
          }
          3. Should I make the class static?
          Code:
          	public partial [B][COLOR="Red"]static[/COLOR][/B] class Structs
          	{
          /P

          Comment


            #6
            Originally posted by poseidon_sthlm View Post
            First thank you for all replies and suggestions!

            The approach described in my inital post does work as expected and I apologize for any confusion. A new instance of a struct has to be declared like this:
            Code:
            private Structs.DataPoint myDataPoint;
            But now I have some follow-up questions:
            1. What is the "supported approach" in NT8 to create structs that can be shared between indicators as was the case in NT7 with structs declared in the "MyCustomIndicatorMethods" file?

            2. I would prefer to be able to use my structs with both indicators and strategies. Would this be possible if I cange the namespace from

            Code:
            NinjaTrader.NinjaScript.Indicators
            {
            }
            to
            Code:
            NinjaTrader.NinjaScript
            {
            }
            3. Should I make the class static?
            Code:
            	public partial [B][COLOR="Red"]static[/COLOR][/B] class Structs
            	{
            /P
            Why not just make the code a partial class of the Indicator class?

            Comment


              #7
              After kognams reply I have another follow-up question:
              Is there any reason not to make the class that holds the structs a partial class of the Indicator class (exactly as the MyCustomIndicatorMethods file in NT7) ?

              namespace NinjaTrader.NinjaScript.Indicators
              {
              //Common structs used between different indicators
              partial class Indicator
              {
              }
              }

              Comment


                #8
                Thank you for your question poseidon_sthlm . The way you are describing working is the way I would recommend working. You may even call this file MyCustomIndicatorMethods.cs if you desire.

                To answer your question directly, the only drawback to this approach is that we introduce the possibility of name conflicts. This is to say, if you use a symbol name in the partial class, this name will be unavailable to other indicators at the same namespace level.

                This is not likely to come up for you as an individual user. However, in the NinjaTrader 7 situation, this situation was more likely to come up as users exported and shared NinjaScript with each other, as was the possibility someone else's MyCustomIndicatorMethods.cs would be overwritten by an import.

                Please let us know if there are any other ways we can help.
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_JessicaP View Post
                  ...
                  To answer your question directly, the only drawback to this approach is that we introduce the possibility of name conflicts. This is to say, if you use a symbol name in the partial class, this name will be unavailable to other indicators at the same namespace level.
                  Not quite correct, or not correctly put. If declared in a partial class, for any value object, any class derived or inheriting, or that has access to objects in the Indicator class gets a copy of the object that was created in the partial class.
                  This is not likely to come up for you as an individual user. However, in the NinjaTrader 7 situation, this situation was more likely to come up as users exported and shared NinjaScript with each other, as was the possibility someone else's MyCustomIndicatorMethods.cs would be overwritten by an import.

                  Please let us know if there are any other ways we can help.
                  Which makes the point in NT7. And it is not an inheritance issue. It is an accessibility issue caused by overwriting the object holder by other code.
                  Last edited by koganam; 04-24-2017, 08:28 AM. Reason: Clarified grammar.

                  Comment


                    #10
                    My apologies koganam, you are correct. I will leave my post unedited for context.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      All,

                      I appreciate my use of globals in post #4 is not the best solution.
                      A question, where a struct (or enum) is returned from an indicator to a strategy, is a better solution to use a partial class / classes? Would the below be a better approach?

                      Code:
                      namespace NinjaTrader.NinjaScript
                      {
                        //Common structs used between different indicators and strategies
                        partial class NinjaScript
                        {
                        }
                      }

                      Comment


                        #12
                        Your solution in post #4, or any other solution, will work well. The best solution is the one you are most comfortable with, that fits your style as a developer best.

                        What we would like to avoid is a situation where you send someone a file named "UserDefinedMethods.cs" that overwrites a similarly named file on another computer. A naming convention unique to you for the UserDefinedMethods file itself should prevent this from happening. As an example, if your e-mail address or online handle was [email protected], you could use "BigBirdUserDefinedMethods.cs" as a name that would not be likely to conflict.

                        I have attached an example which will work for NinjaTrader 8.
                        Attached Files
                        Jessica P.NinjaTrader Customer Service

                        Comment


                          #13
                          The approach that Shansen suggests in post #11 looks more attractive than the one in post #7 if this makes it possible to use common structs between indicators as well as strategies. Is there any reason not use that approach (besides the naming conflict mentioned below)?

                          Code:
                          namespace NinjaTrader.NinjaScript
                          {
                            //Common structs used between different indicators [COLOR="Red"]and strategies[/COLOR]
                            partial class NinjaScript
                            {
                            }
                          }

                          Code:
                          namespace NinjaTrader.NinjaScript.Indicators
                          {
                            //Common structs used between different indicators
                            partial class Indicator
                            {
                            }
                          }
                          /p
                          Last edited by poseidon_sthlm; 04-25-2017, 05:02 PM. Reason: Corrected: "Shansen suggests in post #11" (instead of #4)

                          Comment


                            #14
                            Originally posted by poseidon_sthlm View Post
                            The approach that Shansen suggests in post #4 looks more attractive than the one in post #7 if this makes it possible to use common structs between indicators as well as strategies. Is there any reason not use that approach (besides the naming conflict mentioned below)?

                            Code:
                            namespace NinjaTrader.NinjaScript
                            {
                              //Common structs used between different indicators [COLOR=red]and strategies[/COLOR]
                              partial class NinjaScript
                              {
                              }
                            }

                            Code:
                            namespace NinjaTrader.NinjaScript.Indicators
                            {
                              //Common structs used between different indicators
                              partial class Indicator
                              {
                              }
                            }
                            /p
                            As with many coding questions, it is a matter of style and preference how you organize your code. However,
                            Code:
                            namespace NinjaTrader.NinjaScript
                            {
                              //Common structs used between different indicators [COLOR=red]and strategies[/COLOR]
                              partial class NinjaScript
                              {
                              }
                            }
                            is no different in effect from the code in your original post. All that you would have done is call your class "NinjaScript" (all in all, itself a debatably good thing to do), instead of "Structs", and not explicitly declared the access level to your "NinjaScript" class. In order to access its members, you will still have to declare an instance of the class and then access the members of the instance, just as you did with your original Structs class.

                            Comment


                              #15
                              What would a class for common structs that could be used between different indicators and strategies look like?

                              /P

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by thanajo, 05-04-2021, 02:11 AM
                              3 responses
                              467 views
                              0 likes
                              Last Post tradingnasdaqprueba  
                              Started by Christopher_R, Today, 12:29 AM
                              0 responses
                              10 views
                              0 likes
                              Last Post Christopher_R  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              166 responses
                              2,236 views
                              0 likes
                              Last Post sidlercom80  
                              Started by thread, Yesterday, 11:58 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post thread
                              by thread
                               
                              Started by jclose, Yesterday, 09:37 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Working...
                              X