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

Help with a simple formula - EMA of midpoint

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

    Help with a simple formula - EMA of midpoint

    Hi,

    Would appreciate some urgent help with the below. I just cannot get the script to compile successfully and despite looking at the help etc I still cannot resolve what the problem is.

    I want an x period EMA of
    (Open[0]+High[0]+Low[0]+Close[0])/4

    now when I use the above as a plot in another indicator it works fine.

    I also have my own version of x period EMA of closing price that I have colour coded for rising, falling - it works fine.

    I am trying to create an x period EMA of (Open[0]+High[0]+Low[0]+Close[0])/4 and I just cannot come up with code that will pass the compiler.

    Here is code that works:

    Value.Set(EMA(Close,myInput0) [0]); // works fine with O, H, L or C

    I want to replace Close with (Open+High+Low+Close)/4

    Totally stumped.


    #2
    Created a formula result as input to EMA and that did not work

    I tried inputting the result of a formula to the EMA method and that did work either.

    Looks like maybe EMA can only accept open, high, low or close as input price - is that correct?

    Looked at the std EMA code in Ninja:

    Value.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Value[1]);

    The above code must default to close somehow as it is not specified.

    I can create my own EMA indicator using the above code but what do I need to change the code to so that it uses (O+H+L+C)/4 rather than Close?



    Comment


      #3
      EMA takes an iDataSeries. Adding each value together does not result in an iDataSeries. It result in a number.





      You need to create your own DataSeries.

      The sample here shows a high-low computation.




      Create one of these with your formula, and send that into the EMA.

      Comment


        #4
        Thanks for the assist sledge.

        Skiguy, a very simple script attached that demonstrates how to create the needed series and then passing that to the EMA to calculate your custom output. Period is set to 20 hard, but could of course be customized as user input just like in the default EMA study we ship.
        Attached Files
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thanks sledge that was very helpful.

          Comment


            #6
            Originally posted by sledge View Post
            EMA takes an iDataSeries. Adding each value together does not result in an iDataSeries. It result in a number.





            You need to create your own DataSeries.

            The sample here shows a high-low computation.




            Create one of these with your formula, and send that into the EMA.
            Thank you for such a detailed response and example.

            For future viewers take note that this is a good example of creating a data series and using it as input to plots etc.

            Comment


              #7
              Future viewers may find the following useful.

              As part of trying to resolve this for myself I came across the following:


              Definition
              Type of price data such as open, high, low, etc...

              Property Value
              This property returns a PriceType value of either:

              PriceType.Close
              PriceType.High
              PriceType.Low
              PriceType.Median
              PriceType.Open
              PriceType.Typical


              which can be used as input to the EMA method.

              Typical would be close to what I was looking to achieve.

              Comment


                #8
                Midpoint EMA

                But to come back to your original formula:

                You have two options to get what you want.

                Either you add a DataSeries object to your EMA

                Code:
                region Variables:
                private double period = 14;
                private DataSeries midpoint;
                
                protected override void Initialize()
                {
                     Add(new Plot(Color.Orange, "EMA"));
                     Overlay  = true;
                     midpoint = new DataSeries(this);
                }
                
                protected override void OnBarUpdate()
                {
                      midpoint.Set(0.25 * (Open[0] + High[0] + Low[0] + Close[0]));
                      Value.Set(CurrentBar == 0 ? midpoint[0] : midpoint[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Value[1]);
                }
                or you simply code a Midpoint indicator and use it as input series for the EMA, when you apply it to the chart.

                I have attached a simple midpoint indicator and used it to generate the midpoint EMA, which of course is smoother than an EMA generated from the closes of the typical prices.
                Attached Files
                Last edited by Harry; 06-03-2014, 10:57 AM.

                Comment


                  #9
                  Note to NinjaTrader Developers

                  For NinjaTrader 8, please add Midpoint as a PriceType.

                  Typical = (High + Low + Close) / 3
                  Weighted = (High + Low + 2*Close) / 4
                  Midpoint = (Open + High + Low + Close) / 4

                  The midpoint is required by many other indicators, it would be nice to have the midpoint available as a preset PriceType.

                  Comment


                    #10
                    Great idea Harry.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by pmachiraju, 11-01-2023, 04:46 AM
                    8 responses
                    148 views
                    0 likes
                    Last Post rehmans
                    by rehmans
                     
                    Started by mattbsea, Today, 05:44 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post mattbsea  
                    Started by RideMe, 04-07-2024, 04:54 PM
                    6 responses
                    33 views
                    0 likes
                    Last Post RideMe
                    by RideMe
                     
                    Started by tkaboris, Today, 05:13 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post tkaboris  
                    Started by GussJ, 03-04-2020, 03:11 PM
                    16 responses
                    3,282 views
                    0 likes
                    Last Post Leafcutter  
                    Working...
                    X