Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using DrawnBy

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

    Using DrawnBy

    I am trying to reference lines drawn from another script. I can iterate through them, can print various properties - including DrawnBy - but whenever I try to use DrawnBy in an if-clause, I get an
    Error on calling 'OnBarUpdate' method on bar xxxx: Object reference not set to an instance of an object.​ And the bar reference changes every time I hit refresh script.

    foreach (DrawingTool draw in DrawObjects.ToList() )
    {
    if (draw.ToString().Equals("NinjaTrader.NinjaScript.D rawingTools.Line"))
    {
    i = i + 1;
    Print(draw.DrawnBy);
    }
    }

    ​This works fine, and I can see the DrawnBy property.
    But if I change the if-clause to this, it fails:

    if (draw.DrawnBy.ToString().Equals("MyIndicator" ))

    What am I missing?

    #2
    Hello jculp,

    Thanks for your post.

    I have tested using the code your shared with the if statement if (draw.DrawnBy.ToString().Equals("MyIndicator" ))

    This code will work as long as the name of the indicator being referenced is exactly "MyIndicator". You could use the sample code you shared with the initial if statement to see the exact name of the indicator that is drawing a line.

    For example, if I enable an indicator on a 5-second ES 12-22 chart called DrawLineExample and enable a second indicator that uses your sample code, I can see the following print.

    DrawLineExample(ES 12-22 (5 Second))

    This would need to be passed in for the indicator name in when you use DrawnBy. Such as:

    if (draw.DrawnBy.ToString().Equals("DrawLineExample(E S 12-22 (5 Second))" ))

    Something you could consider is overriding the DisplayName of the indicator that is drawing the line so that you would only need to pass in the DisplayName you set when calling DrawnBy. For example, see below.

    public override string DisplayName
    {
    get { return "MyIndicator"; }
    }


    See the help guide documentation below for more information and sample code.

    DisplayName: https://ninjatrader.com/support/help...isplayname.htm
    DrawBy: https://ninjatrader.com/support/help...t8/drawnby.htm

    Let me know if I may assist further.
    <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


      #3
      Ok thanks for the clarification. I removed everything else from the chart and the code worked as expected.

      Comment


        #4
        A related question: is there a simple way to identify just the most recently added draw object matching a set of criteria? Similar to the if-clause above?

        Comment


          #5
          Hello jculp,

          Thanks for your note.

          There would not be a simple way to identify the most recently drawn object on a chart.

          You would need to either use the barsAgo property or the Anchors time to figure out which is the most recently drawn object on the chart.

          Anchors: https://ninjatrader.com/support/help...t8/anchors.htm

          ChartAnchor: https://ninjatrader.com/support/help...hartanchor.htm

          Let me know if I may assist you further.
          <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

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          648 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          369 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          108 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          572 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          573 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X