Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw plot values in OnRender

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

    Draw plot values in OnRender

    Hi,
    I'm trying to print bar numbers at the bottom or top of the chart within OnRender, but the code throws an index out of range error.

    Code:
    protected override void OnBarUpdate()
    {
    Values[0][0] = Bars.BarsSinceNewTradingDay;
    }
    
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    base.OnRender(chartControl, chartScale);
    
    SharpDX.Direct2D1.Brush dxBrush = FNTcolor.ToDxBrush(RenderTarget);
    TextFormat textFormat = TextFontta.ToDirectWriteTextFormat();
    double y = DrawAtTop ? 5 + TextOffset * textFormat.FontSize : ChartPanel.H - 35 - TextOffset * textFormat.FontSize;
    double startX = -1;
    int daysOnChart = 0;
    
    for (int barIndex = ChartBars.FromIndex; barIndex < ChartBars.ToIndex; barIndex++)
    {
    if (barIndex > 0 && barIndex < ChartBars.ToIndex)
    {
    string myString = barIndex.ToString("N0");
    // string myString = Values[0][barIndex].ToString("N0");
    
    startX = chartControl.GetXByBarIndex(ChartBars, barIndex);
    Point startPoint = new Point(startX, y);
    TextLayout textLayout = new TextLayout(Globals.DirectWriteFactory, myString, textFormat, TextWidth , textFormat.FontSize);
    RenderTarget.DrawTextLayout(startPoint.ToVector2() , textLayout, dxBrush);
    textLayout.Dispose();
    }
    }
    
    textFormat.Dispose();
    }

    Printing the barIndex works, but not printing the plot values.

    string myString = barIndex.ToString("N0");
    // string myString = Values[0][barIndex].ToString("N0");​

    I'd appreciate any help.

    Thomas

    #2
    Hello Thomas,

    The issue is likely from calling Values[0][barIndex] without using TriggerCustomEvent() from the non-data-driven method.
    Help guide: NinjaScript > Language Reference > Common > TriggerCustomEvent()

    I would recommend instead using Values[0].GetValueAt(CurrentBar - barIndex).ToString() instead of calling TriggerCustomEvent() as this will use less CPU demand.
    Help guide: NinjaScript > Language Reference > Common > ISeries<T> > GetValueAt()
    Help guide: NinjaScript > NinjaScript Best Practices​ > Error handling practices > barsAgo indexer vs. absolute bar Index
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      Many thanks. That works perfectly.
      One more question: Is it possible to format the values of a plot in the Data Box?
      E.g. the bar numbers show up as 1.00 for bar number 1.
      As far as I know there are no int plots?

      Originally posted by NinjaTrader_ChelseaB View Post
      Hello Thomas,

      The issue is likely from calling Values[0][barIndex] without using TriggerCustomEvent() from the non-data-driven method.
      Help guide: NinjaScript > Language Reference > Common > TriggerCustomEvent()

      I would recommend instead using Values[0].GetValueAt(CurrentBar - barIndex).ToString() instead of calling TriggerCustomEvent() as this will use less CPU demand.
      Help guide: NinjaScript > Language Reference > Common > ISeries<T> > GetValueAt()
      Help guide: NinjaScript > NinjaScript Best Practices​ > Error handling practices > barsAgo indexer vs. absolute bar Index

      Comment


        #4
        Hello td_910,

        Indicator Plot Series are always Series<double>.

        To control the decimals for a plot override the FormatPriceMarker() method.
        Below is a link to the help guide.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

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