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

Get data from rectangle

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

    Get data from rectangle

    Hello,

    I need to get data from a rectangle that is drawn by an indicator in chart.

    I need to get the data as this appear in drawing objects (see attached screenshot): color, start time, start Y, end time, end Y and then assign these values to respective variables to process later in the logic of the strategy.

    Could I see the code to do this?

    Thanks in advance
    Attached Files

    #2
    Hello federicoo,

    Thank you for the post.

    We have an example of working with the drawing objects in the following page: https://ninjatrader.com/support/help...ub=drawobjects

    Keep in mind this only works in realtime and if other indicators are drawing the objects they may not load in the same order meaning that if your script loads before the other indicator it may not see the objects initially. This should generally be able to be used going forward in time in realtime after the scripts have loaded.


    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      It is not working, may be the type of drawing is not a rectangle... how can I retrieve the type of drawing made by the indicator?

      Thanks

      Comment


        #4
        Hello federicoo,

        The samples in the page I linked show how to do that, what did you try that is not working?

        You mentioned maybe the object is not a rectangle, what kind of object is being drawn? If you open the Right click menu -> Drawing Tools -> Drawing objects menu does it appear?

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello federicoo,

          The samples in the page I linked show how to do that, what did you try that is not working?

          You mentioned maybe the object is not a rectangle, what kind of object is being drawn? If you open the Right click menu -> Drawing Tools -> Drawing objects menu does it appear?

          I look forward to being of further assistance.
          I did that, it is the screenshot attached, after Right click menu -> Drawing Tools -> Drawing objects...
          Attached Files

          Comment


            #6
            Hello federicoo,

            Thank you for the post.

            It looks like the object is present and appears to be a rectangle, what did you try that is not working?


            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you Jesse, but it seem like this third-party indicator is hiding the rectangle data, so I can not get the anchor values from these rectangles.

              Comment


                #8
                Hello federicoo,

                I am not sure what you mean, if you can access the drawing object you should be able to access its anchors. Are you having trouble accessing the rectangle or the anchors of the rectangle in code? Can you provide a sample of what you tried so we can get a better idea of the problem?

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  I had been testing a code by Paul on this thread

                  Hello! How I can get Rectangle coordinates. protected override void OnBarUpdate() { foreach (DrawingTool draw in DrawObjects.ToList())


                  But not success with that TPO indicator... so, I don't know if there is a solution to this.

                  Thanks

                  Comment


                    #10
                    Hello federicoo,

                    I couldn't really say without seeing what you actually coded. The other post is not necessarily helpful because that only shows what they had used but does not tell me what you coded. If you can attach a sample of what you are trying specifically that will help to make sure you are using the right code to know what to check next.

                    The image you provided shows the drawing object in the collection so if you have the script applied to that chart and have let it run in Realtime it should be able to see it. Have you tried using a Print in the drawing object loop from the help guide?

                    Code:
                     foreach (DrawingTool draw in DrawObjects.ToList())
                      {
                        Print(draw.ToString());
                    }
                    The image you provided also does not show where your indicator is applied, if you have this in a different chart it won't see that object.

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

                    Comment


                      #11
                      Hello Jesse,

                      This is the code:

                      ================================================== ===========================
                      protected override void OnBarUpdate()
                      {
                      //************************************************** ****
                      // BLOCK 1

                      foreach (DrawingTool draw in DrawObjects.ToList())
                      {
                      Print(draw.ToString());
                      }

                      //************************************************** ****

                      //************************************************** ****
                      // BLOCK 2

                      foreach (DrawingTool draw in DrawObjects.ToList()) // Go through objects
                      {
                      if (draw is DrawingTools.Rectangle) // test to find rectangle
                      {
                      Print ("Tag name: "+draw.Tag); // priint tag name
                      DrawingTools.Rectangle temp = draw as DrawingTools.Rectangle; // create copy to get anchorpoints

                      Print("startY: " + temp.StartAnchor.Price);
                      Print("startX: " + temp.StartAnchor.Time);

                      Print("endY: " + temp.EndAnchor.Price);
                      Print("endX: " + temp.EndAnchor.Time);
                      }
                      }

                      //************************************************** ****

                      }
                      ================================================== ===============================================

                      BLOCK 1 works perfectly, this identifies the rectangle as what this is really (see output attached).

                      BLOCK 2: does not work... it would identify the rectangle and get the anchors from this... the if statement si never true so it doesn't find the rectangle that, curiously, is finding in BLOCK 1...

                      I can't get a logic explanation to this issue.

                      Thanks
                      Attached Files

                      Comment


                        #12
                        Hello federicoo,

                        Please try using the syntax shown in the following sample for the Block 2 portion of your code: https://ninjatrader.com/support/help...considerations

                        Rather than casting the type to a rectangle try checking its type by using a string representation:

                        Code:
                        foreach (dynamic obj in DrawObjects.ToList())
                        {
                          if (obj.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Rectangle"))
                          {
                              // Access the object by assuming that it is the Type we expect
                              Print(String.Format("rect {0} detected!", obj.Tag));
                          }
                        }
                        Does this allow for your condition to continue?

                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          wow! I have to say that it was wonderful! hehehe... Thank you very much, it solve the problem and I can get the anchors from rectangle finally!

                          The final code is the following:

                          protected override void OnBarUpdate()
                          {
                          //************************************************** ****
                          // BLOCK 1

                          foreach (DrawingTool draw in DrawObjects.ToList())
                          {
                          Print(draw.ToString());
                          }

                          //************************************************** ****

                          //************************************************** ****
                          // BLOCK 2

                          foreach (DrawingTool draw in DrawObjects.ToList()) // Go through objects
                          {
                          if (draw is DrawingTools.Rectangle) // test to find rectangle
                          {
                          Print ("Tag name: "+draw.Tag); // priint tag name
                          DrawingTools.Rectangle temp = draw as DrawingTools.Rectangle; // create copy to get anchorpoints

                          Print("startY: " + temp.StartAnchor.Price);
                          Print("startX: " + temp.StartAnchor.Time);

                          Print("endY: " + temp.EndAnchor.Price);
                          Print("endX: " + temp.EndAnchor.Time);
                          }
                          }

                          //************************************************** ****

                          //************************************************** ****
                          // BLOCK 3
                          foreach (dynamic obj in DrawObjects.ToList())
                          {
                          if (obj.ToString().Equals("NinjaTrader.NinjaScript.Dr awingTools.Rectangle"))
                          {
                          // Access the object by assuming that it is the Type we expect
                          Print(String.Format("rect {0} detected!", obj.Tag));
                          Print("startY: " + obj.StartAnchor.Price);
                          Print("startX: " + obj.StartAnchor.Time);

                          Print("endY: " + obj.EndAnchor.Price);
                          Print("endX: " + obj.EndAnchor.Time);
                          }
                          }
                          //************************************************** ****

                          }

                          And the output attached.

                          THANK YOU Jesse

                          Attached Files

                          Comment


                            #14
                            Hello federicoo,

                            I am glad that worked. Just to clarify here, it seems like the item you are using is likely loaded from an assembly which is why using dynamic would be needed here. The type you are trying to access exists in a different assembly so that is why the direct cast was unable to match the object by type.


                            Please let me know if I may be of additional assistance.
                            JesseNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by arvidvanstaey, Today, 02:19 PM
                            4 responses
                            11 views
                            0 likes
                            Last Post arvidvanstaey  
                            Started by samish18, 04-17-2024, 08:57 AM
                            16 responses
                            58 views
                            0 likes
                            Last Post samish18  
                            Started by jordanq2, Today, 03:10 PM
                            2 responses
                            9 views
                            0 likes
                            Last Post jordanq2  
                            Started by traderqz, Today, 12:06 AM
                            10 responses
                            18 views
                            0 likes
                            Last Post traderqz  
                            Started by algospoke, 04-17-2024, 06:40 PM
                            5 responses
                            47 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Working...
                            X