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

Namespace for enums and structs?

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

    Namespace for enums and structs?

    Hello!

    I would like to put some enums and structs that are used by a strategy and a few indicators in a separate namespace. How do I code this namespace so that that enum varaibles and struct objects can be exposed and passed from the indicator to the strategy? I understand that that enums should be defined outside the
    Code:
    namespace NinjaTrader.
    Should structs be defined within the
    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    ? Can you please help me to correct the example below into something useful, like a 'utility' that I can add with a 'using' directive to the indicators and strategies that share these enums and structs?


    Code:
    namespace myNamespace
    {
    	public enum MyEnum
    	{
    		Alt1,
    		Alt2,
    	}
    	
    	
    	public struct MyStruct
    	{
    		public string name;
    		public int count;
    		
    		public MyStruct(string name, int count)
    		{
    			this.name = myName;
    			this.count = myCount;
    		}
    	}
    }
    /poseidon_sthlm

    #2
    Hello poseidon_sthlm,

    Thank you for your note.

    You could create a new Addon, in which you define your enum. Then you would pass this namespace to your indicator with the using statement.

    I've attached a screen shot of how you could set this up.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Can I also put the struct in my example in the AddOn X or do I need some additional code for that?

      Comment


        #4
        Hello poseidon_sthlm,

        Yes you can put that in that namespace as well.

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by poseidon_sthlm View Post
          Hello!

          I would like to put some enums and structs that are used by a strategy and a few indicators in a separate namespace. How do I code this namespace so that that enum varaibles and struct objects can be exposed and passed from the indicator to the strategy? I understand that that enums should be defined outside the
          Code:
          namespace NinjaTrader.
          Should structs be defined within the
          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          ? Can you please help me to correct the example below into something useful, like a 'utility' that I can add with a 'using' directive to the indicators and strategies that share these enums and structs?


          Code:
          namespace myNamespace
          {
          	public enum MyEnum
          	{
          		Alt1,
          		Alt2,
          	}
          	
          	
          	public struct MyStruct
          	{
          		public string name;
          		public int count;
          		
          		public MyStruct(string name, int count)
          		{
          			this.name = myName;
          			this.count = myCount;
          		}
          	}
          }
          /poseidon_sthlm
          I believe that you have already asked this question once before, ref: https://ninjatrader.com/support/foru...ass#post504455, and I gave you a solution in this post on that thread: https://ninjatrader.com/support/foru...74&postcount=7

          No?

          Comment


            #6
            Originally posted by poseidon_sthlm View Post
            I would like to put some enums and structs that are used by a strategy and a few indicators in a separate namespace. How do I code this namespace so that that enum varaibles and struct objects can be exposed and passed from the indicator to the strategy?
            --For the simple case--

            Consider the indicator that needs a publicly exposed enum. I employ an embedded namespace named reminiscent of the indicator, and then specify it in the 'using' section. A strategy would access the same enum by also 'using' the same namespace.

            For example, consider the bltColorSwing indicator in file bltColorSwing.cs,

            Code:
            #region Using Declarations
            ...
            using blt.ColorSwing;
            #endregion
            
            namespace blt.ColorSwing
            {
                public enum ColorStyle { Transparent, SlopeColors, CustomColors, PlotColor }
            }
            
            namespace NinjaTrader.Indicator
            {
                [Description("bltColorSwing plots a colorized swing ...")]
                public class bltColorSwing : Indicator
                {
                    ....
                    private ColorStyle ColorStyle = ColorStyle.CustomColors;
                    ....
                }
            }
            --For the complex case--

            Sometimes you need common classes and structs (in addition to enums) shared between indicators and strategies. This is where I employ an extra file, which I've called bin/Custom/Indicator/bltCommon.cs.

            For example, consider this file bltCommon.cs,

            Code:
            namespace blt.Common
            {
                public enum SwingStatus
                {  NoSwing, LowerHigh, HigherHigh, LowerLow, HigherLow }
                
                public struct SwingPoint
                {
                    public int Bar;             // current bar index
                    public int Rank;            // rank of swing (HHLL > 0) and (LHHL == 0)
                    public double Price;        // price of swing (High/Low)
                    public SwingStatus Status;  // type of swing
                    public int Index;           // array index
            
                    public void CopyFrom(SwingPoint s)
                    {
                        Bar = s.Bar;
                        Rank = s.Rank;
                        Price = s.Price;
                        Status = s.Status;
                        Index = s.Index;
                    }
            
                    public bool IsEqual(SwingPoint s)
                    {
                        return Bar == s.Bar &&
                               Rank == s.Rank &&
                               Price == s.Price;
                    }
                }
            
                public class Retracement
                {
                    public SwingPoint Anchor;   // starting swing
                    public SwingPoint Target;   // ending swing
                    public int SwingHeight;     // height in ticks, always positive
            
                    public Retracement(Retracement z)
                    {
                        CopyFrom(z);
                    }
            
                    public void CopyFrom(Retracement z)
                    {
                        Anchor.CopyFrom(z.Anchor);
                        Target.CopyFrom(z.Target);
            
                        SwingHeight = z.SwingHeight;
                    }
                }
            }
            Then inside bltColorSwing.cs, the 'using' declarations gets changed to,

            Code:
            #region Using Declarations
            ...
            using blt.Common;
            using blt.ColorSwing;
            #endregion
            For any strategy needing access to these common code items, they need to add,

            Code:
            using blt.Common;
            using blt.ColorSwing;
            to their 'using' section.

            Admittedly, this is a contrived NT7 example, but NT8 is amazingly similar.

            Comment


              #7
              Originally posted by poseidon_sthlm View Post
              I would like to put some enums and structs that are used by a strategy and a few indicators in a separate namespace. How do I code this namespace so that that enum varaibles and struct objects can be exposed and passed from the indicator to the strategy?
              Give it a separate name, unique to you, then add it the 'using' section of all files that need to access these common items.

              Originally posted by poseidon_sthlm View Post
              I understand that that enums should be defined outside the
              Code:
              namespace NinjaTrader.
              Sure, but as long as you don't have any name conflicts, you can kinda do what you want.

              Originally posted by poseidon_sthlm View Post
              Should structs be defined within the
              Code:
              namespace NinjaTrader.NinjaScript.Indicators
              ?
              Well, you could, but I woudn't do that. Put them in a common file, inside a common namespace, with a name unique to you.

              Originally posted by poseidon_sthlm View Post
              Can you please help me to correct the example below into something useful, like a 'utility' that I can add with a 'using' directive to the indicators and strategies that share these enums and structs?

              Code:
              namespace myNamespace
              {
                  public enum MyEnum
                  {
                      Alt1,
                      Alt2,
                  }
                  
                  
                  public struct MyStruct
                  {
                      public string name;
                      public int count;
                      
                      public MyStruct(string name, int count)
                      {
                          this.name = myName;
                          this.count = myCount;
                      }
                  }
              }
              /poseidon_sthlm
              Looks good to me.

              What's the problem?

              Inside your strategies and indicators that need access to MyEnum and MyStruct, they must first say,

              Code:
              using myNamespace;
              Just make sure that the name 'myNamespace' is sufficiently unique. The names MyEnum and MyStruct can be anything, really. They can even be same names as other common enums and structs, it doesn't strictly matter because these names are defined in a different namespace -- but that's not recommended, since down that path lies insanity and dementia.

              Also, make sure the file containing 'myNamespace' is called 'myNamespace.cs' -- same reason here, the point is to maintain your own sanity by reducing unnecessary remembering of useless extra details.

              Comment


                #8
                Originally posted by koganam View Post
                I believe that you have already asked this question once before, ref: https://ninjatrader.com/support/foru...ass#post504455, and I gave you a solution in this post on that thread: https://ninjatrader.com/support/foru...74&postcount=7

                No?
                Hi! This time I was looking for a way to organize and separate structs and enums that belong to different strategies. Using a separate namespace around enums also makes it possible to optimize them in the 'Strategy Analyzer'.

                /poseidon_sthlm

                Comment


                  #9
                  Thanks for all suggestions and help!

                  So far a separate namespace for enums and structs seems to work as expected. But I have one more question about where to place the namespace. Is it ok to place a namespace at the end of the indicator file or should it be in a sparate file in the AddOn folder?

                  If I place my namespace in an indicator file before

                  Code:
                  namespace NinjaTrader.NinjaScript.Indicators
                  {
                  }
                  ,the compiler throws an error, but not if I place my namespace after.

                  /poseidon_sthlm
                  Last edited by poseidon_sthlm; 07-04-2018, 03:49 AM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Kaledus, Today, 01:29 PM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by frankthearm, Yesterday, 09:08 AM
                  13 responses
                  45 views
                  0 likes
                  Last Post frankthearm  
                  Started by PaulMohn, Today, 12:36 PM
                  2 responses
                  16 views
                  0 likes
                  Last Post PaulMohn  
                  Started by Conceptzx, 10-11-2022, 06:38 AM
                  2 responses
                  55 views
                  0 likes
                  Last Post PhillT
                  by PhillT
                   
                  Started by yertle, Yesterday, 08:38 AM
                  8 responses
                  37 views
                  0 likes
                  Last Post ryjoga
                  by ryjoga
                   
                  Working...
                  X