Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

ns Looping through the collection to find specific draw objects

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

    ns Looping through the collection to find specific draw objects

    Hi,

    I am using the below template to ns Looping through the collection to find specific draw objects:
    https://ninjatrader.com/support/help...rawobjects.htm

    This example loops through all drawing objects OnBarUpdate and old objects are looped on each bar.
    Would you please help how to get only the new drawing objects on each bar update?


    #2
    Hello pvincent,

    A new object would entail that it has a new tag name, only uniquely named objects will be drawn as a new object. If the same tag is reused the old drawing object would be updated and remains in the collection. This question depends on how the objects that you are trying to find work in the script that draws them or if they are manually drawn.

    In either case there is no "new" objects list, the collection contains all objects. If you wanted to know only new objects you would have to have stored the collection of items to a variable at some point in the past. You could then compare the tags from the old collection to the tags in the new collection to find existing objects. Anything left after that would be considered new since you last stored the collection as a variable.

    If you otherwise use a specific naming convention with new objects, like dot1, dot2,dot2 etc you could use logic to extract the names of the objects to determine a highest count from those objects. Once you know what count that the existing tags go up to you could predict the next object or have a limit to know when new objects have been added.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Understood. Thank you.

      Can you please confirm how to find the time of the object?

      I see the Anchor Time in the Properties window. Is it possible to get that time value in the script?

      Comment


        #4
        Hello pvincent,

        You can access any of the existing anchor properties by finding the anchor in question and then accessing its properties. If you are using the generic IDrawingTool then it has an anchors collection:
        https://ninjatrader.com/support/help...rawingtool.htm

        https://ninjatrader.com/support/help...tm#chartanchor

        To otherwise access specific anchors that a tool has like start or end anchor you would have to cast to that tools type:

        Code:
          DrawingTools.Line globalLine = draw as DrawingTools.Line;
        You can then access any specific properties to that type, in the above example that would be the Line type.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you.

          Comment


            #6
            Hi,

            When I use the code to get the drawing objects, it lists only the ones that are drawn manually.
            However I am unable to cast the objects that are drawn from another custom indicator.

            GetType().Name shows it is a Text/Line/Rectangle.
            But when I cast it to the corresponding object I am getting error.



            ***


            foreach (DrawingTool draw in DrawObjects.ToList())
            <<Objects from the custom indicator shows up here>>
            {
            if (draw is DrawingTools.Line)
            <<Objects from the custom indicator DOES NOT show up here. GetType().Name shows it is a Text/Line/Rectangle. >>
            ***




            Comment


              #7
              Hello pvincent,

              What error are you seeing?
              JesseNinjaTrader Customer Service

              Comment


                #8
                Sorry, I do not get error when casting.
                Though the GetType().Name shows it is a Text/Line/Rectangle, it does not go inside this condition: if (draw is DrawingTools.Line)

                Same with other objects as well.
                But it isworking for manually drawn obejcts.

                Below is an example of the object drawn from an indicator that I am trying to get the anchor values.


                Click image for larger version

Name:	object.png
Views:	4
Size:	11.4 KB
ID:	1206913



                Comment


                  #9
                  This is happening for manually drawn objects as well.
                  I am not able to get the anchor values because it is not going inside the condition "if (draw is DrawingTools.Rectangle)"
                  I have attached manually drawn object in chart, code and output.
                  I am not getting any errors.

                  Code:

                  if (DrawObjects.Count > 0)
                  {
                  foreach (DrawingTool draw in DrawObjects.ToList())
                  {
                  Print ("Type name: "+draw.GetType().Name);

                  if (draw is DrawingTools.Rectangle)
                  {
                  Print ("Tag name: "+draw.Tag);
                  DrawingTools.Rectangle temp = draw as DrawingTools.Rectangle;

                  Print("startYPrice: " + temp.StartAnchor.Price);
                  Print("startXTime: " + temp.StartAnchor.Time);
                  Print("endYPrice: " + temp.EndAnchor.Price);
                  Print("endXTime: " + temp.EndAnchor.Time);

                  // check to see if it's the rectangle the indicator drew for the current date
                  if (temp.StartAnchor.Time.Date == Time[0].Date)
                  {
                  StartY = temp.StartAnchor.Price;
                  EndY = temp.EndAnchor.Price;
                  Print("StartY : " + StartY + " EndY: " + EndY + "in loop");
                  }
                  }
                  }
                  Click image for larger version

Name:	object.png
Views:	4
Size:	3.8 KB
ID:	1206917
                  }


                  Output:

                  Type name: Rectangle



                  Comment


                    #10
                    Hello pvincent,

                    Depending on the use case you may need to use dynamic instead of casting. You can see an example of that concept here:

                    https://ninjatrader.com/support/help...ub=drawobjects
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Thank you.
                      I will check this.

                      Comment


                        #12
                        Hi,

                        Thank you very much.
                        I was able to get the object information when using the dynamic as recommended.

                        I would like to get the color of the object as well. But I am not sure how to get that.
                        Would you please help?



                        if (draw.ToString().Equals("NinjaTrader.NinjaScript.D rawingTools.Rectangle")
                        )
                        {
                        // Cast line as dynamic and access the object by assuming that it is the Type we expect
                        dynamic temp = draw as dynamic;
                        string strTag = temp.Tag;
                        StartPrice = temp.StartAnchor.Price;
                        EndPrice = temp.EndAnchor.Price;




                        Comment


                          #13
                          Hello pvincent,

                          You would just specify the property name similar to what you have provided in your post using the temp object. Depending on which brush you are trying to get would determine the name of the property. A line for example has the Stroke property.



                          JesseNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by tarikmedjber, Today, 12:27 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post tarikmedjber  
                          Started by trader3000a, Today, 12:14 PM
                          0 responses
                          3 views
                          0 likes
                          Last Post trader3000a  
                          Started by 7robert, Today, 12:05 PM
                          0 responses
                          5 views
                          0 likes
                          Last Post 7robert
                          by 7robert
                           
                          Started by olliger, Today, 11:52 AM
                          0 responses
                          7 views
                          0 likes
                          Last Post olliger
                          by olliger
                           
                          Started by diegobernardes, Today, 04:02 AM
                          3 responses
                          11 views
                          0 likes
                          Last Post NinjaTrader_Oxana  
                          Working...
                          X