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

MACD AND AVG up down colors

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

    MACD AND AVG up down colors

    Hello everybody. I'm totally clueless when it comes to coding. What I'm trying to do is to modify currently posted MACD UpDown indicator and also ad color change to AVG line for up and down. Made some changes but I'm getting errors. Can someone please help? Can you please tell me what I need to change and what lines. Here's the code I've modified so far. How do I need to define Plot Avg lines in raising and falling lines (last lines of code). And any other changes I would need to do.

    Code:
    public class MACDUpDownPlus : Indicator
    	{
    		private Series<double> fastEma;
    		private Series<double> slowEma;
    		
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description									= @"Enter the description for your new custom Indicator here.";
    				Name										= "MACDUpDownPlus";
    				Calculate									= Calculate.OnPriceChange;
    				IsOverlay									= true;
    				DisplayInDataBox							= true;
    				DrawOnPricePanel							= true;
    				DrawHorizontalGridLines						= true;
    				DrawVerticalGridLines						= true;
    				PaintPriceMarkers							= true;
    				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
    				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
    				//See Help Guide for additional information.
    				IsSuspendedWhileInactive					= true;
    				Fast = 12;
    				Slow = 26;
    				Smooth = 9;
    				MACDUP = Brushes.Green;
    				MACDDOWN = Brushes.Red;
    				AVGUP = Brushes.Magenta;
    				AVGDOWN = Brushes.Black;
    				AddPlot(Brushes.Green, "Macd");
    				AddPlot(Brushes.DarkViolet, "Avg");
    				AddPlot(new Stroke(Brushes.Navy, 2), PlotStyle.Bar, "Diff");
    				AddLine(Brushes.DarkGray, 0, "Zero line");
    			}
    			else if (State == State.Configure)
    			{
    			}
    			else if (State == State.DataLoaded)
    			{
    				fastEma = new Series<double>(this);
    				slowEma = new Series<double>(this);
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBar == 0)
    			{
    				fastEma[0] = Input[0];
    				slowEma[0] = Input[0];
    				Value[0] = 0;
    				Avg[0] = 0;
    				Diff[0] = 0;
    			}
    			else
    			{
    				fastEma[0] = (2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1];
    				slowEma[0] = (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[0] = macd;
    				Avg[0] = macdAvg;
    				Diff[0] = macd - macdAvg;
    			}
    
    			// Plots MACD color when rising or falling
    			if (IsRising(MACD(Fast, Slow, Smooth)))
    			{
    				PlotBrushes[0][0] = MACDUP;
    			}
    			else if (IsFalling(MACD(Fast, Slow, Smooth)))
    			{
    				PlotBrushes[0][0] = MACDDOWN;
    			}
    			
    			// Plots AVG color when rising or falling
    			if (IsRising(AVG(Fast, Slow, Smooth)))
    			{
    				PlotBrushes[0][0] = AVGUP;
    			}
    			else if (IsFalling(AVG(Fast, Slow, Smooth)))
    			{
    				PlotBrushes[0][0] = AVGDOWN;

    Thank you.
    Last edited by Peter79; 08-11-2018, 11:31 AM.

    #2
    Hello Peter79,

    NinjaScript is written in C#.

    Below is a public link to a forum post with helpful information about getting started with NinjaScript.


    The code you have posted is incomplete. The variable declarations declare the type which area not included.

    You mentioned you are getting an error when trying to compile. What is the full error message you are receiving?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for replay. The error massage is : "The name "AVG" does not exist in the current context" . And that's regarding the last lines where I try to Plots AVG color when rising or falling.

      Comment


        #4
        Hello Peter79,

        What is AVG?

        Is this an indicator you are calling?
        Do you have an indicator named AVG you can add to a chart from the Indicator window?
        Is this indicator a custom indicator you have created or one you have imported?

        What is the full line of code causing this error?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          AVG is not an indicator just Avg of MACD or ( MacdAvg). It's the line called AVG in regular MACD indicator. There are 3 variables to plot on a chart in macd: macd, avg, diff. What I'm trying to do is to be able to change the color going up or down of not just MACD line ( like in MACDUpDown) but AVG line also.
          The error code is CS0103 and it refers to the last 7 lines of the code.

          Comment


            #6
            Hello Peter79,

            The code you have posted is not an exported script. We are not able to see the line numbers. We are also not able to see the private or public variable declarations.

            You have mentioned AVG with all capitals should be a data series for a plot is this correct?

            It appears there is a data series with the name Avg (where the 'vg' is lowercase).

            Have you also declared a series with the name AVG (where the 'VG' is uppercase)?

            With line:
            if (IsRising(AVG(Fast, Slow, Smooth)))

            AVG could be an indicator or a series but needs to exist..

            AVG is not the same as Avg.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I tried to export the code, but can't with the errors. I tried to change AVG to Avg as well as macdAvg. None of which seems to work. The errors remain the same CS0103 referring to these lines:

              if (IsRising(Avg(Fast, Slow, Smooth))) <<<---- this line CS0103
              {
              PlotBrushes[0][0] = AVGUP;
              }
              else if (IsFalling(Avg(Fast, Slow, Smooth))) <<<---- and this line CS0103
              {
              PlotBrushes[0][0] = AVGDOWN;


              Code:
              #region Using declarations
              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.ComponentModel.DataAnnotations;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
              using System.Windows;
              using System.Windows.Input;
              using System.Windows.Media;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Gui;
              using NinjaTrader.Gui.Chart;
              using NinjaTrader.Gui.SuperDom;
              using NinjaTrader.Gui.Tools;
              using NinjaTrader.Data;
              using NinjaTrader.NinjaScript;
              using NinjaTrader.Core.FloatingPoint;
              using NinjaTrader.NinjaScript.DrawingTools;
              #endregion
              
              //This namespace holds Indicators in this folder and is required. Do not change it. 
              namespace NinjaTrader.NinjaScript.Indicators
              {
              	public class MACDUpDownPlus : Indicator
              	{
              		private Series<double> fastEma;
              		private Series<double> slowEma;
              		
              		protected override void OnStateChange()
              		{
              			if (State == State.SetDefaults)
              			{
              				Description									= @"Enter the description for your new custom Indicator here.";
              				Name										= "MACDUpDownPlus";
              				Calculate									= Calculate.OnPriceChange;
              				IsOverlay									= true;
              				DisplayInDataBox							= true;
              				DrawOnPricePanel							= true;
              				DrawHorizontalGridLines						= true;
              				DrawVerticalGridLines						= true;
              				PaintPriceMarkers							= true;
              				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
              				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
              				//See Help Guide for additional information.
              				IsSuspendedWhileInactive					= true;
              				Fast = 12;
              				Slow = 26;
              				Smooth = 9;
              				MACDUP = Brushes.Green;
              				MACDDOWN = Brushes.Red;
              				AVGUP = Brushes.Magenta;
              				AVGDOWN = Brushes.Black;
              				AddPlot(Brushes.Green, "Macd");
              				AddPlot(Brushes.DarkViolet, "Avg");
              				AddPlot(new Stroke(Brushes.Navy, 2), PlotStyle.Bar, "Diff");
              				AddLine(Brushes.DarkGray, 0, "Zero line");
              			}
              			else if (State == State.Configure)
              			{
              			}
              			else if (State == State.DataLoaded)
              			{
              				fastEma = new Series<double>(this);
              				slowEma = new Series<double>(this);
              			}
              		}
              
              		protected override void OnBarUpdate()
              		{
              			if (CurrentBar == 0)
              			{
              				fastEma[0] = Input[0];
              				slowEma[0] = Input[0];
              				Value[0] = 0;
              				Avg[0] = 0;
              				Diff[0] = 0;
              			}
              			else
              			{
              				fastEma[0] = (2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1];
              				slowEma[0] = (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[0] = macd;
              				Avg[0] = macdAvg;
              				Diff[0] = macd - macdAvg;
              			}
              
              			// Plots MACD color when rising or falling
              			if (IsRising(MACD(Fast, Slow, Smooth)))
              			{
              				PlotBrushes[0][0] = MACDUP;
              			}
              			else if (IsFalling(MACD(Fast, Slow, Smooth)))
              			{
              				PlotBrushes[0][0] = MACDDOWN;
              			}
              			
              			// Plots Avg color when rising or falling
              			if (IsRising(Avg(Fast, Slow, Smooth)))
              			{
              				PlotBrushes[0][0] = AVGUP;
              			}
              			else if (IsFalling(Avg(Fast, Slow, Smooth)))
              			{
              				PlotBrushes[0][0] = AVGDOWN;
              			}
              		}
              				#region Properties
              		[NinjaScriptProperty]
              		[Range(1, int.MaxValue)]
              		[Display(Name = "Fast", Description = "Number of bars for fast EMA", Order = 1, GroupName = "Parameters")]
              		public int Fast
              		{ get; set; }
              
              		[NinjaScriptProperty]
              		[Range(1, int.MaxValue)]
              		[Display(Name = "Slow", Description = "Number of bars for slow EMA", Order = 2, GroupName = "Parameters")]
              		public int Slow
              		{ get; set; }
              
              		[NinjaScriptProperty]
              		[Range(1, int.MaxValue)]
              		[Display(Name = "Smooth", Description = "Number of bars for smoothing", Order = 3, GroupName = "Parameters")]
              		public int Smooth
              		{ get; set; }
              
              		[NinjaScriptProperty]
              		[XmlIgnore]
              		[Display(Name = "MACDUP", Description = "Color for MACD when rising", Order = 4, GroupName = "Parameters")]
              		public Brush MACDUP
              		{ get; set; }
              
              		[Browsable(false)]
              		public string MACDUPSerializable
              		{
              			get { return Serialize.BrushToString(MACDUP); }
              			set { MACDUP = Serialize.StringToBrush(value); }
              		}
              
              		[NinjaScriptProperty]
              		[XmlIgnore]
              		[Display(Name = "MACDDOWN", Description = "Color for MACD when falling", Order = 5, GroupName = "Parameters")]
              		public Brush MACDDOWN
              		{ get; set; }
              
              		[Browsable(false)]
              		public string MACDDOWNSerializable
              		{
              			get { return Serialize.BrushToString(MACDDOWN); }
              			set { MACDDOWN = Serialize.StringToBrush(value); }
              		}
              		
              		[NinjaScriptProperty]
              		[XmlIgnore]
              		[Display(Name = "AVGUP", Description = "Color for AVG when falling", Order = 6, GroupName = "Parameters")]
              		public Brush AVGUP
              		{ get; set; }
              
              		[Browsable(false)]
              		public string AVGUPSerializable
              		{
              			get { return Serialize.BrushToString(AVGUP); }
              			set { AVGUP = Serialize.StringToBrush(value); }
              		}
              		
              		[NinjaScriptProperty]
              		[XmlIgnore]
              		[Display(Name = "AVGDOWN", Description = "Color for AVG when falling", Order = 7, GroupName = "Parameters")]
              		public Brush AVGDOWN
              		{ get; set; }
              
              		[Browsable(false)]
              		public string AVGDOWNSerializable
              		{
              			get { return Serialize.BrushToString(AVGDOWN); }
              			set { AVGDOWN = Serialize.StringToBrush(value); }
              		}
              
              		[Browsable(false)]
              		[XmlIgnore]
              		public Series<double> Macd
              		{
              			get { return Values[0]; }
              		}
              
              		[Browsable(false)]
              		[XmlIgnore]
              		public Series<double> Avg
              		{
              			get { return Values[1]; }
              		}
              
              		[Browsable(false)]
              		[XmlIgnore]
              		public Series<double> Diff
              		{
              			get { return Values[2]; }
              		}
              
              		#endregion
              
              	}
              }
              
              #region NinjaScript generated code. Neither change nor remove.
              
              namespace NinjaTrader.NinjaScript.Indicators
              {
              	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
              	{
              		private MACDUpDownPlus[] cacheMACDUpDownPlus;
              		public MACDUpDownPlus MACDUpDownPlus(int fast, int slow, int smooth, Brush mACDUP, Brush mACDDOWN, Brush aVGUP, Brush aVGDOWN)
              		{
              			return MACDUpDownPlus(Input, fast, slow, smooth, mACDUP, mACDDOWN, aVGUP, aVGDOWN);
              		}
              
              		public MACDUpDownPlus MACDUpDownPlus(ISeries<double> input, int fast, int slow, int smooth, Brush mACDUP, Brush mACDDOWN, Brush aVGUP, Brush aVGDOWN)
              		{
              			if (cacheMACDUpDownPlus != null)
              				for (int idx = 0; idx < cacheMACDUpDownPlus.Length; idx++)
              					if (cacheMACDUpDownPlus[idx] != null && cacheMACDUpDownPlus[idx].Fast == fast && cacheMACDUpDownPlus[idx].Slow == slow && cacheMACDUpDownPlus[idx].Smooth == smooth && cacheMACDUpDownPlus[idx].MACDUP == mACDUP && cacheMACDUpDownPlus[idx].MACDDOWN == mACDDOWN && cacheMACDUpDownPlus[idx].AVGUP == aVGUP && cacheMACDUpDownPlus[idx].AVGDOWN == aVGDOWN && cacheMACDUpDownPlus[idx].EqualsInput(input))
              						return cacheMACDUpDownPlus[idx];
              			return CacheIndicator<MACDUpDownPlus>(new MACDUpDownPlus(){ Fast = fast, Slow = slow, Smooth = smooth, MACDUP = mACDUP, MACDDOWN = mACDDOWN, AVGUP = aVGUP, AVGDOWN = aVGDOWN }, input, ref cacheMACDUpDownPlus);
              		}
              	}
              }
              
              namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
              {
              	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
              	{
              		public Indicators.MACDUpDownPlus MACDUpDownPlus(int fast, int slow, int smooth, Brush mACDUP, Brush mACDDOWN, Brush aVGUP, Brush aVGDOWN)
              		{
              			return indicator.MACDUpDownPlus(Input, fast, slow, smooth, mACDUP, mACDDOWN, aVGUP, aVGDOWN);
              		}
              
              		public Indicators.MACDUpDownPlus MACDUpDownPlus(ISeries<double> input , int fast, int slow, int smooth, Brush mACDUP, Brush mACDDOWN, Brush aVGUP, Brush aVGDOWN)
              		{
              			return indicator.MACDUpDownPlus(input, fast, slow, smooth, mACDUP, mACDDOWN, aVGUP, aVGDOWN);
              		}
              	}
              }
              
              namespace NinjaTrader.NinjaScript.Strategies
              {
              	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
              	{
              		public Indicators.MACDUpDownPlus MACDUpDownPlus(int fast, int slow, int smooth, Brush mACDUP, Brush mACDDOWN, Brush aVGUP, Brush aVGDOWN)
              		{
              			return indicator.MACDUpDownPlus(Input, fast, slow, smooth, mACDUP, mACDDOWN, aVGUP, aVGDOWN);
              		}
              
              		public Indicators.MACDUpDownPlus MACDUpDownPlus(ISeries<double> input , int fast, int slow, int smooth, Brush mACDUP, Brush mACDDOWN, Brush aVGUP, Brush aVGDOWN)
              		{
              			return indicator.MACDUpDownPlus(input, fast, slow, smooth, mACDUP, mACDDOWN, aVGUP, aVGDOWN);
              		}
              	}
              }
              
              #endregion

              Comment


                #8
                Hello Peter79,

                I originally asked if AVG is an indicator, because you are calling this as if it were a method.

                Avg is not an indicator. Avg is a data series object.

                This cannot be called as a method. (A method uses the parenthesis)

                Avg(Fast, Slow, Smooth) will not work.

                Avg as a data series will also not accept any parameters.

                Try
                Code:
                if (IsRising(Avg))

                Further, MACD is an indicator. This can be called as an indicator. The MACD has 3 input parameters. The Fast, Slow, and Smooth.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you Chelsea, that worked. The only additional thing I had to change, was PlotBrushes[0][0] to Plotbrushes[1][0]. Otherwise it was changing colors for MACD line only.
                  Again thank you for your help.

                  Code:
                  // Plots Avg color when rising or falling
                  			if (IsRising(Avg))
                  			{
                  				PlotBrushes[1][0] = AVGUP;
                  			}
                  			else if (IsFalling(Avg))
                  			{
                  				PlotBrushes[1][0] = AVGDOWN;

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Pattontje, Yesterday, 02:10 PM
                  2 responses
                  14 views
                  0 likes
                  Last Post Pattontje  
                  Started by flybuzz, 04-21-2024, 04:07 PM
                  17 responses
                  229 views
                  0 likes
                  Last Post TradingLoss  
                  Started by agclub, 04-21-2024, 08:57 PM
                  3 responses
                  17 views
                  0 likes
                  Last Post TradingLoss  
                  Started by TradingLoss, 04-21-2024, 04:32 PM
                  4 responses
                  43 views
                  2 likes
                  Last Post TradingLoss  
                  Started by cre8able, 04-17-2024, 04:16 PM
                  6 responses
                  56 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X