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

EMAs difference indicator

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

    EMAs difference indicator

    Hi all,

    I´d like to ask u for help... I´m trying to develop indicator, which measures difference between two periods of EMA. Mathematical calculation would be:

    ABS(EMA[x] - EMA[y])

    I would like to have this indicator as a line on my chart with possible coloring - if the line is rising, it should be green, falling should be red and flat should be black.

    Can you help me with this kind of indicator?

    Thanks in advance for any help!

    #2
    Welcome to the forums - best is to work alongside this sample here -



    For the absolute value of the difference you could work with Math.AbsValue in C# / NinjaScript.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand!

      Thank u very much, I´ll try it.

      Comment


        #4
        Hi again,

        I already made the indicator, but only without coloring. I am not able to modify code of the Colored SMA to use with this one. :-( Is there anybody, who can help me with that?

        Comment


          #5
          The Rising / Falling methods used would expect a dataseries to be passed in, so depending on how you structured your code you would need to work with a custom dataseries to achieve this...for ideas you could review the Stochastics Color indicator script posted in our sharing section - http://www.ninjatrader-support2.com/...1&pp=15&page=3
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Hi Bertrand!

            Thanks for patience. My code passed to colored STO seems like this, but can´t be compiled:

            Code:
            // 
            // Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
            // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
            //
            #region Using declarations
            using System;
            using System.Diagnostics;
            using System.Drawing;
            using System.Drawing.Drawing2D;
            using System.ComponentModel;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Data;
            using NinjaTrader.Gui.Chart;
            #endregion
            
            // This namespace holds all indicators and is required. Do not change it.
            namespace NinjaTrader.Indicator
            {
                /// <summary>
                /// This is a sample indicator plotting a SMA in three colors.
                /// </summary>
                [Description("Stochastics plotted with three colors.")]
                [Gui.Design.DisplayName("Stochastics_Color")]
                public class EMADiffColor : Indicator
                {
                    #region Variables
                    private int                    period1    = 7;
                    private int                    period2    = 14;
                    #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 one plot for every color you wish to use.
                        Add(new Plot(new Pen(Color.LimeGreen, 2), PlotStyle.Line, "Rising"));
                        Add(new Plot(new Pen(Color.Red, 2), PlotStyle.Line, "Falling"));
                        Add(new Plot(new Pen(Color.Yellow, 2), PlotStyle.Line, "Neutral"));
                    }
            
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
                        // Checks to make sure we have at least 1 bar before continuing
                        if (CurrentBar < 1)
                            return;
                        
                        // Plot green if the %D is rising
                        // Rising() returns true when the current value is greater than the value of the previous bar.
                        if (Rising((Math.Abs(EMA(Close, 34)[0] - EMA(Close, 204)[0])).D))
                        {
                            // Connects the rising plot segment with the other plots
                            RisingPlot.Set(1, (Math.Abs(EMA(Close, 34)[0] - EMA(Close, 204)[0])).D[1]);                
                            
                            // Adds the new rising plot line segment to the line
                            RisingPlot.Set(Math.Abs(EMA(Close, 34)[0] - EMA(Close, 204)[0]).D[0]);                
                        }
                        
                        // Plot red if the %D is falling
                        // Falling() returns true when the current value is less than the value of the previous bar.
                        else if (Falling(Math.Abs(EMA(Close, 34)[0] - EMA(Close, 204)[0]).D))
                        {
                            // Connects the new falling plot segment with the rest of the line
                            FallingPlot.Set(1, Math.Abs(EMA(Close, 34)[0] - EMA(Close, 204)[0]).D[1]);
                            
                            // Adds the new falling plot line segment to the line
                            FallingPlot.Set(Math.Abs(EMA(Close, 34)[0] - EMA(Close, 204)[0]).D[0]);
                        }
                        
                        // Plot yellow if the %D is neutral
                        else
                        {
                            // Connects the neutral plot segment with the rest of the line
                            NeutralPlot.Set(1, Math.Abs(EMA(Close, 34)[0] - EMA(Close, 204)[0]).D[1]);
                            
                            // Adds the new neutral plot line segment to the line
                            NeutralPlot.Set(Math.Abs(EMA(Close, 34)[0] - EMA(Close, 204)[0]).D[0]);
                        }
                    }
            
                    #region Properties
                    
                    /// <summary>
                    /// Gets the fast K value.
                    /// </summary>
                    
                    // Adds three DataSeries to store the values of the three plots added in Initialize()
                    [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 RisingPlot
                    {
                        get { return Values[1]; }
                    }
            
                    [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 FallingPlot
                    {
                        get { return Values[2]; }
                    }
                    
                    [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 NeutralPlot
                    {
                        get { return Values[3]; }
                    }
                    
                    // Adds an input for the period of the SMA
                    /// <summary>
                    /// </summary>
            
                    // Adds an input for the period of the SMA
                    /// <summary>
                    /// </summary>
                    [Description("Numbers of bars used for calculations")]
                    [Category("Parameters")]
                    public int Period1
                    {
                        get { return period1; }
                        set { period1 = Math.Max(1, value); }
                    }
                    
                    [Description("Smoothing period for SMA")]
                    [Category("Parameters")]
                    public int Period2
                    {
                        get { return period2; }
                        set { period2 = Math.Max(1, value); }
                    }
                    #endregion
                }
            }
            
            #region NinjaScript generated code. Neither change nor remove.
            // This namespace holds all indicators and is required. Do not change it.
            namespace NinjaTrader.Indicator
            {
                public partial class Indicator : IndicatorBase
                {
                    private EMADiffColor[] cacheEMADiffColor = null;
            
                    private static EMADiffColor checkEMADiffColor = new EMADiffColor();
            
                    /// <summary>
                    /// Stochastics plotted with three colors.
                    /// </summary>
                    /// <returns></returns>
                    public EMADiffColor EMADiffColor(int period1, int period2)
                    {
                        return EMADiffColor(Input, period1, period2);
                    }
            
                    /// <summary>
                    /// Stochastics plotted with three colors.
                    /// </summary>
                    /// <returns></returns>
                    public EMADiffColor EMADiffColor(Data.IDataSeries input, int period1, int period2)
                    {
                        checkEMADiffColor.Period1 = period1;
                        period1 = checkEMADiffColor.Period1;
                        checkEMADiffColor.Period2 = period2;
                        period2 = checkEMADiffColor.Period2;
            
                        if (cacheEMADiffColor != null)
                            for (int idx = 0; idx < cacheEMADiffColor.Length; idx++)
                                if (cacheEMADiffColor[idx].Period1 == period1 && cacheEMADiffColor[idx].Period2 == period2 && cacheEMADiffColor[idx].EqualsInput(input))
                                    return cacheEMADiffColor[idx];
            
                        EMADiffColor indicator = new EMADiffColor();
                        indicator.BarsRequired = BarsRequired;
                        indicator.CalculateOnBarClose = CalculateOnBarClose;
                        indicator.Input = input;
                        indicator.Period1 = period1;
                        indicator.Period2 = period2;
                        indicator.SetUp();
            
                        EMADiffColor[] tmp = new EMADiffColor[cacheEMADiffColor == null ? 1 : cacheEMADiffColor.Length + 1];
                        if (cacheEMADiffColor != null)
                            cacheEMADiffColor.CopyTo(tmp, 0);
                        tmp[tmp.Length - 1] = indicator;
                        cacheEMADiffColor = tmp;
                        Indicators.Add(indicator);
            
                        return indicator;
                    }
            
                }
            }
            
            // This namespace holds all market analyzer column definitions and is required. Do not change it.
            namespace NinjaTrader.MarketAnalyzer
            {
                public partial class Column : ColumnBase
                {
                    /// <summary>
                    /// Stochastics plotted with three colors.
                    /// </summary>
                    /// <returns></returns>
                    [Gui.Design.WizardCondition("Indicator")]
                    public Indicator.EMADiffColor EMADiffColor(int period1, int period2)
                    {
                        return _indicator.EMADiffColor(Input, period1, period2);
                    }
            
                    /// <summary>
                    /// Stochastics plotted with three colors.
                    /// </summary>
                    /// <returns></returns>
                    public Indicator.EMADiffColor EMADiffColor(Data.IDataSeries input, int period1, int period2)
                    {
                        return _indicator.EMADiffColor(input, period1, period2);
                    }
            
                }
            }
            
            // This namespace holds all strategies and is required. Do not change it.
            namespace NinjaTrader.Strategy
            {
                public partial class Strategy : StrategyBase
                {
                    /// <summary>
                    /// Stochastics plotted with three colors.
                    /// </summary>
                    /// <returns></returns>
                    [Gui.Design.WizardCondition("Indicator")]
                    public Indicator.EMADiffColor EMADiffColor(int period1, int period2)
                    {
                        return _indicator.EMADiffColor(Input, period1, period2);
                    }
            
                    /// <summary>
                    /// Stochastics plotted with three colors.
                    /// </summary>
                    /// <returns></returns>
                    public Indicator.EMADiffColor EMADiffColor(Data.IDataSeries input, int period1, int period2)
                    {
                        if (InInitialize && input == null)
                            throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
            
                        return _indicator.EMADiffColor(input, period1, period2);
                    }
            
                }
            }
            #endregion
            Why this doesn´t work? What is the problem? Thanks in advance...

            Comment


              #7
              tradinger, Rising and Falling expect a dataseries to be passed into them, not a double as in your case (which Math.Abs would return) -



              You would need to create a custom dataseries and pass in the MathAbs logic you have with the Set() method - http://www.ninjatrader-support.com/H...iesObject.html

              Finally you can pass this resulting custom data series into the Rising and Falling methods to do the coloring aspects.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Bertrand,

                thank you very much for your help. I am still not able to solve that, but it doesn´t matter, I´ll try to find some programmer somewhere. This is too difficult for me.

                Thank you anyway and best wishes!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by drewski1980, Today, 08:24 AM
                0 responses
                3 views
                0 likes
                Last Post drewski1980  
                Started by rdtdale, Yesterday, 01:02 PM
                2 responses
                16 views
                0 likes
                Last Post rdtdale
                by rdtdale
                 
                Started by TradeSaber, Today, 07:18 AM
                0 responses
                7 views
                0 likes
                Last Post TradeSaber  
                Started by PaulMohn, Today, 05:00 AM
                0 responses
                10 views
                0 likes
                Last Post PaulMohn  
                Started by ZenCortexAuCost, Today, 04:24 AM
                0 responses
                6 views
                0 likes
                Last Post ZenCortexAuCost  
                Working...
                X