Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Write Text Question (Current Bar)

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

    Write Text Question (Current Bar)

    Objective:

    Write Text at Top Right of Screen on Current Bar and have the Text
    stay with the Bar it started on.

    As per...

    I've looked quite a bit, including the library, but just not finding what I need. Text.Fixed gets the location but of course it stays there. What I want to do is write text, place it in that start location, and then allow it to stay with the candle as the session progresses.

    Just that exact line of code is what I'm after.

    The following gives me a Text Box which I have put in the top right position.


    Draw.TextFixed(this, @"My Object", @"My Text", GetTextPosition(Location), InfoBrush, Font, Brushes.Cyan, Brushes.Transparent, 0);


    It would be nice if I can continue to call additional variables as I am doing above, but not critical. Bottom line is I want text written to start at top right of screen & then follow along with the bar timeframe it started on for the rest of the session.

    FTR, I already have kill-switch logic in place for end of session...

    Thanx
    JM

    #2
    The trouble is keeping it at the TOP of the screen as it works its way left. Draw.TextFixed does not work this way (locked to a bar number on the X axis but locked to the top of the screen on the Y axis) and neither does Draw.Text, so what you'll need to do is to implement this in a custom OnRender. In OnRender, if you know the bar number in question, you can use chartControl.GetXByBarIndex to get the X position and chartScale.GetYByValue(chartScale.MaxValue) to get the top, or you can also get the top by understanding that the upper left is (0, 0) and working your way down from there. You'll want to take into account the dimensions of the font if you're trying to make it exactly at the top.
    Last edited by QuantKey_Bruce; 04-30-2023, 09:57 AM.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Thank you Bruce, if you have a line of code which implements this I can work off of that. Thanks for taking the time on a Sunday
      JM

      Comment


        #4
        This could certainly be cleaned up and improved, but it ought to get you started thinking about this in the right direction.
        Code:
        #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.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 TextOnBarAtTopExample : Indicator
            {
                private int MarkBarIndex = -1;
                private string MarkBarText = "";
                private SolidColorBrush MarkBarBrush = Brushes.Blue;
        
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Name = "Text On Bar At Top Example";
                        IsOverlay = true;
                    }
                    else if (State == State.Transition)
                    {
                        // here we are at the end of the historical bars, so let's make a note of the bar number we want to mark, and what we want to say there
                        MarkBarIndex = CurrentBars[0] - 10; // we'll arbitrarily pick a bar ten bars back from the start of realtime and stick with that bar from now on
                        MarkBarText = "THIS BAR" + System.Environment.NewLine +
                            "RIGHT HERE" + System.Environment.NewLine +
                            "IS THE ONE" + System.Environment.NewLine +
                            "|" + System.Environment.NewLine +
                            "V";
        
                        if (CurrentBars[0] >= 10) Draw.Text(this, "This is a tag", false, "X", 10, Closes[0][10], 0, MarkBarBrush, new SimpleFont("System", 10),
                            TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
                    }
                }
        
                protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                {
                    base.OnRender(chartControl, chartScale);
        
                    // mark the text in question by centering it on the bar
                    if (MarkBarIndex >= 0 && MarkBarText != "")
                    {
                        SimpleFont TextFont = chartControl.Properties.LabelFont ?? new SimpleFont();
        
                        FormattedText FormattedText = new FormattedText(MarkBarText, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                            TextFont.Typeface, TextFont.Size, MarkBarBrush, VisualTreeHelper.GetDpi(chartControl).PixelsPerDip);
        
                        float FormattedTextWidth = (float)FormattedText.Width;
                        float FormattedTextHeight = (float)FormattedText.Height;
        
                        using (SharpDX.Direct2D1.Brush TextBrushDX = MarkBarBrush.ToDxBrush(RenderTarget))
                        using (SharpDX.DirectWrite.TextFormat TextFormatDX = new SharpDX.DirectWrite.TextFormat(Core.Globals.DirectWriteFactory, TextFont.Family.ToString(),
                            (TextFont.Bold ? SharpDX.DirectWrite.FontWeight.Bold : SharpDX.DirectWrite.FontWeight.Normal),
                            (TextFont.Italic ? SharpDX.DirectWrite.FontStyle.Italic : SharpDX.DirectWrite.FontStyle.Normal),
                            SharpDX.DirectWrite.FontStretch.Normal, (float)TextFont.Size) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Center })
                        {
                            int BarX = chartControl.GetXByBarIndex(ChartBars, MarkBarIndex);
                            int TopY = 0; // also chartScale.GetYByValue(chartScale.MaxValue);
        
                            RenderTarget.DrawText(MarkBarText, TextFormatDX, new SharpDX.RectangleF(BarX - FormattedTextWidth / 2, TopY, FormattedTextWidth, FormattedTextHeight),
                                TextBrushDX);
                        }
                    }
                }
            }
        }
        ​
        Screenshot:
        Click image for larger version

Name:	image.png
Views:	423
Size:	31.3 KB
ID:	1249112
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Awesome,
          thank you sir

          Comment


            #6
            Hello johnMoss,

            Thanks for your post.

            QuantKey_Bruce is correct, this would require custom rendering text using RenderTarget.DrawText() in OnRender. The sample code that QuantKey_Bruce shared demonstrates how you could go about accomplishing your goal using OnRender.

            See the help guide documentation below for more information. You may also view the SampleCustomRender indicator that comes default with NinjaTrader by opening a New > NinjaScript Editor window, opening the Indicators folder, and double-clicking on the SampleCustomRender help guide.

            OnRender: https://ninjatrader.com/support/help...8/onrender.htm
            Using SharpDX in OnRender: https://ninjatrader.com/support/help..._rendering.htm
            RenderTarget.DrawText: https://ninjatrader.com/support/help...t_drawtext.htm
            GetXByBarIndex: https://ninjatrader.com/support/help...bybarindex.htm
            GetYByValue: https://ninjatrader.com/support/help...etybyvalue.htm
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Thank you

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              600 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              347 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
              558 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              558 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X