Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AddPlot Identify Object

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

    AddPlot Identify Object

    I'm trying to call the value of an indicator output that does not have a readable plot for the Strat builder. Specifically the bar counter indicator that plots the current bar count on each bar close. I added

    AddPlot(Brushes.Transparent, "Alert");

    to the OnStateChange.

    From the following:
    --------------------------------------------------------------------------------------------------------
    if (Bars.IsFirstBarOfSession) //start counting from first bar of session
    {
    _barCntr = 1;
    }
    else
    _barCntr++;


    if (_showOddNumbers)
    {
    if (_barCntr % 2 != 0) //is odd number
    Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);


    }
    else
    {
    if (_barCntr % 2 == 0) //is even number
    Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
    }
    ---------------------------------------------------------------------------------------------------------

    From what do I derive the current bar value to place into the alert log?

    Hence:

    Alert[0] = ??;

    Thanx !

    #2
    Hello johnMoss,

    To provide a signal to the strategy builder or to the market analyzer, you must add a plot and set the Value[plot index][bar index] of that plot.

    The Values[plot index] will also need to be returned as a public property.

    Below is a link to an example.
    https://ninjatrader.com/support/foru...238#post812238

    With your question:
    "From what do I derive the current bar value to place into the alert log?"

    I'm not certain what this means.

    You can use CurrentBar which is the number of the bar currently being processed in OnBarUpdate.
    https://ninjatrader.com/support/help...currentbar.htm

    You can trigger an alert to the alert log with the Alert() method in real-time (not historical).
    https://ninjatrader.com/support/help.../nt8/alert.htm
    Last edited by NinjaTrader_ChelseaB; 12-17-2021, 10:52 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Got it, thank you
      Last edited by johnMoss; 12-16-2021, 04:52 PM. Reason: edited as I had put code in wrong configuration

      Comment


        #4
        Hello johnMoss,

        You want to know how many bars since the start of the session?

        You can save CurrentBar to a variable when Bars.IsFirstBarOfSession is true.


        The subtract CurrentBar from this number.

        private int firstBarOfSessionNumber;

        if (Bars.IsFirstBarOfSession)
        {
        firstBarOfSessionNumber = CurrentBar;
        }

        Print("Its been " + (CurrentBar - firstBarOfSessionNumber).ToString() + " bars since the open of the session");
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          OK I had loaded your previous example in the correct manner and it compiled just fine. Only problem is when added to strategy, the strategy quit firing entries...

          This is what I have, (AddPlot is properly configured but not shown here, and set to "barNumber")

          ************************************************** ************************************

          if (Bars.IsFirstBarOfSession)
          {
          _barCntr = 1;

          }
          else
          _barCntr++;
          }


          [Browsable(false)]
          [XmlIgnore]

          public Series<double> barNumber
          {
          get { return Values[0]; }
          }

          ************************************************** *********************************

          This does not appear to be working...

          Comment


            #6
            In the attached pic is the goal. As you see the bar counter indicator is displaying bar numbers below each candle. I need for the strat builder to be able to use [see] them.

            Comment


              #7
              Hello johnMoss,

              It does not look like you are assigning a plot value in OnBarUpdate.

              AddPlot would be added in OnStateChange under State.SetDefaults

              Then Values[PlotIndex][BarsAgo] would be assigned with your plot value in OnBarUpdate.

              If you go through our first indicator tutorial, the process for adding plots and assigning values should be easier to follow.

              Comment


                #8
                Hi Jim, thank you for jumping in here, you may be gone by now, so to you or Chelsea: As I noted above, Yes, I did add it. I had left it out in the above to simplify the presentation. While awaiting yall's next response, I shall watch the tutorial and examine some other indicators. I very much appreciate your time and responses, I know I'm pushing the envelope as to the scope of your functions here ... I perceive this to be a rather simple [novice] nomenclature issue on my part. The way I personally approach this learning process is to tinker with existing constructs... In the moment, here is the codeSet, with additions made by me in highlight:

                ************************************************** ************************************************** **************
                namespace NinjaTrader.NinjaScript.Indicators
                {
                public class BarCounter : Indicator
                {
                private int _barCntr = 1;
                private bool _showOddNumbers = true;
                private Brush _textColorDefinedbyUser = Brushes.Gray;
                private int _pixelsAboveBelowBar = -50;
                private bool _textIsBelowBars = true;
                private int _fontSize = 14;
                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = @"Labels Bars by number from session start";
                Name = "BarCounter";
                Calculate = Calculate.OnBarClose;
                IsOverlay = true;
                DisplayInDataBox = false;
                DrawOnPricePanel = true;
                //DrawHorizontalGridLines = true;
                //DrawVerticalGridLines = false;
                PaintPriceMarkers = false;
                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;
                TextColor = Brushes.Gray;


                AddPlot(Brushes.Transparent, "barNumber");


                }
                else if (State == State.Configure)
                {
                if (_pixelsAboveBelowBar > 0 && _textIsBelowBars == true)
                _pixelsAboveBelowBar = _pixelsAboveBelowBar * -1;
                }
                }



                protected override void OnBarUpdate()
                {
                double _textYStartingPoint = High[0];
                _textYStartingPoint = Low[0];

                NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 14) { Size = _fontSize, Bold = false };

                if (Bars.IsFirstBarOfSession)
                _barCntr = 1;

                }
                else
                _barCntr++;



                if (_showOddNumbers)
                {
                if (_barCntr % 2 != 0) //is odd number
                Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);


                }
                else
                {
                if (_barCntr % 2 == 0) //is even number
                Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
                }

                }



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

                }

                Properties, etc. etc... in continuance...

                ************************************************** ************************************************** ********************

                This compiles just fine, and the values as viewable on the chart continue to be correct, but barNumber here is apparently not returning the correct value, I suspect it is resetting to zero or one on each bar update...

                Thnx
                JM

                Comment


                  #9
                  Don't know if yall are seeing this latest response above. Problem remains...

                  Comment


                    #10
                    Hello johnMoss,

                    Are you leaving out the part of the code where barNumber[0] is being assigned a value? (or Values[0][0] is being assigned a value)

                    If so, may I have you include this code?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      The total code is placed above in this feed. Here it is again:

                      I highlighted my additions...

                      ************************************************** ************************************************** **************
                      namespace NinjaTrader.NinjaScript.Indicators
                      {
                      public class BarCounter : Indicator
                      {
                      private int _barCntr = 1;
                      private bool _showOddNumbers = true;
                      private Brush _textColorDefinedbyUser = Brushes.Gray;
                      private int _pixelsAboveBelowBar = -50;
                      private bool _textIsBelowBars = true;
                      private int _fontSize = 14;
                      protected override void OnStateChange()
                      {
                      if (State == State.SetDefaults)
                      {
                      Description = @"Labels Bars by number from session start";
                      Name = "BarCounter";
                      Calculate = Calculate.OnBarClose;
                      IsOverlay = true;
                      DisplayInDataBox = false;
                      DrawOnPricePanel = true;
                      //DrawHorizontalGridLines = true;
                      //DrawVerticalGridLines = false;
                      PaintPriceMarkers = false;
                      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;
                      TextColor = Brushes.Gray;


                      AddPlot(Brushes.Transparent, "barNumber");


                      }
                      else if (State == State.Configure)
                      {
                      if (_pixelsAboveBelowBar > 0 && _textIsBelowBars == true)
                      _pixelsAboveBelowBar = _pixelsAboveBelowBar * -1;
                      }
                      }



                      protected override void OnBarUpdate()
                      {
                      double _textYStartingPoint = High[0];
                      _textYStartingPoint = Low[0];

                      NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 14) { Size = _fontSize, Bold = false };

                      if (Bars.IsFirstBarOfSession)
                      _barCntr = 1;

                      }
                      else
                      _barCntr++;



                      if (_showOddNumbers)
                      {
                      if (_barCntr % 2 != 0) //is odd number
                      Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);


                      }
                      else
                      {
                      if (_barCntr % 2 == 0) //is even number
                      Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
                      }

                      }



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

                      }

                      Properties, etc. etc... in continuance...

                      Comment


                        #12
                        Hello johnMoss,

                        You have added a plot, but you are not assigning a value to the plot in OnBarUpdate.

                        After going through the first indicator tutorial and observing how Values[0][BarsAgo] = is used, you can better see how to assign values to the added plot.

                        Comment


                          #13
                          The example you are providing does not work, in a multitude of configurations. I tried using your tutorial as best I understand and it continuously generates errors on compilation. Here is the latest I tried, doing my best to follow the instructions, nay to no avail... My additions are highlighted.



                          The indicator works fine, it places a number under a bar on a chart. I need to access that number in the strategy builder.


                          ************************************************** ************************************************** **************
                          namespace NinjaTrader.NinjaScript.Indicators
                          {
                          public class BarCounter : Indicator
                          {
                          private int _barCntr = 1;
                          private bool _showOddNumbers = true;
                          private Brush _textColorDefinedbyUser = Brushes.Gray;
                          private int _pixelsAboveBelowBar = -50;
                          private bool _textIsBelowBars = true;
                          private int _fontSize = 14;
                          protected override void OnStateChange()
                          {
                          if (State == State.SetDefaults)
                          {
                          Description = @"Labels Bars by number from session start";
                          Name = "BarCounter";
                          Calculate = Calculate.OnBarClose;
                          IsOverlay = true;
                          DisplayInDataBox = false;
                          DrawOnPricePanel = true;
                          //DrawHorizontalGridLines = true;
                          //DrawVerticalGridLines = false;
                          PaintPriceMarkers = false;
                          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;
                          TextColor = Brushes.Gray;


                          AddPlot(Brushes.Transparent, "barNumber");


                          }
                          else if (State == State.Configure)
                          {
                          if (_pixelsAboveBelowBar > 0 && _textIsBelowBars == true)
                          _pixelsAboveBelowBar = _pixelsAboveBelowBar * -1;
                          }
                          }



                          protected override void OnBarUpdate()
                          {
                          double _textYStartingPoint = High[0];
                          _textYStartingPoint = Low[0];

                          NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 14) { Size = _fontSize, Bold = false };

                          if (Bars.IsFirstBarOfSession)
                          _barCntr = 1;

                          barNumber = 1;
                          }
                          else
                          _barCntr++;

                          barNumber++

                          if (_showOddNumbers)
                          {
                          if (_barCntr % 2 != 0) //is odd number
                          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);


                          }
                          else
                          {
                          if (_barCntr % 2 == 0) //is even number
                          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
                          }

                          }



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

                          }

                          ************************************************** ************************************************** ************

                          This generates several errors on compilation...
                          Last edited by johnMoss; 12-17-2021, 02:12 PM. Reason: Tone...

                          Comment


                            #14
                            Hello johnMoss,

                            The tutorial walks through creating an indicator and assigning plot values in that indicator.

                            You are not assigning plot values in your indicator.

                            barNumber is a Series<double> and you are not assigning values to it correctly.

                            You would assign the current value of a Series<double> like barNumber[0] = 1;

                            This would assign the plot value associated with that bar to 1 because barNumber gets Values[0].

                            You can also do Values[0][0] = 1; and this will also assign the plot value to 1.

                            If you want to increment a counter, I suggest creating a class level private variable for the counter, and then to assign your plot value with the value from your variable.

                            Compile errors would mean the syntax is incorrect. If you are struggling to write the proper syntax, I suggest taking a step back and to practice crating small pieces of code with the Strategy Builder, and to use View Code to see the resulting syntax.

                            Publicly available resources on C# can also be looked into to better learn how to write the C# syntax.

                            Comment


                              #15
                              Thank you Jim, I will dive into the above further....

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              581 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              338 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              554 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              552 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X