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

Smooth MACD line

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

    Smooth MACD line

    Hello All,

    I have recently moved to TS to NT and NT coding looks very foreign to me. While I continue to learn the language, can someone please help me with a "MACD Indicator i.e line & signal line"...

    1) That has MACD line (only MACD line) smoothened with 3 period EMA. Only the MACD line and not for the signal line.

    2) And I am not looking for any Histogram.

    Thanks a lot.
    Last edited by Sam7768; 11-30-2009, 09:35 PM.

    #2
    Welcome to our forums, I would suggest you step through those -



    For the code, you could use something like this -

    Code:
     
    double mySmoothMACD = EMA(MACD(Close, 10, 20, 1), 3)[0];
    or this -

    Code:
     
    double mySmoothMACD = MACD(Close, 10, 20, 3)[0];
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thank you very much NT Bertrand.

      In the below code, where to make the change?

      Thanks a lot for your support.

      Rgds,

      //
      // 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>
      /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
      /// </summary>
      [Description("The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.")]
      public class MACD : Indicator
      {
      #region Variables
      private int fast = 12;
      private int slow = 26;
      private int smooth = 9;
      private DataSeries fastEma;
      private DataSeries slowEma;
      #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.Green, "Macd"));
      Add(new Plot(Color.DarkViolet, "Avg"));
      Add(new Plot(new Pen(Color.Navy, 2), PlotStyle.Bar, "Diff"));

      Add(new Line(Color.DarkGray, 0, "Zero line"));

      fastEma = new DataSeries(this);
      slowEma = new DataSeries(this);
      }

      /// <summary>
      /// Calculates the indicator value(s) at the current index.
      /// </summary>
      protected override void OnBarUpdate()
      {
      if (CurrentBar == 0)
      {
      fastEma.Set(Input[0]);
      slowEma.Set(Input[0]);
      Value.Set(0);
      Avg.Set(0);
      Diff.Set(0);
      }
      else
      {
      fastEma.Set((2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]);
      slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);

      double macd = fastEma[0] - slowEma[0];
      double macdAvg = (2.0 / (1 + Smooth)) * macd + (1 - (2.0 / (1 + Smooth))) * Avg[1];

      Value.Set(macd);
      Avg.Set(macdAvg);
      Diff.Set(macd - macdAvg);
      }
      }

      #region Properties
      /// <summary>
      /// </summary>
      [Browsable(false)]
      [XmlIgnore()]
      public DataSeries Avg
      {
      get { return Values[1]; }
      }

      /// <summary>
      /// </summary>
      [Browsable(false)]
      [XmlIgnore()]
      public DataSeries Default
      {
      get { return Values[0]; }
      }

      /// <summary>
      /// </summary>
      [Browsable(false)]
      [XmlIgnore()]
      public DataSeries Diff
      {
      get { return Values[2]; }
      }

      /// <summary>
      /// </summary>
      [Description("Number of bars for fast EMA")]
      [Category("Parameters")]
      public int Fast
      {
      get { return fast; }
      set { fast = Math.Max(1, value); }
      }

      /// <summary>
      /// </summary>
      [Description("Number of bars for slow EMA")]
      [Category("Parameters")]
      public int Slow
      {
      get { return slow; }
      set { slow = Math.Max(1, value); }
      }

      /// <summary>
      /// </summary>
      [Description("Number of bars for smoothing")]
      [Category("Parameters")]
      public int Smooth
      {
      get { return smooth; }
      set { smooth = 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 MACD[] cacheMACD = null;

      private static MACD checkMACD = new MACD();

      /// <summary>
      /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
      /// </summary>
      /// <returns></returns>
      public MACD MACD(int fast, int slow, int smooth)
      {
      return MACD(Input, fast, slow, smooth);
      }

      /// <summary>
      /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
      /// </summary>
      /// <returns></returns>
      public MACD MACD(Data.IDataSeries input, int fast, int slow, int smooth)
      {
      checkMACD.Fast = fast;
      fast = checkMACD.Fast;
      checkMACD.Slow = slow;
      slow = checkMACD.Slow;
      checkMACD.Smooth = smooth;
      smooth = checkMACD.Smooth;

      if (cacheMACD != null)
      for (int idx = 0; idx < cacheMACD.Length; idx++)
      if (cacheMACD[idx].Fast == fast && cacheMACD[idx].Slow == slow && cacheMACD[idx].Smooth == smooth && cacheMACD[idx].EqualsInput(input))
      return cacheMACD[idx];

      MACD indicator = new MACD();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      indicator.Input = input;
      indicator.Fast = fast;
      indicator.Slow = slow;
      indicator.Smooth = smooth;
      indicator.SetUp();

      MACD[] tmp = new MACD[cacheMACD == null ? 1 : cacheMACD.Length + 1];
      if (cacheMACD != null)
      cacheMACD.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cacheMACD = 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>
      /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.MACD MACD(int fast, int slow, int smooth)
      {
      return _indicator.MACD(Input, fast, slow, smooth);
      }

      /// <summary>
      /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
      /// </summary>
      /// <returns></returns>
      public Indicator.MACD MACD(Data.IDataSeries input, int fast, int slow, int smooth)
      {
      return _indicator.MACD(input, fast, slow, smooth);
      }

      }
      }

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      public partial class Strategy : StrategyBase
      {
      /// <summary>
      /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.MACD MACD(int fast, int slow, int smooth)
      {
      return _indicator.MACD(Input, fast, slow, smooth);
      }

      /// <summary>
      /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
      /// </summary>
      /// <returns></returns>
      public Indicator.MACD MACD(Data.IDataSeries input, int fast, int slow, int smooth)
      {
      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.MACD(input, fast, slow, smooth);
      }

      }
      }
      #endregion

      Comment


        #4
        For me second suggestion, you could just change the smooth input to 3 - this would give you the MACD value and it's 3 period EMA then, the histogram you could set to transparent color to not display it's values.
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by usazencort, Today, 01:16 AM
        0 responses
        1 view
        0 likes
        Last Post usazencort  
        Started by kaywai, 09-01-2023, 08:44 PM
        5 responses
        603 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by xiinteractive, 04-09-2024, 08:08 AM
        6 responses
        22 views
        0 likes
        Last Post xiinteractive  
        Started by Pattontje, Yesterday, 02:10 PM
        2 responses
        21 views
        0 likes
        Last Post Pattontje  
        Started by flybuzz, 04-21-2024, 04:07 PM
        17 responses
        230 views
        0 likes
        Last Post TradingLoss  
        Working...
        X