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

How To Use Enum Value in a Function?

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

    How To Use Enum Value in a Function?

    Hi,

    I want to be able, to use, the User selected Enum DIRECTLY in a Function.
    The Following will Not work.

    Code:
    #region _Variables
    private MovAvg_MA_TF1    mA_TF1   = MovAvg_MA_TF1.SMA;
    #endregion
     
    public enum MovAvg_MA_TF1
    {
     EMA,
     HMA,
     SMA,
     WMA
    }
     
    switch (mA_TF1)
     {
      //If the MovAvgType is defined as EMA then...
      case MovAvg_MA_TF1.EMA:
      {
       var movAvgType = EMA;
       break;
      }
      //If the MovAvgType is defined as HMA then...
      case MovAvg_MA_TF1.HMA:
      {
       var movAvgType = HMA;
       break;
      }
      //If the MovAvgType is defined as SMA then...
      case MovAvg_MA_TF1.SMA:
      {
       var movAvgType = SMA;
       break;
      }
      //If the MovAvgType is defined as WMA then...
      case MovAvg_MA_TF1.WMA:
      {
       var movAvgType = WMA;
       break;
      }
     }
     
    Plot0.Set(movAvgType(Close[0], Period)[0]);
     
    # region Properties
     [Description("Choose a Moving Average type.")]
     [GridCategory("MA_TF1 Settings")]
     public MovAvg_MA_TF1 MA_TF1
     {
      get { return mA_TF1; }
      set { mA_TF1 = value; }
     }
    #endregion
    Is This POSSIBLE in C# ???

    Could someone PLEASE tell How I can modify the:

    var movAvgType =
    AND
    Plot0.Set(movAvgType(Close[0], Period)[0]);

    So that this method would work ???

    THANK YOU IN ADVANCE !!!

    P.S.
    (I know the other way that works is to put the:
    Plot0.Set(EMA(Close[0], Period)[0]);
    Plot0.Set(HMA(Close[0], Period)[0]);
    Plot0.Set(SMA(Close[0], Period)[0]);
    Plot0.Set(WMA(Close[0], Period)[0]);
    Inside each Case, But for other Reasons I Do Not want to do this.)

    #2
    Try assigning to a DataSeries instead of a variable:

    //In Variables
    private DataSeries movAvgType;

    //In Initialize()
    movAvgType = new DataSeries(this);

    //If the MovAvgType is defined as EMA then...
    case MovAvg_MA_TF1.EMA:
    {
    movAvgType.Set(EMA(Close, Period))[0];
    break;
    }
    //etc.

    //Use this to check if rising, for example
    if(Rising(movAvgType))
    //do something
    Last edited by eDanny; 04-01-2011, 03:27 PM.
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      What is the error message that you are getting?

      Comment


        #4
        Thank You for your Quick Response.

        I don't think this will acomplish what I'm really trying to acheive, because there are actually 64 possibilities (64 Switch Tests) for the combination of user inputs:

        publicenum MovAvg_MA_Type
        {
        EMA,
        HMA,
        SMA,
        WMA
        }
        publicenum MovAvg_MA_Input
        {
        CLOSE,
        HIGH,
        LOW,
        OPEN
        }
        publicenum MovAvg_MA_SmoothType
        {
        EMA,
        HMA,
        SMA,
        WMA
        }

        I did accomplish this with a 3 Layer deep 64 Switch Test, But wanted to simplify this with only 3 Switch Tests:
        1 For Moving Average Type: EMA,HMA,SMA,WMA
        1 For Input Type: Close,High,Low,Open
        1 For Moving Average Smoothing Type: EMA,HMA,SMA,WMA

        Then Use Just ONE Plot Function:

        mA_DataSeries.Set(mA_Type(mA_Input[0], mA_Period)[0]);
        Plot0.Set(mA_SmoothType(mA_DataSeries, mA_SmoothPeriod)[0]);

        Comment


          #5
          When I use the following Code:

          switch (mA_Type)
          {
          // If the MA Type is defined as EMA then...
          case MovAvg_MA.EMA:
          {
          switch (mA_Input)
          {
          // If Close Type is defined as CLOSE then...
          case MovAvg_MA_Input.CLOSE:
          {
          switch (mA_SmoothType)
          {
          // If the Smooth MA Type is defined as EMA then...
          case MovAvg_MA_SmoothType.EMA:
          {
          // Sets the Plot to be equal to MA-EMA, CLOSES, MA-SMOOTH-EMA
          var mAType = "EMA";
          var mAInput = "Closes";
          var mASmoothType = "EMA";
          // eval(type)(name);
          // eval(type)(name);
          break;
          }

          mA_DataSeries.Set(mAType(mAInput[0], mA_Period)[0]);
          Plot0.Set(mASmoothType(mA_DataSeries, mA_SmoothPeriod)[
          0]);

          I get the Following Errors:

          CS011 'mASmoothType' is a 'variable' but is used like a 'method'
          CS011 'mAType' is a 'variable' but is used like a 'method'

          Comment


            #6
            You are making a direct text substitution for a method name. I have never been able to get that to work. You instead want to instantiate your MATypes as variables themselves in the OnStartUp() method.

            Code:
            #region Variables
            
            private WMA wmaCloses;
            private EMA emaOpens;
            //etc
            
            protected override void OnStartUp()
            {
            wmaCloses = WMA(Close, Period);
            emaOpens = WMA(Open, Period);
            //etc
            }
            Then you access them in OnBarUpdate() with:

            wmaCloses[0] etc.

            You may want to take a look at this thread: http://www.ninjatrader.com/support/f...21406#poststop
            Last edited by koganam; 04-01-2011, 03:21 PM.

            Comment


              #7
              Or you can try your original way:

              public enum MovAvg_MA_Type
              {
              EMA,
              HMA,
              SMA,
              WMA
              }
              public enum MovAvg_MA_Input
              {
              CLOSE,
              HIGH,
              LOW,
              OPEN
              }
              public enum MovAvg_MA_SmoothType
              {
              EMA,
              HMA,
              SMA,
              WMA
              }
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              [Description("Enter the description of your new custom indicator here")]
              public class EnumTest : Indicator
              {
              #region Variables
              private int mA_Period = 14;
              private int mA_SmoothPeriod = 14;
              private MovAvg_MA_Type mA_TF1 = MovAvg_MA_Type.SMA;
              private MovAvg_MA_Input mA_Input = MovAvg_MA_Input.CLOSE;
              private MovAvg_MA_SmoothType mA_Smooth = MovAvg_MA_SmoothType.EMA;
              private DataSeries movAvgSmoothType;
              private DataSeries movAvgInput;
              private DataSeries movAvgType;

              #endregion

              /// <summary>
              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              protected override void Initialize()
              {
              Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));

              movAvgType = new DataSeries(this);
              movAvgInput = new DataSeries(this);
              movAvgSmoothType = new DataSeries(this);

              Overlay = true;
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              switch (mA_Input)
              {
              case MovAvg_MA_Input.CLOSE: movAvgInput.Set(Close[0]); break;
              case MovAvg_MA_Input.HIGH: movAvgInput.Set(High[0]); break;
              case MovAvg_MA_Input.LOW: movAvgInput.Set(Low[0]); break;
              case MovAvg_MA_Input.OPEN: movAvgInput.Set(Open[0]); break;
              }

              switch (mA_TF1)
              {
              case MovAvg_MA_Type.EMA: movAvgType.Set(EMA(movAvgInput, mA_Period)[0]); break;
              case MovAvg_MA_Type.HMA: movAvgType.Set(HMA(movAvgInput, mA_Period)[0]); break;
              case MovAvg_MA_Type.SMA: movAvgType.Set(SMA(movAvgInput, mA_Period)[0]); break;
              case MovAvg_MA_Type.WMA: movAvgType.Set(WMA(movAvgInput, mA_Period)[0]); break;
              }

              switch (mA_Smooth)
              {
              case MovAvg_MA_SmoothType.EMA: movAvgSmoothType.Set(EMA(movAvgType, mA_SmoothPeriod)[0]); break;
              case MovAvg_MA_SmoothType.HMA: movAvgSmoothType.Set(HMA(movAvgType, mA_SmoothPeriod)[0]); break;
              case MovAvg_MA_SmoothType.SMA: movAvgSmoothType.Set(SMA(movAvgType, mA_SmoothPeriod)[0]); break;
              case MovAvg_MA_SmoothType.WMA: movAvgSmoothType.Set(WMA(movAvgType, mA_SmoothPeriod)[0]); break;
              }

              Plot0.Set(movAvgSmoothType[0]);
              }

              I just put this together and it is untested other than seeing that it plots a line. I don't know if it is accurate or not. It would be a starting point.

              Dan
              eDanny
              NinjaTrader Ecosystem Vendor - Integrity Traders

              Comment


                #8
                Thank You for your Support and Information Koganam, I appreciate your assistance.
                Joe

                Comment


                  #9
                  Hi Dan,

                  Sorry for my Delayed response, I had my kids this weekend. I Hope you had a Nice weekend.

                  It worked PERFECT, the only thing missing for any one else viewing this post was the following in the Properties Region:

                  [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                  [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                  public DataSeries Plot0
                  {
                  get { return Values[0]; }
                  }

                  I did not know you could build upon a dataseries this way. I think this was a Great work around for DIRECTLY substituting a Enum name into a Function (direct text substitution for a method name).

                  Using this method it only took 800 lines of Code instead of 3500 lines of Code to achieve the same results with my custom indicator I created.

                  This was an EXCELLENT Learning Example for me.

                  Thanks So Much for taking the time to create this code for me, that was Really Nice of you.

                  Joe

                  P.S. Now I know why you named your Company INTEGRITY Traders. I (Think ?) It's Rare to see someone help others with coding, when they also sell there own Indicators.

                  P.S.S. I Have Viewed your Indicators on your Website in the Past and would Recommend them to anyone looking for some Free Indicators and also some Unique Indicators, Especially the Time and Sales Volume Indicators for Small Lots and Big Lots.

                  P.S.S.S This was NOT a Paid Solicitation, Just a Sincere Thank You from a GRATEFUL, Newbie C# Programmer.

                  Comment


                    #10
                    You're welcome.

                    Dan
                    eDanny
                    NinjaTrader Ecosystem Vendor - Integrity Traders

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by algospoke, Yesterday, 06:40 PM
                    2 responses
                    23 views
                    0 likes
                    Last Post algospoke  
                    Started by ghoul, Today, 06:02 PM
                    3 responses
                    14 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    45 views
                    0 likes
                    Last Post jeronymite  
                    Started by Barry Milan, Yesterday, 10:35 PM
                    7 responses
                    21 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by AttiM, 02-14-2024, 05:20 PM
                    10 responses
                    181 views
                    0 likes
                    Last Post jeronymite  
                    Working...
                    X