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

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[COLOR="Blue"][1][/COLOR][0] = UptickAVG;
    }
    else if (Falling(MACD(fast, slow, smooth).Avg))
    {	
    	PlotColors[COLOR="blue"][1][/COLOR][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:
            [B]if (CurrentBar <1) return;[/B]
            // 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
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, 04-17-2024, 06:40 PM
            6 responses
            48 views
            0 likes
            Last Post algospoke  
            Started by arvidvanstaey, Today, 02:19 PM
            4 responses
            11 views
            0 likes
            Last Post arvidvanstaey  
            Started by samish18, 04-17-2024, 08:57 AM
            16 responses
            61 views
            0 likes
            Last Post samish18  
            Started by jordanq2, Today, 03:10 PM
            2 responses
            9 views
            0 likes
            Last Post jordanq2  
            Started by traderqz, Today, 12:06 AM
            10 responses
            19 views
            0 likes
            Last Post traderqz  
            Working...
            X