Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Tick difference between EMA and price

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

    Tick difference between EMA and price

    Hello,

    I am trying to write simple indicator, that will display simple whole number next to EMA in a chart. This number should be difference between last price and current value of EMA.

    I am using EMA 34 and EMA 204. If price is (TF) i.e. 610.1 and EMA 34 is 612.2, there should be written 21 next to the line. EMA 204 is 608.7 and next to the line will be written 14.

    How to make it? I am trying to use Print() function as well as DrawText(), but without any success... Can you help me please?

    Thanks in advance,

    Tom

    #2
    Tom, welcome to the forums - you can try for example something like this snippet and adapt it to your needs -

    Code:
     
    double myDiff = Instrument.MasterInstrument.Round2TickSize(Math.Abs(Close[0] - EMA(Close, 34)[0]));
     
     
    DrawTextFixed("myTag", myDiff.ToString(), TextPosition.Center);
    Last edited by NinjaTrader_Bertrand; 10-15-2009, 06:35 AM.

    Comment


      #3
      Hello Bertrand!

      Thank you very much for your help. It works great! I have only one more problem - how can I adjust text position on the chart to have it exactly right next to the EMA? I.e. I am using two EMAs and I would like to have these numbers exactly next (one bar next) to the current point of EMA in the chart. Is this possible? I think it is not possible with TextPosition.xxx argument...?

      Thank you for patience.

      Comment


        #4
        You're welcome, great this helps - then use DrawText for more control -

        Code:
         
        DrawText("myTag", myDiff.ToString(), 0, EMA(Close, 34)[0] + TickSize, Color.Blue);

        Comment


          #5
          Bertrand,

          thank you one more time, that's it!

          Comment


            #6
            I have one more question... I would like to move text output 2 bars back on the chart (= I won't to write it exactly upon the last bar, but 2 bars ago).

            If I write:

            DrawText("myTag", myDiff.ToString(), 2, EMA(Close, 34)[0] + TickSize, Color.Blue);

            it doesn't work... So how to move this text output on X axe back?

            Thanks in advance for any help.

            Comment


              #7
              Hello,

              I'm not sure what you want exactly but the "2" in your code shifts the text back two bars. If you want the text on the first bar use "0".

              If you want the current value change whatever value is going into "myDiff":
              myDiff.ToString()

              ...so that is uses a "[0]" bar references (current bar references).
              DenNinjaTrader Customer Service

              Comment


                #8
                Hello Ben

                yes, that's exactly what I need - shift the text back two bars. But it doesn't work. :-) If there is "0", it is printed upon the last bar, it works fine. But if I write 2 for shifting the text 2 bars ago, it doesn't work and no text is printed. Any idea for solution?

                Thanks in advance.

                Comment


                  #9
                  Hello,

                  Paste this into your SampleMACrossOver Strategy in the OnBarUpdate() code block:
                  DrawText("myTag", "mytext", 2, EMA(Close, 34)[0] + TickSize, Color.Blue);

                  It works for me.
                  DenNinjaTrader Customer Service

                  Comment


                    #10
                    Hello Ben,

                    thank you very much, but it doesn't work for me. We differ only in using vars:

                    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.Gui.Chart;
                    #endregion
                    
                    namespace NinjaTrader.Indicator
                    {
                        [Description("Measures number of ticks between price and EMA")]
                        public class EMAdif : Indicator
                        {
                            #region Variables
                                private int period = 34; // Default setting for EMA Period
                                private int multiplier = 10; // Multiplier for tick counter.
                            #endregion
                    
                            protected override void Initialize()
                            {   
                                CalculateOnBarClose    = false;
                                Overlay        = true;
                                PriceTypeSupported    = false;
                            }
                    
                            protected override void OnBarUpdate()
                            {
                            double myDiff = (Instrument.MasterInstrument.Round2TickSize(Math.Abs(Close[0] - EMA(Close, period)[0]))) * multiplier;
                            DrawText("myTag", myDiff.ToString(), 0, EMA(Close, period)[0] + TickSize, Color.Black);
                            }
                    
                            #region Properties
                        [Description("Period value setting")]
                            [Category("Parameters")]
                            public int Period
                            {
                                get { return period; }
                                set { period = Math.Max(1, value); }
                            }
                            
                        [Description("Multiplier value setting")]
                            [Category("Parameters")]
                            public int Multiplier
                            {
                                get { return multiplier; }
                                set { multiplier = Math.Max(1, value); }
                            }
                            #endregion
                        }
                    }
                    What's wrong?

                    Comment


                      #11
                      Please review this link here - http://www.ninjatrader-support2.com/...ead.php?t=3170

                      and then ensure you add a check like this to your indicator OnBarUpdate()

                      Comment


                        #12
                        Thank you, Bertrand!

                        I am not able to adapt this into code bellow, but it doesn't matter. However thank you very much for your help and time.

                        Best wishes.

                        Comment


                          #13
                          Originally posted by cz_trader View Post
                          Thank you, Bertrand!

                          I am not able to adapt this into code bellow, but it doesn't matter. However thank you very much for your help and time.

                          Best wishes.
                          I think Bertrand is saying that you need to code like this:

                          protected override void OnBarUpdate()
                          {

                          if ( CurrentBar < period) return; // this ensures you have enough bars for the calculation... if you don't this forces an exit; the code will not work otherwise

                          double myDiff = (Instrument.MasterInstrument.Round2TickSize(Math.A bs(Close[0] - EMA(Close, period)[0]))) * multiplier;
                          DrawText("myTag", myDiff.ToString(), 0, EMA(Close, period)[0] + TickSize, Color.Black);
                          }

                          Comment


                            #14
                            Saroj,

                            thank you very much, that's it! It works perfectly fine. Thank you one more time.

                            Best wishes!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            605 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            351 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            105 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            560 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            561 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X