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

HMA modification - help with code

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

    HMA modification - help with code

    Hi Folks

    HMA is, in my humble view, the best MA there is out there. Load it! Use it!

    The good news is it's there with NT7 (and I guess NT8).

    I've had a peek at the code and this is it:

    double value1 = 2 * WMA(Inputs[0], (int)(Period / 2))[0];
    double value2 = WMA(Inputs[0], Period)[0];
    diffSeries.Set(value1 - value2);

    Value.Set(WMA(diffSeries, (int) Math.Sqrt(Period))[0]);


    Alan Hull has done a great job in so few lines of code (a bit longer if you include the WMA, but that isn't complex). My gratitude to him.

    There is, I believe, a tiny section that could be 'upgraded'.

    In the final line, again below, please look at the section in red:

    Value.Set(WMA(diffSeries, (int) Math.Sqrt(Period))[0]);

    In NT7 (and obviously C# - and as it happens, Excel), (int) x gives the highest integer lower than or equal to the double (decimal) x (or floor x ). So:

    (int) 6 = floor 6 = 6;
    (int) 5.75 = floor 5.75 = 5;
    (int) 5.25 = floor 5.25 = 5;


    But what makes sense mathematically is for this section to give the rounded square root, not floor(square root).

    So I've 'Saved As...' HMA to make me own editable script and I've tried to put something like:

    Value.Set(WMA(diffSeries, Math.Round( Sqrt(Period))[0] ) );

    I've tried a whole combination of 'int's and 'double's but the compiler likes none of them!

    Could someone please tell how to to accomplish this!

    Many thanks in advance.
    Last edited by arbuthnot; 05-19-2015, 11:15 AM.

    #2
    Hello,

    Your solution looks good, and it works on my end. The only thing that may be throwing you off is the variable type of Period. Is Period a double in your script, or is it an int? If it is an int, then you may be calling the overload for Math.Round that takes ints, which should then produce an int result. Can you tell me what kind of values you are getting that are letting you know it's not working?

    When I tested with the code below, I received results as a double, rounded to two decimal places:

    Code:
    double i = 3670;
    double x = Math.Round(Math.Sqrt(i),2);
    Print(x);
    I look forward to your reply.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Thanks, Dave.

      I've copied below the complete adjusted code (changed 'Period' to a double - thanks for the suggestion).

      However, I'm getting these compiling errors:

      Cannot apply indexing with [] to an expression of type 'double'

      The best overload method for ... has some invalid arguments

      Argument '1': cannot convert from NT WMA to type 'double'


      Thanks in advance for your further advice with this.

      This is the code:

      #region Variables
      private double period = 21;
      private DataSeries diffSeries;
      #endregion

      protected override void Initialize()
      {
      Add(new Plot(new Pen(Color.Green, 6), PlotStyle.Line, "HLine01"));

      Overlay = true; // Plots the indicator on top of price
      diffSeries = new DataSeries(this);
      }

      protected override void OnBarUpdate()
      {
      double value1 = 2 * WMA(Inputs[0], (int)(Period / 2))[0];
      double value2 = WMA(Inputs[0], (int) Period)[0];
      diffSeries.Set(value1 - value2);

      Value.Set(WMA(diffSeries, Math.Round( Math.Sqrt(Period))[0] ) );
      }

      #region Properties

      [Description("Number of bars used for calculation")]
      [GridCategory("Parameters")]
      public double Period
      {
      get { return period; }
      set { period = Math.Max(1, value); }
      }

      #endregion

      Comment


        #4
        Originally posted by arbuthnot View Post

        The best overload method for ... has some invalid arguments
        The key is going to be the method name referenced in this error. Is there any way you can expand this column and tell me what method this is referring to?
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Sorry for not quoting it fully first time:

          "The best overload method for 'NinjaTrader.Data.DataSeries.Set(double)' has some invalid arguments"

          Any ideas, Dave, as to how to proceed now?

          Comment


            #6
            Hello,

            Thank you. I see what's going on now. It looks like you need to move one closing parenthesis over to the right of the bar index in the line below:

            Value.Set(WMA(diffSeries, Math.Round( Math.Sqrt(Period))[0] ) );

            Please try changing it to the following:

            Value.Set(WMA(diffSeries, Math.Round( Math.Sqrt(Period)))[0] );

            As it is right now, it looks like the index is not being applied to WMA, but rather to Math.Round, so the WMA is being passed in as a data series, rather than as a double.
            Dave I.NinjaTrader Product Management

            Comment


              #7
              Thanks very much, Dave. Great detective work!

              It then compiled but only after I'd added (int) before Math.Round. This is OK as Math.Round only produces integers and

              (int) Math.Round = floor ( Math.Round )

              in all cases.

              If anyone reading this thread would like to comment as to whether or not this is a valid 'update' for HMA, I'd welcome comments.

              I think it necessarily improves it, as, half the time, any random real number x > floor (x) + ˝. So rounding the square root in this code should make it more accurate - or, better put, consistent - half the time.

              Cheers, Dave, for your help with this.

              Comment


                #8
                Originally posted by arbuthnot View Post
                Sorry for not quoting it fully first time:

                "The best overload method for 'NinjaTrader.Data.DataSeries.Set(double)' has some invalid arguments"

                Any ideas, Dave, as to how to proceed now?
                That is because you have simply not quite understood what was written. You are calling the WMA(). The WMA calling signature requires an IDataSeries and an int, or just an int. The particular overload involved in this case requires the 2 parameters. WMA is itself a IDataSeries, and so is indexed. An index must be an int, not a double.

                The compiler is telling you that you cannot use a double as an index.

                Comment


                  #9
                  I use HMA (and Jurik's JMA, which is not free but is a little better than HMA). I'd like to test your improvement but I think I'd have to piece together the indicator from the various posts here to get to the final version. Can you post "HMAarb" so I can import it and give it a run?

                  Also, since I'm a newbie to NT and am not proficient in coding yet, can you suggest a way to color the HMA plot so the color changes when the slope goes neg to pos and pos to neg?

                  Thanks!

                  Comment


                    #10
                    Hello gdstuart,

                    I was not able to locate the HMAarb indicator on our Indicators forum or on the web, unfortunately. Do you know someone who uses this? If so, they may be able to export it for you, so that you can import it on your end.

                    The standard way to change the color of a plot like that is to actually use two separate plots, each with a different color. Then, when a certain condition is met, you can stop one of the plots from drawing, and set the other plot to draw, and vice versa for the opposite condition. Even though it is two plots, it will appear to be one plot with a changing color on the chart.

                    The post linked below includes an attachment named HMA_2C.zip which will show you exactly how to accomplish this. The code in that attachment is pretty simple and straightforward, but feel free to let me know if you have any questions:

                    http://www.ninjatrader.com/support/f...ad.php?t=48268
                    Dave I.NinjaTrader Product Management

                    Comment


                      #11
                      I've attached my modified HMA indicator showing rising/falling colors.

                      When importing, if you're asked if you want to import any other files, it's essential to say 'no'!

                      Hope this helps.
                      Attached Files

                      Comment


                        #12
                        Dave,

                        I made up the name HMAarb, as an example of what arbuthnot might call his modified HMA. He understood and as you can see, he posted his zip file for me to test with.

                        Thanks for the reference to HMA_2C. I'll check it out.

                        Geoff

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by dmking, 11-12-2019, 12:31 PM
                        4 responses
                        4,140 views
                        0 likes
                        Last Post jasonw
                        by jasonw
                         
                        Started by roblogic, Today, 04:31 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post roblogic  
                        Started by morrnel, 05-12-2024, 06:07 PM
                        4 responses
                        56 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by xepher101, 05-10-2024, 12:19 PM
                        6 responses
                        71 views
                        0 likes
                        Last Post xepher101  
                        Started by gbourque, Today, 04:11 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post gbourque  
                        Working...
                        X