Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trouble with Draw.Text with OnRender

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

    Trouble with Draw.Text with OnRender

    I have what I think is a simple problem, but can't figure out how to solve it. I'm trying to create an index across the bottom of my chart. I need to estimate how many bars back an event occurs when I am scanning through a chart.

    Click image for larger version

Name:	image.png
Views:	627
Size:	92.7 KB
ID:	1259300
    It's easy to do in OnBarUpdate()


    PHP Code:
    protected override void OnBarUpdate()
    {
    if(CurrentBar < 500) return;
    
    double y = .2;
    int x = 10;
    
    Draw.Text(this, "50", true, "50", 50, y, x, Brushes.White, displayFont, TextAlignment.Center,
    Brushes.Transparent, Brushes.Transparent, 0);
    Draw.Text(this, "100", true, "100", 100, y, x, Brushes.White, displayFont, TextAlignment.Center,
    Brushes.Transparent, Brushes.Transparent, 0);
    Draw.Text(this, "150", true, "150", 150, y, x, Brushes.White, displayFont, TextAlignment.Center,
    Brushes.Transparent, Brushes.Transparent, 0);
    Draw.Text(this, "200", true, "200",200, y, x, Brushes.White, displayFont, TextAlignment.Center,
    Brushes.Transparent, Brushes.Transparent, 0);
    Draw.Text(this, "250", true, "250", 250, y, x, Brushes.White, displayFont, TextAlignment.Center,
    Brushes.Transparent, Brushes.Transparent, 0);
    Draw.Text(this, "300", true, "300", 300, y, x, Brushes.White, displayFont, TextAlignment.Center,
    Brushes.Transparent, Brushes.Transparent, 0);
    Draw.Text(this, "350", true, "350", 350, y, x, Brushes.White, displayFont, TextAlignment.Center,
    Brushes.Transparent, Brushes.Transparent, 0);
    } 
    


    But I would like to scroll back and have the index still there based on the bar at the right edge of the screen. So I moved to OnRender()

    I thought this would do it

    PHP Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    // if(ChartBars.FromIndex < 1000) return; // I don't need the indexing to start early, thought this would help but it didn't
    
    int totalBars = ChartBars.Count; // the total number of bars on the chart
    int rightBar = ChartBars.ToIndex; // the right bar visible on the chart
    
    int i200 = 200 + totalBars - rightBar; // this is bar 200 from the right edge
    
    
    Draw.TextFixed(this, "scale", rightBar + " " + totalBars + " " + i200,
    TextPosition.TopRight, Brushes.White,
    displayFont, Brushes.Transparent, Brushes.Transparent, 0);
    
    double y = .2;
    int x = 10;
    
    Draw.Text(this, "200", true, "200", i200,
    y, x, Brushes.White, displayFont,
    TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
    
    } 
    

    I am able to correctly get the bar number for bar 200 on the chart (i200)
    Click image for larger version

Name:	image.png
Views:	397
Size:	88.2 KB
ID:	1259301
    But when I try to use i200 in Draw.Text, I get the error
    Click image for larger version

Name:	image.png
Views:	395
Size:	5.2 KB
ID:	1259302

    I know what to do in OnBarUpdate()... Put an if statement with a return until I have enough bars on the chart to do the calculation.

    But I am able to calculate and display i200 in a Draw.TextFixed() box in the lower right corner. So its processing correctly at that point.

    But when I use Draw.Text() it errors out.

    Help please
    ​​

    #2
    Hello cre8able,

    Drawing tools should not be called from OnRender(). This is for custom rendering only.

    You would use RenderTarget.DrawText() instead.


    Included with NinjaTrader is the SampleCustomRender indicator which demonstrates custom rendering.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      thanks ChelseaB. I appear to be in over my head on what I thought was a simple plan. I struggled through the documentation and can print on the screen using RenderTarget.DrawText(). My problem is putting it where I want it. For example, I want to pint a "100" at the bottom of my chart on the current bar 100 as measured from the left edge of the screen.

      I googled and searched the forum for any help or examples on how to read the bar location then pass it to RenderTarget.DrawText() but struck out.

      Can anyone point me in the right direction?

      Comment


        #4
        Hello cre8able,

        Below is a link to an example of a scrollable object that is updated with bar coordinates.


        The X value would come from ChartControl.GetXByBarIndex(). The Y value would be chartScale.Height minus the height of the text to be at the bottom of the chart.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          hey Chelsea
          I'm getting close but since I'm pretty much copying you I ran into an error I don't know how to clear. I know when writing an indicator I need to make sure it has enough bars before I start calculating so I use something like:

          PHP Code:
              protected override void OnBarUpdate()
                  {
                      if (CurrentBar < 500) return;&#8203; 
          



          This is the error message

          .​Click image for larger version  Name:	image.png Views:	0 Size:	13.7 KB ID:	1260120

          I thought this would fix it

          Click image for larger version  Name:	image.png Views:	0 Size:	498.2 KB ID:	1260122​​

          PHP Code:
          namespace NinjaTrader.NinjaScript.Indicators
          {
              public class RenderScrollableObjectExample : Indicator
              {
                  private SharpDX.Direct2D1.Brush        brushDx;
                  private Point                        endPoint;
                  private int                            lastBarNum;
                  private double                        lastPrice;
                  private Point                        startPoint;
          
                  protected override void OnStateChange()
                  {
                      if (State == State.SetDefaults)
                      {
                          Description                                    = @"Enter the description for your new custom Indicator here.";
                          Name                                        = "RenderScrollableObjectExample";
                          Calculate                                    = Calculate.OnBarClose;
                          IsOverlay                                    = true;
                          DisplayInDataBox                            = false;
                          IsSuspendedWhileInactive                    = true;
                          BarsRequiredToPlot                            = 100;
                      }
                      else if (State == State.DataLoaded)
                      {
                          lastBarNum = 0;
                      }
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (State == State.Historical && Count > 100 && CurrentBar == Count - 2)
                      {
                          lastBarNum    = CurrentBar;
                          lastPrice    = Close[0];
                      }
                  }
          
                  public override void OnRenderTargetChanged()
                  {
                      if (brushDx != null)
                          brushDx.Dispose();
          
                      if (RenderTarget != null)
                      {
                          try
                          {
                              brushDx = Brushes.Blue.ToDxBrush(RenderTarget);
                          }
                          catch (Exception e) { }
                      }
                  }
          
                  protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                  {
                      if (!IsVisible)
                          return;
          
                      if (!IsInHitTest && lastBarNum > 0)
                      {
          /*                // start point is 10 bars back and 5 ticks up from 2nd to last bar and price
                          startPoint        = new Point(ChartControl.GetXByBarIndex(ChartBars, (lastBarNum - 10)), chartScale.GetYByValue(lastPrice + 5 * TickSize));
                          // end point is 2nd to last bar and 5 ticks down from price
                          endPoint        = new Point(ChartControl.GetXByBarIndex(ChartBars, (lastBarNum)), chartScale.GetYByValue(lastPrice - 5 * TickSize));
          */
                          startPoint        = new Point(ChartControl.GetXByBarIndex(ChartBars, (ChartBars.ToIndex - 50)), chartScale.Height - 5);
                          endPoint        = new Point(ChartControl.GetXByBarIndex(ChartBars, (ChartBars.ToIndex - 50)), chartScale.Height - 10);
          
          
                          SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode;
          
                          RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive;
                          RenderTarget.DrawLine(startPoint.ToVector2(), endPoint.ToVector2(), brushDx, 15);
          
                          RenderTarget.AntialiasMode = oldAntialiasMode;
                      }
          
                      base.OnRender(chartControl, chartScale);
                  }
              }
          }&#8203;​ 
          


          Since changing those 2 counters didn't clear my error, can you point me to where I need to account for the use of "ChartBars.ToIndex - 50"

          thanks again
          cre8able
          Attached Files
          Last edited by cre8able; 07-12-2023, 04:39 PM.

          Comment


            #6
            Hello cre8able,

            You will need to use whichever is greater ChartBars.ToIndex - 50 or ChartBars.FromIndex.

            The index must be a visible and valid bar.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              thank you for the help ChelseaB

              Comment

              Latest Posts

              Collapse

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