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

Indicator plotting - visual management

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

    Indicator plotting - visual management

    Hi there,

    I take an indicator and i want it to plot only on the right half of the chart.
    How do i do that? How do i tell it to dispose of plots 60 minutes or x bars old?

    What i want to do is i want to plot two indicator over each other. the first i displace x bars the second i do what i want above so they don't overlap each other, but are at the same height. Scale justification is not needed since its the same kind of indicator just different settings.

    Thanks!

    #2
    Originally posted by Gregorio View Post
    Hi there,

    I take an indicator and i want it to plot only on the right half of the chart.
    How do i do that? How do i tell it to dispose of plots 60 minutes or x bars old?

    What i want to do is i want to plot two indicator over each other. the first i displace x bars the second i do what i want above so they don't overlap each other, but are at the same height. Scale justification is not needed since its the same kind of indicator just different settings.

    Thanks!
    How are you setting the Plot? More specifically, what is the name of the plot on which you are using the Set method.

    Comment


      #3
      Originally posted by koganam View Post
      How are you setting the Plot? More specifically, what is the name of the plot on which you are using the Set method.
      Its a MACD indicator.

      Comment


        #4
        Originally posted by Gregorio View Post
        Its a MACD indicator.
        To do what you want to do, you must modify the indicator. That is why I asked you for the line that sets the Plot that you want to behave in the manner that you described. So telling me that it is a MACD indicator means nothing, as it does not tell me what I need to know in order to tell you what to modify.

        So, as I cannot under the circumstances, tell you what exactly to add/modify, here is the general principle of what you need to do. You do either or both of:
        • Reset the Plot at "x" bars back.
        • Change the color of the Plot "x" bars back, to transparent.

        Comment


          #5
          Sorry! I send you the code of course! This is the NT8 version

          #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.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
          {
          /// <summary>
          /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator
          /// that shows the relationship between two moving averages of prices.
          /// </summary>
          public class MACD : Indicator
          {
          private Series<double> fastEma;
          private Series<double> slowEma;
          private double constant1;
          private double constant2;
          private double constant3;
          private double constant4;
          private double constant5;
          private double constant6;

          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionMACD;
          Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMACD;
          Fast = 12;
          IsSuspendedWhileInactive = true;
          Slow = 26;
          Smooth = 9;

          AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMACD);
          AddPlot(Brushes.Crimson, NinjaTrader.Custom.Resource.NinjaScriptIndicatorAv g);
          AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.NinjaScriptIndicatorDi ff);
          AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);
          }
          else if (State == State.Configure)
          {
          constant1 = 2.0 / (1 + Fast);
          constant2 = (1 - (2.0 / (1 + Fast)));
          constant3 = 2.0 / (1 + Slow);
          constant4 = (1 - (2.0 / (1 + Slow)));
          constant5 = 2.0 / (1 + Smooth);
          constant6 = (1 - (2.0 / (1 + Smooth)));
          fastEma = new Series<double>(this);
          slowEma = new Series<double>(this);
          }
          }

          protected override void OnBarUpdate()
          {
          double input0 = Input[0];

          if (CurrentBar == 0)
          {
          fastEma[0] = input0;
          slowEma[0] = input0;
          Value[0] = 0;
          Avg[0] = 0;
          Diff[0] = 0;
          }
          else
          {
          double fastEma0 = constant1 * input0 + constant2 * fastEma[1];
          double slowEma0 = constant3 * input0 + constant4 * slowEma[1];
          double macd = fastEma0 - slowEma0;
          double macdAvg = constant5 * macd + constant6 * Avg[1];

          fastEma[0] = fastEma0;
          slowEma[0] = slowEma0;
          Value[0] = macd;
          Avg[0] = macdAvg;
          Diff[0] = macd - macdAvg;
          }
          }

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

          [Browsable(false)]
          [XmlIgnore]
          public Series<double> Default
          {
          get { return Values[0]; }
          }

          [Browsable(false)]
          [XmlIgnore]
          public Series<double> Diff
          {
          get { return Values[2]; }
          }

          [Range(1, int.MaxValue), NinjaScriptProperty]
          [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)]
          public int Fast
          { get; set; }

          [Range(1, int.MaxValue), NinjaScriptProperty]
          [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 1)]
          public int Slow
          { get; set; }

          [Range(1, int.MaxValue), NinjaScriptProperty]
          [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 2)]
          public int Smooth
          { get; set; }
          #endregion
          }
          }

          Comment


            #6
            You will have to make a copy of the indicator and edit it, because NT tries to prevent editing its shipped files.
            1. Open the MACD in the NT Editor.
            2. Right-click in the file, and use the "Save As ..." menu item to save it as a different filename.

            I have cut out a part of what you posted, so as to show you where to place your modifications to the code.
            //This came from what you posted
            ...
            fastEma[0] = fastEma0;
            slowEma[0] = slowEma0;
            Value[0] = macd;
            Avg[0] = macdAvg;
            Diff[0] = macd - macdAvg;

            //Turn transparent the Plots at "x" bars ago
            int x = 60; //say. You may want to make this a class variable and input parameter.

            PlotBrushes[0][x] = Brushes.Transparent;
            PlotBrushes[1][x] = Brushes.Transparent;
            PlotBrushes[2][x] = Brushes.Transparent;
            My additions are in blue.

            Comment


              #7
              I tried the plotbrushes method but it plots only when the value is 1. If i set a value greater than 1 it fails to plot and gives an OnBarUpdate error saying accessing an index with a value that is invalid since out of range. Any ideas? What is the other method? the "Reset the Plot at "x" bars back."

              Here is what i changed in the script.



              #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.Data;
              using NinjaTrader.NinjaScript;
              using NinjaTrader.Core.FloatingPoint;
              using NinjaTrader.NinjaScript.DrawingTools;
              #endregion

              namespace NinjaTrader.NinjaScript.Indicators
              {
              /// <summary>
              /// The AAAMACD (Moving Average Convergence/Divergence) is a trend following momentum indicator
              /// that shows the relationship between two moving averages of prices.
              /// </summary>
              public class AAAMACD : Indicator
              {
              private int agoBarx;
              private Series<double> fastEma;
              private Series<double> slowEma;
              private double constant1;
              private double constant2;
              private double constant3;
              private double constant4;
              private double constant5;
              private double constant6;

              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionMACD;
              Name = "AAAMACD";
              Fast = 12;
              IsSuspendedWhileInactive = true;
              Slow = 26;
              Smooth = 9;
              AgoBarx = 1;

              AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMACD);
              AddPlot(Brushes.Crimson, NinjaTrader.Custom.Resource.NinjaScriptIndicatorAv g);
              AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.NinjaScriptIndicatorDi ff);
              AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);

              }
              else if (State == State.Configure)
              {
              constant1 = 2.0 / (1 + Fast);
              constant2 = (1 - (2.0 / (1 + Fast)));
              constant3 = 2.0 / (1 + Slow);
              constant4 = (1 - (2.0 / (1 + Slow)));
              constant5 = 2.0 / (1 + Smooth);
              constant6 = (1 - (2.0 / (1 + Smooth)));
              fastEma = new Series<double>(this);
              slowEma = new Series<double>(this);


              }
              }

              protected override void OnBarUpdate()
              {
              double input0 = Input[0];

              if (CurrentBar == 0)
              {
              fastEma[0] = input0;
              slowEma[0] = input0;
              Value[0] = 0;
              Avg[0] = 0;
              Diff[0] = 0;




              }
              else
              {
              double fastEma0 = constant1 * input0 + constant2 * fastEma[1];
              double slowEma0 = constant3 * input0 + constant4 * slowEma[1];
              double macd = fastEma0 - slowEma0;
              double macdAvg = constant5 * macd + constant6 * Avg[1];

              fastEma[0] = fastEma0;
              slowEma[0] = slowEma0;
              Value[0] = macd;
              Avg[0] = macdAvg;
              Diff[0] = macd - macdAvg;

              PlotBrushes[0][AgoBarx] = Brushes.Transparent;
              PlotBrushes[1][AgoBarx] = Brushes.Transparent;
              PlotBrushes[2][AgoBarx] = Brushes.Transparent;

              }


              }

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

              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Default
              {
              get { return Values[0]; }
              }

              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Diff
              {
              get { return Values[2]; }
              }

              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)]
              public int Fast
              { get; set; }

              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 1)]
              public int Slow
              { get; set; }

              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 2)]
              public int Smooth
              { get; set; }

              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "BarAgo", GroupName = "NinjaScriptParameters", Order = 3)]
              public int AgoBarx
              { get; set; }

              #endregion
              }
              }

              Comment


                #8
                Originally posted by Gregorio View Post
                I tried the plotbrushes method but it plots only when the value is 1. If i set a value greater than 1 it fails to plot and gives an OnBarUpdate error saying accessing an index with a value that is invalid since out of range. Any ideas? What is the other method? the "Reset the Plot at "x" bars back."

                Here is what i changed in the script.



                #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.Data;
                using NinjaTrader.NinjaScript;
                using NinjaTrader.Core.FloatingPoint;
                using NinjaTrader.NinjaScript.DrawingTools;
                #endregion

                namespace NinjaTrader.NinjaScript.Indicators
                {
                /// <summary>
                /// The AAAMACD (Moving Average Convergence/Divergence) is a trend following momentum indicator
                /// that shows the relationship between two moving averages of prices.
                /// </summary>
                public class AAAMACD : Indicator
                {
                private int agoBarx;
                private Series<double> fastEma;
                private Series<double> slowEma;
                private double constant1;
                private double constant2;
                private double constant3;
                private double constant4;
                private double constant5;
                private double constant6;

                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionMACD;
                Name = "AAAMACD";
                Fast = 12;
                IsSuspendedWhileInactive = true;
                Slow = 26;
                Smooth = 9;
                AgoBarx = 1;

                AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMACD);
                AddPlot(Brushes.Crimson, NinjaTrader.Custom.Resource.NinjaScriptIndicatorAv g);
                AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.NinjaScriptIndicatorDi ff);
                AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);

                }
                else if (State == State.Configure)
                {
                constant1 = 2.0 / (1 + Fast);
                constant2 = (1 - (2.0 / (1 + Fast)));
                constant3 = 2.0 / (1 + Slow);
                constant4 = (1 - (2.0 / (1 + Slow)));
                constant5 = 2.0 / (1 + Smooth);
                constant6 = (1 - (2.0 / (1 + Smooth)));
                fastEma = new Series<double>(this);
                slowEma = new Series<double>(this);


                }
                }

                protected override void OnBarUpdate()
                {
                double input0 = Input[0];

                if (CurrentBar == 0)
                {
                fastEma[0] = input0;
                slowEma[0] = input0;
                Value[0] = 0;
                Avg[0] = 0;
                Diff[0] = 0;




                }
                else
                {
                double fastEma0 = constant1 * input0 + constant2 * fastEma[1];
                double slowEma0 = constant3 * input0 + constant4 * slowEma[1];
                double macd = fastEma0 - slowEma0;
                double macdAvg = constant5 * macd + constant6 * Avg[1];

                fastEma[0] = fastEma0;
                slowEma[0] = slowEma0;
                Value[0] = macd;
                Avg[0] = macdAvg;
                Diff[0] = macd - macdAvg;

                PlotBrushes[0][AgoBarx] = Brushes.Transparent;
                PlotBrushes[1][AgoBarx] = Brushes.Transparent;
                PlotBrushes[2][AgoBarx] = Brushes.Transparent;

                }


                }

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

                [Browsable(false)]
                [XmlIgnore]
                public Series<double> Default
                {
                get { return Values[0]; }
                }

                [Browsable(false)]
                [XmlIgnore]
                public Series<double> Diff
                {
                get { return Values[2]; }
                }

                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)]
                public int Fast
                { get; set; }

                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 1)]
                public int Slow
                { get; set; }

                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 2)]
                public int Smooth
                { get; set; }

                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(ResourceType = typeof(Custom.Resource), Name = "BarAgo", GroupName = "NinjaScriptParameters", Order = 3)]
                public int AgoBarx
                { get; set; }

                #endregion
                }
                }
                Two ways to resolve that. The easiest way is to search the forum for that message and you will find many kinds of answers which amount to the same thing essentially.

                Here is the more elegant way.
                Code:
                int BarsBack = Math.Min(CurrentBar, AgoBarx);
                			
                PlotBrushes[0][BarsBack] = Brushes.Transparent;
                PlotBrushes[1][BarsBack] = Brushes.Transparent;
                PlotBrushes[2][BarsBack] = Brushes.Transparent;
                My mistake. I should have handled escaping when there were not enough bars.

                ref: http://ninjatrader.com/support/forum...37&postcount=3

                Comment


                  #9
                  Perfect. Thanks koganam!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by PaulMohn, Today, 12:36 PM
                  2 responses
                  16 views
                  0 likes
                  Last Post PaulMohn  
                  Started by Conceptzx, 10-11-2022, 06:38 AM
                  2 responses
                  53 views
                  0 likes
                  Last Post PhillT
                  by PhillT
                   
                  Started by Kaledus, Today, 01:29 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post Kaledus
                  by Kaledus
                   
                  Started by yertle, Yesterday, 08:38 AM
                  8 responses
                  37 views
                  0 likes
                  Last Post ryjoga
                  by ryjoga
                   
                  Started by rdtdale, Today, 01:02 PM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_LuisH  
                  Working...
                  X