Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trouble displaying size of current bar

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

    Trouble displaying size of current bar

    Hi, this indicator is mostly working, but confusingly, it will only update the displayed value when the previous value is exceeded. Each new bar it starts over at 0 and its value will only go up, not back down if a bar gets smaller. Why?

    Code:
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using System.Windows.Media;
    using NinjaTrader.NinjaScript.DrawingTools;
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class CurrentBarBodySize : Indicator
        {
            private double bodySize;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = "Displays the current bar's body size (High - Low) on the top-left of the chart.";
                    Name = "CurrentBarBodySize";
                    Calculate = Calculate.OnEachTick;
                    IsOverlay = true;
                    DisplayInDataBox = false;
                    DrawOnPricePanel = true;
                    PaintPriceMarkers = false;
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 1)
                    return;
    
                // Calculate the current bar's body size (High - Low)
                bodySize = High[0] - Low[0];
    
                // Format the display text
                string displayText = $"{bodySize:F2}";
    
                // Draw the text in the top-left corner
                Draw.TextFixed(this, "CurrentBodyBarSize", displayText, TextPosition.TopLeft);
            }
        }
    }​

    #2
    Hello rrsch,

    The high and low won't update to different values unless a new high or low is observed so it sounds like this is likely working as expected. Each bar will start with the high and low at the same point, from there the high and low will update each time a new value is observed, your text will only have increasing values until the next bar because you are subtracting the low from the high.

    Comment


      #3
      ‍Facepalm.

      That's what I get for using ChatGPT when I'm tired. I need Open - Close I'm assuming. I'll test that out. Thanks!

      Comment

      Latest Posts

      Collapse

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