Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't Draw a Line on Chart with NinjaScript

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

    Can't Draw a Line on Chart with NinjaScript

    I've been banging my head against a wall trying to get Draw.Line() to work.

    I want to simply draw a line between the highs of bar 0 (current bar) and the bar n bars ago (e.g., 30 bars ago), as shown on the attached snippet. Have had no success

    Below you can find the C# code. What am I doing wrong? Eventually, I'd like to draw a line between the corresponding highs of the Cumulative Delta bars on the second panel. Any guidance on how to do that would be greatly appreciated as well. Thanks in advance!
    PHP Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class JIPDrawingLine : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "JIPDrawingLine";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    Draw.Line(this, "tag1", true, 30, High[30], 0, High[0], Brushes.LimeGreen, DashStyleHelper.Dot, 2);
    }
    }
    } 
    
    Attached Files

    #2
    Hello poctrader,

    Thank you for your post.

    Do you get any errors on the Log tab of the Control Center when you add this indicator to a chart? When I test this out, I am getting an error "Object reference not set to an instance of an object" because at the beginning of the chart when it tries to calculate, there are not enough bars on the chart yet to fulfill the 30 bars ago requirement. It is important to make sure there are enough bars loaded to process your logic, as described on this page:



    Using your example, when I add the following check at the beginning of OnBarUpdate then I am able to get the line to draw properly:

    if (CurrentBar < 30)
    return;

    Checking for errors in the log and using Print() statements are both helpful ways to debug a script. We have a post that goes in-depth about debugging indicators and strategies here:



    Please let us know if we may be of further assistance.

    Comment


      #3
      Thank you, Emily! That did it. Now I'm trying to draw a line on pane 2, where I have the cumulative delta candles. I'm trying to draw a line between the highs of cumulative delta bar 0 (current bar) and the cumulative delta bar n bars ago (e.g., 30 bars ago). The issue now is that the line doesn't render properly. It renders an (almost) horizontal line, way off where the values should be. Here is the code, and a snippet showing what's rendering. Any idea as to what I'm doing wrong? TIA!

      PHP Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class JIPDrawOnCD : Indicator
      {
      
      private OrderFlowCumulativeDelta cumulativeDelta;
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Draw lines on CD pane";
      Name = "JIPDrawOnCD";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      DisplayInDataBox = true;
      DrawOnPricePanel = false;
      DrawHorizontalGridLines = true;
      DrawVerticalGridLines = true;
      PaintPriceMarkers = true;
      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
      //See Help Guide for additional information.
      IsSuspendedWhileInactive = true;
      }
      else if (State == State.Configure)
      {
      AddDataSeries(Data.BarsPeriodType.Tick, 1);
      }
      else if (State == State.DataLoaded)
      {
      
      cumulativeDelta = OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
      
      }
      }
      
      protected override void OnBarUpdate()
      {
      //Add your custom indicator logic here.
      
      
      if (BarsInProgress == 0)
      {
      if (CurrentBar > 30)
      {
      Draw.Line(this, "tag2", true, 30, cumulativeDelta.High[30], 0, cumulativeDelta.High[0], Brushes.LimeGreen, DashStyleHelper.Dot, 2);
      
      
      }
      
      
      
      
      
      }
      
      //else current bars index is 1 then do work on the cumulative delta
      else if (BarsInProgress == 1)
      {
      // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
      cumulativeDelta.Update(cumulativeDelta.BarsArray[1].Count - 1, 1);
      }
      
      
      }
      }
      } 
      
      Attached Files

      Comment


        #4
        Hello poctrader,

        Thank you for your reply.

        I am noticing two things when comparing your code and your screenshot.
        1. Your screenshot shows an Order Flow Cumulative Delta set to a Bid Ask delta with a Session period type, yet your code has cumulativeDelta saved as a Bid Ask delta with a Bar period type
        2. You are referencing data points cumulativeDelta.High[30] and cumulativeDelta.High[0], although to get the high value of the delta you should use cumulativeDelta.DeltaHigh[int barsAgo] as mentioned in the help guide page under "Returns High value" found here: https://ninjatrader.com/support/help...ive_delta2.htm
        It is important to reference the DeltaHigh in your script and plot the same type of cumulative delta indicator on your chart for your check against the line values and the delta high values. I suspect these changes should resolve your inquiry.

        Please let me know if I may be of further assistance.

        Comment


          #5
          Thanks! It worked!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          558 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
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          545 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          547 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X