Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Point counter indicator on each bar

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

    #16
    Hello,

    You can use RemoveDrawObject() on the 21st bar to only have the last 20 bars be visible.

    You would need to loop through the DrawObjects collection to find specific draw objects, get their tag name, and use that tag name when calling RemoveDrawObject().

    See the sample code on the DrawObjects help guide page linked below for how to loop through the DrawObjects collection to find specific draw objects.


    DrawObjects: https://ninjatrader.com/support/help...rawobjects.htm

    RemoveDrawObject: https://ninjatrader.com/support/help...drawobject.htm

    SampleRemoveDrawObjects: https://ninjatrader.com/support/help...s_from_the.htm


    Please let us know if we can assist further.

    Comment


      #17
      Hello Gaby,

      Could you make me a code example? The Info from the sample removes every drawing and just lets bar 1 counting....

      I dont know what I did wrong...

      Thanks!

      Comment


        #18
        Hello tradingnasdaqprueba,

        Instead of deleting all of the drawing objects, you could delete the object 21 bars ago on each new bar.

        if (CurrentBar > 20)
        {
        RemoveDrawObject("MyDrawingObjectName" + (CurrentBar - 21).ToString());
        }

        This is assuming you are making each drawing object tag name unique by adding CurrentBar to the string.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #19
          Hello Gaby,

          Where should I put this code?



          Code:
          protected override void OnBarUpdate()
          
          {
          if (State == State.Historical && CurrentBar < Count - _BarsToCount)
          
          return;
          
          double _textYStartingPoint = High[0]; //position text above or below bars
          if (_textIsBelowBars)
          _textYStartingPoint = Low[0];
          
          //Add your custom indicator logic here.
          NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 14) { Size = _fontSize, Bold = false };
          
          if (Bars.IsLastBarOfSession) //start counting from first bar of session
          {
          _barCntr = Math.Round(((High[0] - Low[0]) / TickSize) /4);
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          }
          
          else
          _barCntr = Math.Round(((High[0] - Low[0]) / TickSize) /4);
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          
          if (_CalculateOpenLow)
          {
          if (Close[0] > Open[0]) // candle is an up 'green' bar
          
          {
          _barCntr = Math.Round(((Close[0] - Low[0]) / TickSize) /4);
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          }
          else
          _barCntr = Math.Round(((High[0] - Close[0]) / TickSize) /4);
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          
          
          if (_CalculateDecimals)
          {
          if (Close[0] > Open[0]) // candle is an up 'green' bar
          
          {
          _barCntr = ((Close[0] - Low[0]) / TickSize) /4;
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          }
          else
          _barCntr = ((High[0] - Close[0]) / TickSize) /4;
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          }
          if (_ShowStopDollars)
          {
          if (Instrument.MasterInstrument.Name == "MNQ")
          if (Close[0] > Open[0]) // candle is an up 'green' bar
          
          {
          _barCntr = ((Close[0] - Low[0]) / TickSize) /2;
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString("C"), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          }
          else
          _barCntr = ((High[0] - Close[0]) / TickSize) /2;
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString("C"), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          
          if (Instrument.MasterInstrument.Name == "NQ")
          if (Close[0] > Open[0]) // candle is an up 'green' bar
          
          {
          _barCntr = ((Close[0] - Low[0]) / TickSize) *5;
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString("C"), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          }
          else
          _barCntr = ((High[0] - Close[0]) / TickSize) *5;
          Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString("C"), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
          }
          }
          
          }

          Comment


            #20
            Hello,

            You can add this to the end of OnBarUpdate(), after you have drawn all your drawing objects.

            Please let us know if you have any other questions.

            Comment


              #21
              Hello Gaby,

              I did as you said... but nothing happens... the 21 last Bar is not removed...

              Here my code, maybe you find something wrong:

              Code:
              [HASHTAG="t3322"]region[/HASHTAG] 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 BarCounter : Indicator
              {
              private double _barCntr = 0;
              private Brush _textColorDefinedbyUser = Brushes.White;
              private int _pixelsAboveBelowBar = -25;
              private int _BarsToCount = 20;
              private bool _textIsBelowBars = true;
              private bool _CalculateOpenLow = true;
              private bool _CalculateDecimals = true;
              private bool _ShowStopDollars = true;
              private int _fontSize = 11;
              
              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Labels Bars Points from User define Bars";
              Name = "BarCounter";
              Calculate = Calculate.OnPriceChange;
              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.White;
              
              
              
              
              
              }
              else if (State == State.Configure)
              {
              if (_pixelsAboveBelowBar > 0 && _textIsBelowBars == true)
              _pixelsAboveBelowBar = _pixelsAboveBelowBar * -1;
              }
              }
              
              
              
              protected override void OnBarUpdate()
              
              {
              if (State == State.Historical && CurrentBar < Count - _BarsToCount)
              
              return;
              
              double _textYStartingPoint = High[0]; //position text above or below bars
              if (_textIsBelowBars)
              _textYStartingPoint = Low[0];
              
              //Add your custom indicator logic here.
              NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 14) { Size = _fontSize, Bold = false };
              
              if (Bars.IsLastBarOfSession) //start counting from first bar of session
              {
              _barCntr = Math.Round(((High[0] - Low[0]) / TickSize) /4);
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              }
              
              else
              _barCntr = Math.Round(((High[0] - Low[0]) / TickSize) /4);
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              
              if (_CalculateOpenLow)
              {
              if (Close[0] > Open[0]) // candle is an up 'green' bar
              
              {
              _barCntr = Math.Round(((Close[0] - Low[0]) / TickSize) /4);
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              }
              else
              _barCntr = Math.Round(((High[0] - Close[0]) / TickSize) /4);
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              
              
              if (_CalculateDecimals)
              {
              if (Close[0] > Open[0]) // candle is an up 'green' bar
              
              {
              _barCntr = ((Close[0] - Low[0]) / TickSize) /4;
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              }
              else
              _barCntr = ((High[0] - Close[0]) / TickSize) /4;
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString(), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              }
              if (_ShowStopDollars)
              {
              if (Instrument.MasterInstrument.Name == "MNQ")
              if (Close[0] > Open[0]) // candle is an up 'green' bar
              
              {
              _barCntr = ((Close[0] - Low[0]) / TickSize) /2;
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString("C"), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              }
              else
              _barCntr = ((High[0] - Close[0]) / TickSize) /2;
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString("C"), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              
              if (Instrument.MasterInstrument.Name == "NQ")
              if (Close[0] > Open[0]) // candle is an up 'green' bar
              
              {
              _barCntr = ((Close[0] - Low[0]) / TickSize) *5;
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString("C"), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              }
              else
              _barCntr = ((High[0] - Close[0]) / TickSize) *5;
              Draw.Text(this, CurrentBar.ToString(), true, _barCntr.ToString("C"), 0, _textYStartingPoint, _pixelsAboveBelowBar, _textColorDefinedbyUser, myFont, TextAlignment.Center, null, null, 1);
              }
              }
              
              if (CurrentBar > 20)
              {
              RemoveDrawObject(CurrentBar.ToString() + (CurrentBar - 21).ToString());
              }
              
              }
              
              [HASHTAG="t3322"]region[/HASHTAG] Properties
              
              [NinjaScriptProperty]
              [Display(Name = "BarsToCount", Description = "Bars To Count", Order = 1, GroupName = "Visual")]
              public int BarsToCount
              {
              get { return _BarsToCount; }
              set { _BarsToCount = value; }
              }
              
              [NinjaScriptProperty]
              [XmlIgnore]
              [Display(Name = "TextColor", Description = "Text Color ", Order = 2, GroupName = "Visual")]
              public Brush TextColor
              {
              get { return _textColorDefinedbyUser; }
              set { _textColorDefinedbyUser = value; }
              }
              
              [Range(4, 40)]
              [NinjaScriptProperty]
              [Display(Name = "TextFontSize", Description = "Text Font Size", Order = 3, GroupName = "Visual")]
              public int TextFontSize
              {
              get { return _fontSize; }
              set { _fontSize = value; }
              }
              
              [Browsable(false)]
              public string TextColorSerializable
              {
              get { return Serialize.BrushToString(TextColor); }
              set { TextColor = Serialize.StringToBrush(value); }
              }
              
              [Range(0, 2000)]
              [NinjaScriptProperty]
              [Display(Name = "PixelsAboveBelow", Description = "Text Offset in Pixels from Bar", Order = 4, GroupName = "Visual")]
              public int PixelsAboveBelow
              {
              get { return Math.Abs(_pixelsAboveBelowBar); }
              set { _pixelsAboveBelowBar = value; }
              }
              
              [NinjaScriptProperty]
              [Display(Name = "TextIsBelowBars", Description = "Display Text Above/Below Bars", Order = 5, GroupName = "Visual")]
              public bool TextIsBelowBars
              {
              get { return _textIsBelowBars; }
              set { _textIsBelowBars = value; }
              }
              
              [NinjaScriptProperty]
              [Display(Name = "CalculateFromOpenLow", Description = "Calculate From Open Low", Order = 6, GroupName = "Visual")]
              public bool CalculateOpenLow
              {
              get { return _CalculateOpenLow; }
              set { _CalculateOpenLow = value; }
              }
              
              [NinjaScriptProperty]
              [Display(Name = "CalculateDecimals", Description = "Calculate With Decimals", Order = 7, GroupName = "Visual")]
              public bool CalculateDecimals
              {
              get { return _CalculateDecimals; }
              set { _CalculateDecimals = value; }
              }
              
              [NinjaScriptProperty]
              [Display(Name = "ShowStopDollars", Description = "Show Stop in Dollars", Order = 8, GroupName = "Visual")]
              public bool ShowStopDollars
              {
              get { return _ShowStopDollars; }
              set { _ShowStopDollars = value; }
              }
              
              #endregion
              }
              }
              Thanks!​

              Comment


                #22
                Hello tradingnasdaqprueba,

                Your drawing object tag is only the bar number.

                Draw.Text(this, CurrentBar.ToString()

                In this case, just supply the bar number.

                RemoveDrawObject(CurrentBar - 21).ToString());

                Try printing

                Print(CurrentBar.ToString());

                Compare this to the output of

                Print(CurrentBar.ToString() + (CurrentBar - 21).ToString());

                The tag name supplied to RemoveDrawObject() needs to match the tag name of the object you want to remove.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #23
                  Hello Chelsea,

                  Still no change... I did it taking my integer so that it changes with the user selected bars to count, but still nothing happens...

                  I would really appreciate if you could help me with the coding...


                  Click image for larger version

Name:	image.png
Views:	149
Size:	58.4 KB
ID:	1273576

                  Thanks!​

                  Comment


                    #24
                    Hello,

                    Try printing Print(CurrentBar.ToString() + (CurrentBar - 21).ToString()); and Print(CurrentBar - (_BarsToCount + 1).ToString().

                    Is this output matching?

                    Comment


                      #25
                      Hello,

                      I get this error:

                      Click image for larger version

Name:	image.png
Views:	150
Size:	18.0 KB
ID:	1273614

                      Comment


                        #26
                        Hello,

                        To clarify, are you trying to subtract _BarsToCount from CurrentBar, and then add 1?

                        If so, you can encapsulate the entire operation in .ToString() this way:

                        (CurrentBar - _BarsToCount + 1).ToString()

                        Comment


                          #27
                          Hi Gaby,

                          Error CS0023

                          Click image for larger version

Name:	image.png
Views:	134
Size:	17.5 KB
ID:	1273712

                          Comment


                            #28
                            Hello,

                            You need to have .ToString inside the Print statement.

                            Print((CurrentBar - _BarsToCount + 1).ToString())

                            Comment


                              #29
                              Hello Gaby,

                              I get this:

                              Click image for larger version

Name:	image.png
Views:	129
Size:	34.8 KB
ID:	1274533

                              Comment


                                #30
                                Hello,

                                This would indicate that the user selected _BarsToCount added by 1 does not equal 21, so it will not remove the 21st bar. Additionally, you are doing a different operation so it is expected that this won't remove the 21st bar.

                                You indicated that you want to subtract _BarsToCount from CurrentBar, and then add 1.

                                RemoveDrawObject((CurrentBar - (_BarsToCount + 1).ToString());

                                This is different from the operation to remove the 21st bar.

                                RemoveDrawObject(CurrentBar - 21).ToString());

                                _BarsToCount would have to be 20 in order to remove the 21st bar that you're looking for. Try printing Print(_BarsToCount + 1) to see what bar number is going to be removed.

                                Comment

                                Latest Posts

                                Collapse

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