Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bar Index Number indicator

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

    Bar Index Number indicator

    When building indicators and strategies, referring to the correct part is the crucial step.

    My idea is to build a Bar Index Number indicator, that paints bar index number at the bottom of the chart, something like this:
    - As you can see, the most right bar is marked as "/", as it's live bar and it is yet to close to be reffered as Time[0] (it can be empty in the indicator)
    - Then, from right to left should be numbers from 0 to TOTAL (user input integer, in order to save up space and do not paint bar index numbers for 500+ bars)
    - It may also be beneficial to have those numbers rotated 90 degrees, as it would be hard to fit 3 digit numbers like 110 into one bar.

    How can this be achieved?

    Also, it is essential, that numbers correspond to the bar index correctly, that is if I input Time[BarIndex] it should return correct value.
    Attached Files

    #2
    Hello UltraNIX,

    For what you described you could use Draw.Text for a basic version however that would not be at the bottom of the chart specifically. The right side text could be achieved with Draw.TextFixed.

    For more advanced text you could use OnRender to draw text in custom ways or have custom placement/rotations. That would allow all numbers to be placed at the bottom of the chart panel instead of based on price data. You can see the indciator SampleCustomRender for some basic examples of using OnRender and positioning.

    The bar index would just be CurrentBar. Time[BarIndex] is not valid, time takes a BarsAgo not an index. if you wanted to get a value by the index you need GetValueAt: Time.GetValueAt(barIndex).





    Comment


      #3
      Rotate text at 90 degrees?

      Comment


        #4
        Hello UltraNIX,

        Sure thats possible, you can search online for guides for SharpDX in general which is the rendering library NinjaTrader uses. You may need to restructure some of the code because the variables are named slightly different or in a different NinjaTrader specific location. I reformatted the following example i found by searching for "SharpDX rotate text".
        I wondered how to rotate a text rendered with Direct2D with SharpDX. Can not find any possiblity in RenderTarget2D.DrawText() or RenderTarget2D.DrawTextLayout()


        This is a simplified copy of the code from SampleCustomRender including the code from that stackoverflow link.

        Code:
        protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
        {
            NinjaTrader.Gui.Tools.SimpleFont simpleFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12);
           SharpDX.DirectWrite.TextFormat textFormat1 = simpleFont.ToDirectWriteTextFormat();
           SharpDX.Vector2 upperTextPoint = new SharpDX.Vector2(ChartPanel.X + 10, ChartPanel.Y + 20);
           SharpDX.DirectWrite.TextLayout textLayout1 = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Gl obals.DirectWriteFactory, "My Text", textFormat1, ChartPanel.X + ChartPanel.W, textFormat1.FontSize);
        
        [B]SharpDX.Matrix3x2 currentTransform = RenderTarget.Transform;[/B]
        [B]RenderTarget.Transform = SharpDX.Matrix3x2.Rotation(90, new SharpDX.Point(100, 100));[/B]
        
           RenderTarget.DrawTextLayout(upperTextPoint, textLayout1, Brushes.Red.ToDxBrush(RenderTarget), SharpDX.Direct2D1.DrawTextOptions.NoSnap);
        
        [U][B]RenderTarget.Transform = currentTransform;[/B][/U]
        }

        Comment


          #5
          I finally tested the code. And it's not working as expected:
          1) It only displays text once, and I need bar number on each bar of the screen.
          2) it is placed on the top and to the left, while I need to have it at the bottom
          3) Rotation angle is also incorrect, from a visual perspective it's like 10 degrees off.

          I am attaching screenshot.

          Comment


            #6
            I worked with the code myself and I am able to achieve text at the very bottom, get correct numbers at the corresponding bars, but as I do it via Draw.Text within OnBarUpdate, I don't know a way to rotate each Draw.Text object by 90 degrees.

            Comment


              #7
              Hello UltraNIX,

              1) It only displays text once, and I need bar number on each bar of the screen.
              2) it is placed on the top and to the left, while I need to have it at the bottom
              3) Rotation angle is also incorrect, from a visual perspective it's like 10 degrees off.
              These are all items you would have to change if you wanted to display the text differently. The purpose of that sample was simply to rotate text from OnRender, you can make whatever changes you need to that to accommodate your goal. The concept would be the same if you wanted to display other text and rotate them, that would be the general workflow to do that.

              Also keep in mind that the term "Degrees" does not make sense in contrast to bar placement/scaling of the chart. Degrees would be the amount of rotation the text does, that won't align specifically with the chart as it can be scaled both vertically and horizontally. The rotation wont take account of the scales to rotate differently with chart as its scaled. Thats just 90 degrees from the point you specify with the text object. You can choose a different center for the rotation by changing the values in the code: SharpDX.Point(100, 100)

              but as I do it via Draw.Text within OnBarUpdate, I don't know a way to rotate each Draw.Text object by 90 degrees.
              You can't rotate the drawing object, you would need to use OnRender for this task. You can make a duplicate drawing tool and change its OnRender code to rotate the text. You can additionally re use the OnRender code the Draw.Text already has if you wanted to do exactly the same rendering/placement in your indicator.


              Comment


                #8
                I found this on the forum, originally coded by Bryan Cass. I updated it so the Offset is based off the bottom of the panel as a percentage of the panel height. It works in any panel. Updated 2/24/15: The text is better centered on the bar. Added a Bold text option. NOTE: This contains […]






                Al Brooks style Bar Counter which places the number of the bar below every other bar starting at 1 for the first bar of each session. You can change the text size, color, place text below or above price bars, change the distance below/above a price bar, show either odd or even numbers.
                Last edited by PaulMohn; 09-12-2024, 01:57 AM.

                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