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

Enum Declarations

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

    Enum Declarations

    Hi I have a question about enum declarations.

    Do I need to declare them outside the Indicator/Strategy class or can I declare them inside the class?

    It appears that some of my code has enum declarations inside the class. Sometimes it works and sometimes it doesn't. I'm not sure what is going on here.

    I took a look at your SampleUniversalMovingAverage example and I see that the enum is declared outside of the class.

    Thanks,

    CS

    #2
    Hello cshep,

    Thank you for your post.

    We support the way an enum is used in the mentioned sample, created in the namespace. Try to name it something unique, that coorelates with the indicator name to avoid namespace pollution (all indicators are compiled together into one assembly).

    There has been a discussion on enum usage in indicators, you can see the thread here: http://www.ninjatrader.com/support/f...ad.php?t=37521

    Please let me know if I can assist any further.
    DexterNinjaTrader Customer Service

    Comment


      #3
      i have an indicator that has an enum, and i want to use them in a strategy. the indicator has five different conditions, neutral, longmarket, shortmarket, longlimit & shortlimit.

      the indicator name is Thunder
      the enum name is OrderLogic
      Neutral
      LongMarket
      ShortMarket
      LongLimit
      ShortLimit
      when ever the indicator value's one of these conditions i want the strategy to place an order. but i can not figure out how to call the enum's into the strategy?

      using the wizard i have gotten this far, but i knew and know that won't match the value of the enum.

      // Condition set 3
      if (Thunder(3, 5, 3, 13, 0, NinjaTrader.Indicator.OperatingMode.Oscillator, 233, 0.33).Umbrella[0] == LongMarket) ¿¿¿"Thunder.OrderLogic.LongMarket"???
      {
      ExitShort("Exit", "");
      EnterLong(DefaultQuantity, "LongMarket");
      }

      Comment


        #4
        Jupie,

        Thanks for the post.

        However this falls under communication between strategies or in your case communication between strategy and indicator. Which unfortunately we do not provide support for and is outside of the scope we are able to provide support for.

        If you need the indicator data in the strategy you will need to copy the indicator code into the strategy so it runs natively in the strategy. This would be the supported method to do this.

        Let me know if I can be of further assistance.

        Comment


          #5
          Originally posted by NinjaTrader_Brett View Post
          However this falls under communication between strategies or in your case communication between strategy and indicator. Which unfortunately we do not provide support for and is outside of the scope we are able to provide support for.
          really than why is there a ADD()

          If you need the indicator data in the strategy you will need to copy the indicator code into the strategy so it runs natively in the strategy. This would be the supported method to do this.
          this does not seem like it something that can be done via the wizard so there most be another supported ways.
          Let me know if I can be of further assistance.
          that would be nice

          Comment


            #6
            Hello jupiejupe,

            You can achieve this in the most straightforward manner by declaring your enum in the NinjaTrader.Indicator namespace, but not in your class. Be sure to name it something unique, preferably with a similar name prefix as your strategy, as everything is in one namespace for indicators and strategies respectively and you want to avoid naming conflicts.

            Once that is done you can access it by using the enumeration directly in your strategy, e.g.
            OrderLogic Something;
            // stuff
            if(Something == OrderLogic.Neutral)

            Here is a link to a post with a sample of doing this, built from the SampleUniversalMovingAverage example: http://www.ninjatrader.com/support/f...02&postcount=9

            Please let me know if I can assist any further.
            DexterNinjaTrader Customer Service

            Comment


              #7
              thank you so much this will point me in the correct direction. an thank you so much for given a straight answer, i really appreciate the guidance.

              Comment


                #8
                Hi jupiejupe,

                You are most welcome,

                Just let us know if we can assist you with any other NinjaTrader inquiries.
                DexterNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Dexter, I followed the advice given in the post you recommended and I wrapped all of my enums in separate namespaces to avoid namespace pollution. I did this for all indicators and strategies.

                  This seems to be working well. Do you foresee any problems with this? It doesn't seem to be a NinjaTrader recommended practice.

                  Any thoughts on having all enums in a common namespace under UserDefinedMethods or something similar?

                  Thanks,

                  CS

                  Comment


                    #10
                    Hi cshep,

                    Thank you for your reply.

                    While we cannot provide support for this method, I do not see how any issues aside from naming conflict could come up with this approach.

                    Please let me know if I can assist you any further.
                    DexterNinjaTrader Customer Service

                    Comment


                      #11
                      Hi, I am trying to utilize the SampleUniversalMovingAverage Indicator code in a strategy. I understand how to use it for an indicator but I'm having difficulty using it in a strategy. I would like to have the option in the strategy settings to select which type of stop loss method to use. i.e. set stop loss to an SMA or EMA

                      A very simple example on how to use this with either an SMA or EMA on a long entry would be perfect to get me started.

                      Comment


                        #12
                        Hello brucelevy,

                        Thanks for writing in to our Support team.

                        You will want to do this in your switch statements and adjust depending on which MAType you are using.

                        For example, using the code provided in the SamleUniversalMovingAverage.cs from our references thread:

                        //create a variable to hold the moving average type
                        private UniversalMovingAverage matype = UniversalMovingAverage.SMA;

                        protected override void OnBarUpdate()
                        {
                        switch (matype)
                        case UniversalMovingAverage.SMA:
                        {
                        // set your stop loss based on an SMA value
                        SetStopLoss(SMA(20));
                        }

                        Please let me know if I may be of any further assistance.
                        Alan S.NinjaTrader Customer Service

                        Comment


                          #13
                          I am getting this error: https://gyazo.com/d34b91207c283c010d1fb44ecd8bf484

                          Code:
                          #region Using declarations
                          using System;
                          using System.ComponentModel;
                          using System.Diagnostics;
                          using System.Drawing;
                          using System.Drawing.Drawing2D;
                          using System.Xml.Serialization;
                          using NinjaTrader.Cbi;
                          using NinjaTrader.Data;
                          using NinjaTrader.Indicator;
                          using NinjaTrader.Gui.Chart;
                          using NinjaTrader.Strategy;
                          #endregion
                          
                          // This namespace holds all strategies and is required. Do not change it.
                          namespace NinjaTrader.Strategy
                          {
                              /// <summary>
                              /// Enter the description of your strategy here
                              /// </summary>
                              [Description("Enter the description of your strategy here")]
                              public class matypetest : Strategy
                              {
                                  #region Variables
                                private UniversalMovingAverage	matype	= UniversalMovingAverage.SMA;
                          		private int				period	= 14;
                                  #endregion
                          
                                  /// <summary>
                                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                                  /// </summary>
                                  protected override void Initialize()
                                  {
                                      CalculateOnBarClose = true;
                                  }
                          
                                  /// <summary>
                                  /// Called on each bar update event (incoming tick)
                                  /// </summary>
                                  protected override void OnBarUpdate()
                                  {
                          			
                          			switch (matype)
                          			{
                          			
                          case UniversalMovingAverage.SMA:
                          {
                          // set your stop loss based on an SMA value
                          SetStopLoss(SMA(20));
                          }
                          
                          case UniversalMovingAverage.HMA:
                          		{
                          			// Sets the plot to be equal to the HMA's plot
                          			SetStopLoss(HMA(20));				
                          		}
                          
                          
                          			}
                          
                          
                                  }
                          
                                  #region Properties
                                  		// Creates the user definable parameter for the moving average type.
                          		[Description("Choose a Moving Average type.")]
                          		[Category("Parameters")]
                          		public UniversalMovingAverage MAType
                          		{
                          			get { return matype; }
                          			set { matype = value; }
                          		}
                          		
                          		[Description("Numbers of bars used for calculations")]
                          		[Category("Parameters")]
                          		public int Period
                          		{
                          			get { return period; }
                          			set { period = Math.Max(1, value); }
                          		}
                                  #endregion
                              }
                          } 
                          public enum UniversalMovingAverage
                          {
                          	EMA,
                          	HMA,
                          	SMA,
                          	WMA,
                          }

                          Comment


                            #14
                            Originally posted by brucelevy View Post
                            I am getting this error: https://gyazo.com/d34b91207c283c010d1fb44ecd8bf484

                            Code:
                            #region Using declarations
                            using System;
                            using System.ComponentModel;
                            using System.Diagnostics;
                            using System.Drawing;
                            using System.Drawing.Drawing2D;
                            using System.Xml.Serialization;
                            using NinjaTrader.Cbi;
                            using NinjaTrader.Data;
                            using NinjaTrader.Indicator;
                            using NinjaTrader.Gui.Chart;
                            using NinjaTrader.Strategy;
                            #endregion
                            
                            // This namespace holds all strategies and is required. Do not change it.
                            namespace NinjaTrader.Strategy
                            {
                                /// <summary>
                                /// Enter the description of your strategy here
                                /// </summary>
                                [Description("Enter the description of your strategy here")]
                                public class matypetest : Strategy
                                {
                                    #region Variables
                                  private UniversalMovingAverage	matype	= UniversalMovingAverage.SMA;
                            		private int				period	= 14;
                                    #endregion
                            
                                    /// <summary>
                                    /// This method is used to configure the strategy and is called once before any strategy method is called.
                                    /// </summary>
                                    protected override void Initialize()
                                    {
                                        CalculateOnBarClose = true;
                                    }
                            
                                    /// <summary>
                                    /// Called on each bar update event (incoming tick)
                                    /// </summary>
                                    protected override void OnBarUpdate()
                                    {
                            			
                            			switch (matype)
                            			{
                            			
                            case UniversalMovingAverage.SMA:
                            {
                            // set your stop loss based on an SMA value
                            SetStopLoss(SMA(20));
                            }
                            
                            case UniversalMovingAverage.HMA:
                            		{
                            			// Sets the plot to be equal to the HMA's plot
                            			SetStopLoss(HMA(20));				
                            		}
                            
                            
                            			}
                            
                            
                                    }
                            
                                    #region Properties
                                    		// Creates the user definable parameter for the moving average type.
                            		[Description("Choose a Moving Average type.")]
                            		[Category("Parameters")]
                            		public UniversalMovingAverage MAType
                            		{
                            			get { return matype; }
                            			set { matype = value; }
                            		}
                            		
                            		[Description("Numbers of bars used for calculations")]
                            		[Category("Parameters")]
                            		public int Period
                            		{
                            			get { return period; }
                            			set { period = Math.Max(1, value); }
                            		}
                                    #endregion
                                }
                            } 
                            public enum UniversalMovingAverage
                            {
                            	EMA,
                            	HMA,
                            	SMA,
                            	WMA,
                            }
                            You, or someone else whose code you are using, has already defined the enum UniversalMovingAverage in the global namespace. Call the one that you are defining here something else.

                            That is neither the neatest, nor most sensible way to tackle the issue, but that is the only NT-approved way.

                            Comment


                              #15
                              Hello brucelevy,

                              Thanks for your reply.

                              koganam is correct in saying that you have already defined an enum by the name of UniversalMovingAverage if you are receiving this error - I would recommend that you add this enum in a custom namespace like so:

                              //implement this namespace outside of the NinjaTrader.Strategy namespace

                              namespace _UniversalMovingAverageEnums
                              {

                              public enum UniversalMovingAverage
                              {
                              EMA,
                              HMA,
                              SMA,
                              WMA
                              }
                              }

                              You would retrieve the enum contained in this namespace like so:
                              switch (matype)
                              {
                              case _UniversalMovingAverageEnums.UniversalMovingAverag e.HMA
                              {
                              //set stop loss based on HMA value
                              SetStopLoss(HMA(20));
                              }
                              }

                              Please let me know if I may be of any further assistance.
                              Alan S.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kujista, Today, 05:44 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post kujista
                              by kujista
                               
                              Started by ZenCortexCLICK, Today, 04:58 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post ZenCortexCLICK  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              172 responses
                              2,281 views
                              0 likes
                              Last Post sidlercom80  
                              Started by Irukandji, Yesterday, 02:53 AM
                              2 responses
                              18 views
                              0 likes
                              Last Post Irukandji  
                              Started by adeelshahzad, Today, 03:54 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post adeelshahzad  
                              Working...
                              X