Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

modding the standard ema

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

    modding the standard ema

    Hi can you help me mod the standard ema very slightly?

    I would like it so that if price goes above the ema it colours it green and if the price is below the ema it changes it to red..

    I only want it colouring the part the candles are above or below not the whole ema so it would look like this:



    Its just the standard Ninja EMA i would like to mod but i have pasted the code below anyway.. thanks for the help..

    Code:
    // 
    // Copyright (C) 2006, 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.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    	/// <summary>
    	/// Exponential Moving Average. The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
    	/// </summary>
    	[Description("The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.")]
    	public class EMA : Indicator
    	{
    		#region Variables
    		private int			period		= 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(new Plot(Color.Orange, "EMA"));
    
    			Overlay				= true;
    		}
    		
    		/// <summary>
    		/// Called on each bar update event (incoming tick)
    		/// </summary>
    		protected override void OnBarUpdate()
    		{
    			Value.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Value[1]);
    		}
    
    		#region Properties
    		/// <summary>
    		/// </summary>
    		[Description("Numbers of bars used for calculations")]
    		[GridCategory("Parameters")]
    		public int Period
    		{
    			get { return period; }
    			set { period = 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 EMA[] cacheEMA = null;
    
            private static EMA checkEMA = new EMA();
    
            /// <summary>
            /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
            /// </summary>
            /// <returns></returns>
            public EMA EMA(int period)
            {
                return EMA(Input, period);
            }
    
            /// <summary>
            /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
            /// </summary>
            /// <returns></returns>
            public EMA EMA(Data.IDataSeries input, int period)
            {
                if (cacheEMA != null)
                    for (int idx = 0; idx < cacheEMA.Length; idx++)
                        if (cacheEMA[idx].Period == period && cacheEMA[idx].EqualsInput(input))
                            return cacheEMA[idx];
    
                lock (checkEMA)
                {
                    checkEMA.Period = period;
                    period = checkEMA.Period;
    
                    if (cacheEMA != null)
                        for (int idx = 0; idx < cacheEMA.Length; idx++)
                            if (cacheEMA[idx].Period == period && cacheEMA[idx].EqualsInput(input))
                                return cacheEMA[idx];
    
                    EMA indicator = new EMA();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    indicator.Period = period;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    EMA[] tmp = new EMA[cacheEMA == null ? 1 : cacheEMA.Length + 1];
                    if (cacheEMA != null)
                        cacheEMA.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cacheEMA = tmp;
                    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>
            /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.EMA EMA(int period)
            {
                return _indicator.EMA(Input, period);
            }
    
            /// <summary>
            /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
            /// </summary>
            /// <returns></returns>
            public Indicator.EMA EMA(Data.IDataSeries input, int period)
            {
                return _indicator.EMA(input, period);
            }
        }
    }
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        public partial class Strategy : StrategyBase
        {
            /// <summary>
            /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.EMA EMA(int period)
            {
                return _indicator.EMA(Input, period);
            }
    
            /// <summary>
            /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
            /// </summary>
            /// <returns></returns>
            public Indicator.EMA EMA(Data.IDataSeries input, int period)
            {
                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.EMA(input, period);
            }
        }
    }
    #endregion

    #2
    Hello Lucyjoy,
    Welcome to the forum and I am happy to assist you.

    You can use the PlotColors method to color the plots.


    A sample code will be like
    protected override void OnBarUpdate()
    Code:
    {
    	Value.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Value[1]);
    	
    	if (CurrentBar < 1) return;
    	if (Value[0] > Value[1])
    		this.PlotColors[0][0] = Color.Green;
    	else this.PlotColors[0][0] = Color.Red;
    }
    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hi

      Thanks for the fast reply, im afraid im not really a coder and i didnt really understand where to put this code, im just a trader and find a visual ema is easier for me to see price at a glance..

      Would you be able to mod the code for me or tell me where to put the code you made me?

      I really appriciate this and how fast you responded..

      Thanks

      Lucy

      Comment


        #4
        Sorry

        I was being thick

        I worked it out from your code..

        Thanks

        Lucy

        Comment


          #5
          Hello lucyjoy,
          Glad to know everything is working fine at your end.

          Please let me know if I can assist you any further.
          JoydeepNinjaTrader Customer Service

          Comment


            #6
            Hi

            could you help me make another mod please?

            I would like to plot 3 of these at the same time from the same indicator i understand ill need to add

            private int period1 = 8;
            private int period2 = 50;
            private int period3 = 200;

            but then im stuck lol

            please can you help?

            Thanks

            Lucy

            Comment


              #7
              Hello lucyjoy,
              If you want to plot 3 EMA's from the same indicator then beside the period value you have to add the plots too.

              In Initialze section of the code
              Code:
              protected override void Initialize()
              {
              	Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot8"));
                          Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Plot50"));
                          Add(new Plot(Color.FromKnownColor(KnownColor.DarkViolet), PlotStyle.Line, "Plot200"));
                          Overlay				= true;
              }
              In OnBarUpdate
              Code:
              Plot8.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period1)) + (1 - (2.0 / (1 + Period1))) * Plot8[1]);
                          Plot50.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period2)) + (1 - (2.0 / (1 + Period2))) * Plot50[1]);
              			Plot200.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period3)) + (1 - (2.0 / (1 + Period3))) * Plot200[1]);
              In Protperties
              Code:
              [Browsable(false)]
              [XmlIgnore()]
              public DataSeries Plot8
              {
              	get { return Values[0]; }
              }
              
              [Browsable(false)]
              [XmlIgnore()]
              public DataSeries Plot50
              {
              	get { return Values[1]; }
              }
              
              [Browsable(false)]
              [XmlIgnore()]
              public DataSeries Plot200
              {
              	get { return Values[2]; }
              }
              Please let me know if I can assist you any further.
              Last edited by NinjaTrader_Joydeep; 05-24-2012, 04:06 AM.
              JoydeepNinjaTrader Customer Service

              Comment


                #8
                Hi

                Thanks so much for you fast reponces, im nearly getting there with my first indicator to trade with... So happy..

                Couple of problems though!!

                ill go through 1 at a time..

                first problem i have is the colour changing only works on 1 of the lines.. which is the 8ema.

                I modded the code as i thought!

                Code:
                protected override void OnBarUpdate()
                		{
                			Plot8.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period1)) + (1 - (2.0 / (1 + Period1))) * Plot8[1]);
                            Plot50.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period2)) + (1 - (2.0 / (1 + Period2))) * Plot50[1]);
                			
                				if (CurrentBar < 1) return;
                	if (Plot8[0] > Plot8[1])
                		this.PlotColors[0][0] = Color.Green;
                	else this.PlotColors[0][0] = Color.Red;
                			
                					if (CurrentBar < 1) return;
                	if (Plot50[0] > Plot50[1])
                		this.PlotColors[0][0] = Color.Green;
                	else this.PlotColors[0][0] = Color.Red;
                	
                		}
                I have also lost the ability to edit the inputs for the periods...

                this was erroring in the properties box so i removed it...

                Code:
                [Description("Numbers of bars used for calculations")]
                		[GridCategory("Parameters")]
                		public int Period
                		{
                			get { return period; }
                			set { period = Math.Max(1, value); }
                		}
                Again thanks so much for helping me....
                Last edited by lucyjoy; 05-25-2012, 02:28 AM.

                Comment


                  #9
                  Hello lucyjoy,
                  PlotColors[0][0] will color the first plot only

                  To make changes to the other plots please use the following code
                  Code:
                  PlotColors[0][0] = Color.Blue;
                  PlotColors[1][0] = Color.Green;
                  PlotColors[2][0] = Color.Gold;

                  To edit the period input please append the below codes in the Properties region
                  Code:
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int Period1
                  {
                      get { return period1; }
                      set { period1 = Math.Max(1, value); }
                  }
                  
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int Period2
                  {
                      get { return period2; }
                      set { period2 = Math.Max(1, value); }
                  }
                  
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int Period3
                  {
                      get { return period3; }
                      set { period3 = Math.Max(1, value); }
                  }
                  Please let me know if I can assist you any further.
                  JoydeepNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  601 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  347 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  103 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  559 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  558 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X