Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to find user drawn object (hor. line)

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

    how to find user drawn object (hor. line)

    Hi,

    I'd like to find all user drawn horizontal lines and use their anchors for some calculations.
    How can I do that?

    Best regards
    Thomas

    #2
    Hello td_910,

    Thank you for your note.

    You would need to loop through the drawing objects collection, find horizontal lines and check they are user drawn, then you can get the line values from the anchors:

    Code:
    foreach (HorizontalLine line in DrawObjects.ToList())
    {
    // Use ToString().Equals() to check the object's Type
    if (line.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.HorizontalLine") && line.IsUserDrawn == true)
    {
    Print(String.Format("Horizontal Line {0} detected!", line.Tag));
    Print("Line Object: " + line.Tag + " Manually Drawn: " + line.IsUserDrawn);
    Print("Start Bar: " + line.StartAnchor.SlotIndex + " End Bar: " + line.EndAnchor.SlotIndex);
    Print("Price: " + line.StartAnchor.Price + " Time: " + line.EndAnchor.Time);
    }
    }
    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Hello Kate,

      Thanks for your help. If I use that code snippet I get the following error:

      "Error on calling 'OnBarUpdate' method on bar 156: Unable to cast object of type 'NinjaTrader.NinjaScript.DrawingTools.Diamond' to type 'NinjaTrader.NinjaScript.DrawingTools.HorizontalLi ne'."

      There are a couple of other chart objects on that chart as well.

      Comment


        #4
        Hello td_910,

        Thank you for your reply.

        Ah yes, that code is specifically expecting the only drawing tools on the chart to be horizontal lines. Try this:


        Code:
        foreach (DrawingTool draw in DrawObjects.ToList())
        {
        if (draw.ToString().Equals("NinjaTrader.NinjaScript.D rawingTools.HorizontalLine") && draw.IsUserDrawn == true)
        {
        if (draw is DrawingTools.HorizontalLine)
        {
        DrawingTools.HorizontalLine line = draw as DrawingTools.HorizontalLine;
        Print(String.Format("Horizontal Line {0} detected!", line.Tag));
        Print("Line Object: " + line.Tag + " Manually Drawn: " + line.IsUserDrawn);
        Print("Start Bar: " + line.StartAnchor.SlotIndex + " End Bar: " + line.EndAnchor.SlotIndex);
        Print("Price: " + line.StartAnchor.Price + " Time: " + line.EndAnchor.Time);
        }
        }
        }
        Please let us know if we may be of further assistance to you.
        Last edited by NinjaTrader_Kate; 06-24-2022, 10:58 AM. Reason: copied an incorrect version of script

        Comment


          #5
          Hello Kate,
          That one gives a compile error:

          NinjaTrader.NinjaScript.DrawingTools.DrawingTool' does not contain a definition for 'StartAnchor' and no extension method 'StartAnchor' accepting a first argument of type 'NinjaTrader.NinjaScript.DrawingTools.DrawingTool' could be found (are you missing a using directive or an assembly reference?)

          Best regards
          Thomas

          Comment


            #6
            Hello td_910,

            Thank you for your reply.

            Looks like I copied and pasted a "inbetween" version of the code - that's my fault! I've updated the code in my prior post.

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

            Comment


              #7
              Many Thanks!

              Comment


                #8
                Can someone explain to me what this line in the code is doing?

                DrawingTools.HorizontalLine line = draw as DrawingTools.HorizontalLine;

                Comment


                  #9
                  Hello RISKYBUSINEZZ,

                  This line is casting the generic draw class (drawing tool object) as a specific drawing object (HorizontalLine) to use properties specific to HorizontalLine.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    So some beginner questions about to follow: Is the left of the = being cast. Or is the right being cast to the left?

                    So is this telling the code to make all drawn objects "horizontal objects" or does this cause the drawn objects to "handle" like horizontal objects? If the latter, how can that be possible since a rectangle has many parameters a Horizontal line does not?

                    Sorry, this is more programy than it is specific to NT8.

                    Comment


                      #11
                      Hello RISKYBUSINEZZ,

                      draw as DrawingTools.HorizontalLine

                      The above statement a cast.

                      The actual drawing object we are wanting to get information from is a HorizontalLine class object.
                      The foreach is specifying the draw variable object type is DrawingTool (the base class of all drawing tools).
                      The variable is casted from DrawingTool to HorizontalLine to use properties specific to HorizontalLine.

                      Note, this would fall under general C# education.

                      Below is a link to a 3rd party educational site.
                      W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


                      I would highly recommend a formal class on C# to understand the basics of C#.
                      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
                      571 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      331 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
                      549 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      550 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X