Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enhancing MACDUpDown

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

    Enhancing MACDUpDown

    I am new to NinjaTrader and currently teaching myself NinjaScript. I have worked through the tutorials and thought as my first project I would try to enhance an existing indicator. In this regard, I choose MACDUpDown created by NinjaTrader_Matthew.

    I thought it would be useful to have the Avg live (%D) change color when it goes up and down. While my changes compile without any errors and I have the option to change the color for the Avg line, the line remains one solid color.

    I have attached a copy of the script and have summarized my changes below:

    Inserted the following into Variables (lines 30 & 31)
    Code:
            private Color			uptickAVG 	= Color.Green;
            private Color			downtickAVG	= Color.Red;
    Inserted the following on lines 92-100:

    Code:
    	// Plots AVG color when rising or falling
           	if (Rising(MACD(fast, slow, smooth).Avg))
    	{
    	       PlotColors[0][0] = UptickAVG;
    	}
    	else if (Falling(MACD(fast, slow, smooth).Avg))
    	{	
    		PlotColors[0][0] = DowntickAVG;
    	}
    And finally inserted the following into Properties (lines 193 - 223)

    Code:
    	[XmlIgnore()]
            [Description("Color for MACD Avg when rising")]
            [Category("Plots")]
    	[Gui.Design.DisplayNameAttribute("Avg Up Color")]
            public Color UptickAVG
            {
                get { return uptickAVG; }
                set { uptickAVG = value; }
            }
    	[Browsable(false)]
    	public string UptickAVGSerialize
    	{
    		get { return NinjaTrader.Gui.Design.SerializableColor.ToString(UptickAVG); }
    		set { UptickAVG = NinjaTrader.Gui.Design.SerializableColor.FromString(value); }
    	}
    		
    	[XmlIgnore()]
            [Description("Color for MACD Avg when falling")]
            [Category("Plots")]
    	[Gui.Design.DisplayNameAttribute("Avg Down Color")]
            public Color DowntickAVG
            {
                get { return downtickAVG; }
                set { downtickAVG = value; }
            }
    	[Browsable(false)]
    	public string DowntickAVGSerialize
    	{
    		get { return NinjaTrader.Gui.Design.SerializableColor.ToString(DowntickAVG); }
    		set { DowntickAVG = NinjaTrader.Gui.Design.SerializableColor.FromString(value); }
    	}

    I should be grateful if someone would look at my code and let me know what I am missing.

    Thanks,
    Richard
    Attached Files

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

    You have referenced to the first plot while coloring the plots. Please make sure to refer to the correct plot (Avg being the 2nd plot series) to color that plot spcifically.

    If you try modifying your code as below then are you getting the correct values.


    Code:
    // Plots AVG color when rising or falling
    if (Rising(MACD(fast, slow, smooth).Avg))
    {
    	PlotColors[1][0] = UptickAVG;
    }
    else if (Falling(MACD(fast, slow, smooth).Avg))
    {	
    	PlotColors[1][0] = DowntickAVG;
    }
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thank you Joydeep, that was it.

      Now that I have it working, I noticed that the %D line is "behind" the MACD. Since I have the MACD set to bars, it is very difficult to read the %D line. Therefore, I was wondering if there is a way to change the order and have the %D line print on top of the MACD bars and/or change the opacity of the MACD bars? If not, might I ask that this be added to the feature request list?

      Comment


        #4
        Hello Richard,
        To change the opacity you can assign the color as
        Code:
        Color.FromArgb(100, Color.Green)
        100 is the opacity value and you can assign a number between 0 and 255.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Thank you Joydeep,

          The opacity worked, but ideally I would like to move the %D line in front of MACD. Is there any way of doing that?

          I would like to further enhance the indicator by turning the %D (Avg) line black when it is "flat". I have programmed it such that the user can define the threshold amount used to determine when the %D line is flat.

          While the script compiles without any errors, the latest enhancement is not working. I have narrowed down the issue to line 121 (where I compare the current to previous amounts of the Avg line). Since line 88 states:

          Code:
          Avg.Set(macdAvg);
          I thought the calculation to determine if %D is flat should be:

          Code:
          if(Math.Abs(Avg[1] - Avg[0]) <= tolerance)
             {
                PlotColors[1][0] = FlatAVG;
              }
          However, when tolerance is anything but zero (zero turns off tolerance), the MACDUpDown indicator is completely blank.

          I unsuccessfully tried the following at line 120 (after removing line 121):

          Code:
          if(Math.Abs((MACD(fast, slow, smooth).Avg)[1] - (MACD(fast, slow, smooth).Avg)[0]) <= tolerance)
          Are you able to advise what I am missing? For your convenience, I have attached a copy of the script.

          Thank you in advance for your help.

          Richard
          Attached Files

          Comment


            #6
            Hello Richard,
            The plot will be displayed as per the order you set up the Plots. If you change the plots as below then the Avg will be the topmost.
            Code:
            Add(new Plot(Color.Green, PlotStyle.Line, "Macd"));
            Add(new Plot(new Pen(Color.Navy, 2), PlotStyle.Bar, "Diff"));
            Add(new Plot(Color.DarkViolet, "Avg"));
            Also please make sure to make changes to the necessary data series property values too.

            You have to check if there are adequeate bars to resolve it.
            Code:
            if (CurrentBar <1) return;
            // if(Math.Abs((MACD(fast, slow, smooth).Avg)[1] - (MACD(fast, slow, smooth).Avg)[0]) <= tolerance)
            if(Math.Abs(Avg[1] - Avg[0]) <= tolerance)

            Please refer to this post which further discusses it
            http://ninjatrader.com/support/forum...ead.php?t=3170
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Jeremytang, 07-14-2018, 06:45 PM
            21 responses
            440 views
            0 likes
            Last Post mav8rick  
            Started by LeonK, Yesterday, 09:26 PM
            1 response
            10 views
            0 likes
            Last Post bltdavid  
            Started by NicholasJoannette, Yesterday, 03:01 PM
            2 responses
            12 views
            0 likes
            Last Post NicholasJoannette  
            Started by rach1994, Yesterday, 12:20 PM
            1 response
            35 views
            0 likes
            Last Post bltdavid  
            Started by litamm89, Yesterday, 11:00 AM
            5 responses
            22 views
            1 like
            Last Post rafael_delima86  
            Working...
            X