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

Looping through DrawObjects

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

    Looping through DrawObjects

    I want to loop through all manually drawn objects on a chart and to read some of their parameters when I click on a menu button. I cannot get the typecasting of the draw objects to work properly. I have this code to check which kind of drawing an object is:

    Code:
            private void PrintObjects()
            {
                Print("First Loop ------------------------------");
                foreach (DrawingTool draw in DrawObjects)
                {
                    Print("Name: " + draw.Name + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                }
                Print("Second Loop ------------------------------");
                foreach (DrawingTool draw in DrawObjects)
                {
                    if (draw is DrawingTools.Line)
                        Print("Line Object          : " + "     GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                    if (draw is DrawingTools.Ellipse)
                        Print("Ellipse Object       : " + "     GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                    if (draw is DrawingTools.Rectangle)
                        Print("Rectangle Object     : " + "     GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                    if (draw is DrawingTools.Ray)
                        Print("Ray Object           : " + "     GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                    if (draw is DrawingTools.ArrowLine)
                        Print("ArrowLine Object     : " + "     GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                    if (draw is DrawingTools.HorizontalLine)
                        Print("HorizontalLine Object: " + "     GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                    if (draw is DrawingTools.VerticalLine)
                        Print("VerticalLine Object  : " + "     GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                }  
                Print("End ==============================");
            }
    Output:
    First Loop ------------------------------
    Name: Line GetType().Name: Line draw.Tag: Line 1
    Name: Ray GetType().Name: Ray draw.Tag: Ray 2
    Name: Arrow line GetType().Name: ArrowLine draw.Tag: Arrow line 3
    Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 4
    Name: Ellipse GetType().Name: Ellipse draw.Tag: Ellipse 5
    Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: Horizontal Line 6
    Name: Vertical Line GetType().Name: VerticalLine draw.Tag: Vertical Line 7
    Second Loop ------------------------------
    Line Object : GetType().Name: Line draw.Tag: Line 1
    Line Object : GetType().Name: Ray draw.Tag: Ray 2
    Ray Object : GetType().Name: Ray draw.Tag: Ray 2
    Line Object : GetType().Name: ArrowLine draw.Tag: Arrow line 3
    ArrowLine Object : GetType().Name: ArrowLine draw.Tag: Arrow line 3
    Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 4
    Ellipse Object : GetType().Name: Ellipse draw.Tag: Ellipse 5
    Line Object : GetType().Name: HorizontalLine draw.Tag: Horizontal Line 6
    HorizontalLine Object: GetType().Name: HorizontalLine draw.Tag: Horizontal Line 6
    Line Object : GetType().Name: VerticalLine draw.Tag: Vertical Line 7
    VerticalLine Object : GetType().Name: VerticalLine draw.Tag: Vertical Line 7
    End ==============================
    First Loop ------------------------------
    Name: Line GetType().Name: Line draw.Tag: Line 1
    Name: Ray GetType().Name: Ray draw.Tag: Ray 2
    Name: Arrow line GetType().Name: ArrowLine draw.Tag: Arrow line 3
    Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 4
    Name: Ellipse GetType().Name: Ellipse draw.Tag: Ellipse 5
    Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: Horizontal Line 6
    Name: Vertical Line GetType().Name: VerticalLine draw.Tag: Vertical Line 7
    Second Loop ------------------------------
    Line Object : GetType().Name: ArrowLine draw.Tag: Arrow line 3
    ArrowLine Object : GetType().Name: ArrowLine draw.Tag: Arrow line 3
    Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 4
    Line Object : GetType().Name: HorizontalLine draw.Tag: Horizontal Line 6
    HorizontalLine Object: GetType().Name: HorizontalLine draw.Tag: Horizontal Line 6
    Line Object : GetType().Name: VerticalLine draw.Tag: Vertical Line 7
    VerticalLine Object : GetType().Name: VerticalLine draw.Tag: Vertical Line 7
    End ==============================
    In the output, the first loop lists all drawings on the chart correctly. The second loop shows the wrong object type for some drawings (e.g. Ray as Line object), and sometimes drawings are not printed. Each time I reload the indicator (attached), I get a different output for the second loop.

    What is wrong in the code?
    Attached Files

    #2
    Hello tombri, and thank you for your question. Your code is correct, what is happening is that some drawing objects inherit from other drawing objects. For example, a Ray is a kind of Line. This is why you see Ray printed out multiple times; a Ray is a Ray, and a Ray is a Line. Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thank you Jessica. I now understand why some drawing objects are printed out multiple times. I have changed the code to prevent this.

      My second question was, why aren't all drawing objects printed out? And why does the print out change when I reload the indicator without modifying the chart?

      Here is another example with the new code and a chart with 10 rectangles. First time only five of them are printed. After I press F5 to reload the indicator, it prints 8 of the 10. After I reload the indicator again, 9 of the 10 are printed.

      What is the problem here?

      Code:
              private void PrintObjects()
              {
                  Print("First Loop ------------------------------");
                  foreach (DrawingTool draw in DrawObjects)
                  {
                      Print("Name: " + draw.Name + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                  }
                  Print("Second Loop ------------------------------");
                  foreach (DrawingTool draw in DrawObjects)
                  {
                      if (draw is DrawingTools.Ray)
                          Print("Ray Object                  : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.ArrowLine)
                          Print("ArrowLine Object            : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.HorizontalLine)
                          Print("HorizontalLine Object       : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.VerticalLine)
                          Print("VerticalLine Object         : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.Line)
                          Print("Line Object                 : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.FibonacciExtensions)
                          Print("FibonacciExtensions Object  : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.FibonacciRetracements)
                          Print("FibonacciRetracements Object: " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);                
                      else if (draw is DrawingTools.Ellipse)
                          Print("Ellipse Object              : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.Rectangle)
                          Print("Rectangle Object            : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.TrendChannel)
                          Print("TrendChannel Object         : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else if (draw is DrawingTools.Triangle)
                          Print("Triangle Object             : " + "    GetType().Name: " + draw.GetType().Name + "    draw.Tag: " + draw.Tag);
                      else
                          Print(">> Drawing object " + draw.Tag + " not found");
                  }  
                  Print("End ==============================");        
              }
      First Loop ------------------------------
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 65
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 66
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 67
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 68
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 69
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 70
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 71
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 72
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 73
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 74
      Second Loop ------------------------------
      >> Drawing object Rectangle 65 not found
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 66
      >> Drawing object Rectangle 67 not found
      >> Drawing object Rectangle 68 not found
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 69
      >> Drawing object Rectangle 70 not found
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 71
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 72
      >> Drawing object Rectangle 73 not found
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 74
      End ==============================
      First Loop ------------------------------
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 65
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 66
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 67
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 68
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 69
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 70
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 71
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 72
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 73
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 74
      Second Loop ------------------------------
      >> Drawing object Rectangle 65 not found
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 66
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 67
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 68
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 69
      >> Drawing object Rectangle 70 not found
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 71
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 72
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 73
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 74
      End ==============================
      First Loop ------------------------------
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 65
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 66
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 67
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 68
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 69
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 70
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 71
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 72
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 73
      Name: Rectangle GetType().Name: Rectangle draw.Tag: Rectangle 74
      Second Loop ------------------------------
      >> Drawing object Rectangle 65 not found
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 66
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 67
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 68
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 69
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 70
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 71
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 72
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 73
      Rectangle Object : GetType().Name: Rectangle draw.Tag: Rectangle 74
      End ==============================

      Comment


        #4
        I believe your test code is valid. I believe your code under test may need review. I have included a copy of your code with some code under test I have written, and I received the results in the attached results.txt with a single refresh.

        Please examine the code sample I have provided and compare to your code. We are happy to answer any questions we may.
        Attached Files
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          This problem occurs when looping through manually drawn objects.

          Comment


            #6
            Thank you for this clarification. I made a video where I manually drew the same objects, and I received the attached results.



            Since I received results I expected, I would like to invite you to review the differences both between my test and your test, and between my code and your code. We are happy to help if any questions come up.
            Attached Files
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Jessica,

              I found this post and it relates to unexpected results I get with trying to find drawing objects. I inserted your code to my strategy and the print section is below. Note that in the first loop it finds objects yet in the second loop those objects are not found.


              Any thoughts?


              First Loop ------------------------------
              Name: Risk Reward GetType().Name: RiskReward draw.Tag: myRR
              Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: Currprice
              Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 200
              Name: Ray GetType().Name: Ray draw.Tag: @Ray 201
              Name: Ray GetType().Name: Ray draw.Tag: @Ray 210
              Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 200
              Name: Ray GetType().Name: Ray draw.Tag: @Ray 201
              Name: Ray GetType().Name: Ray draw.Tag: @Ray 210
              Name: Ray GetType().Name: Ray draw.Tag: @Ray 201
              Second Loop ------------------------------
              >> Drawing object myRR not found
              HorizontalLine Object : GetType().Name: HorizontalLine draw.Tag: Currprice
              HorizontalLine Object : GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 200
              Ray Object : GetType().Name: Ray draw.Tag: @Ray 201
              Ray Object : GetType().Name: Ray draw.Tag: @Ray 210
              >> Drawing object @Horizontal Line 200 not found
              >> Drawing object @Ray 201 not found
              >> Drawing object @Ray 210 not found
              Ray Object : GetType().Name: Ray draw.Tag: @Ray 201

              -------------------------------------
              Side Question: your code includes this if statement in OnBarUpdate. My understanding of the code, this should never be true since it can't be the first bar of the session and more than 5 bars into the chart at the same time???

              if (Bars.IsFirstBarOfSession && CurrentBar > 5)
              Last edited by Richls; 01-06-2017, 10:46 AM.

              Comment


                #8
                Hello Rich, and thank you for your question. I noticed in your output that all the not-found objects were found twice during the first loop. Is your strategy a multi-timeframe strategy? This is just a diagnostics question, either a yes or a no will be helpful. I would also like to ask that you change

                Code:
                [FONT=Courier New]
                                else
                                    Print(">> Drawing object " + draw.Tag + " not found");[/FONT]
                to

                Code:
                [FONT=Courier New]
                                else
                                    Print( ">> Drawing object " + draw.Tag + " not found, attempting to get type: " + (null == draw.GetType() ? "null" : draw.GetType().Name) );[/FONT]
                on lines 83 and 84 (line numbers relative to my script, toward the end of PrintObjects() otherwise). Could you then attach this output?

                Regarding your second question, this event occurs reliably as soon as State.Realtime is reached, and occasionally during State.Historical if there are multiple historic sessions. It is designed to filter out historical session starts that have fewer than 5 bars available, since I look back 5 bars at several points. There is a "session start" during State.Transition and after State.Historical, so this happens at least once always. You could replace the IsFirstBarOfSession call by a simple check for State.Realtime to achieve the same effect.

                I am looking forward to assisting further.
                Last edited by NinjaTrader_JessicaP; 01-06-2017, 10:58 AM.
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Yes, this is a multiple timeframe strategy. However, I am only processing one timeframe in the body of the strategy. BarsArray[1]

                  the new print looks like this:

                  First Loop ------------------------------
                  Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: Currprice
                  Name: Ray GetType().Name: Ray draw.Tag: @Ray 215
                  Name: Ray GetType().Name: Ray draw.Tag: @Ray 216
                  Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 217
                  Name: Ray GetType().Name: Ray draw.Tag: @Ray 215
                  Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 217
                  Name: Ray GetType().Name: Ray draw.Tag: @Ray 216
                  Name: Horizontal Line GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 217
                  Second Loop ------------------------------
                  HorizontalLine Object : GetType().Name: HorizontalLine draw.Tag: Currprice
                  >> Drawing object @Ray 215 not found, attempting to get type: Ray
                  Ray Object : GetType().Name: Ray draw.Tag: @Ray 216
                  HorizontalLine Object : GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 217
                  >> Drawing object @Ray 215 not found, attempting to get type: Ray
                  HorizontalLine Object : GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 217
                  Ray Object : GetType().Name: Ray draw.Tag: @Ray 216
                  HorizontalLine Object : GetType().Name: HorizontalLine draw.Tag: @Horizontal Line 217
                  Last edited by Richls; 01-06-2017, 11:53 AM.

                  Comment


                    #10
                    Thank you for this additional information.A ray should have been detected, if not as a ray, as at least another of the types on the list. I would like to ensure you are using NinjaTrader 8.0.2.0 or higher? If so please send a stripped-down (without any trading logic) copy of your strategy to platformsupport[at]ninjatrader[dot]com referencing attn:ninjatrader_jessicap and 1621772 in the subject line, so we may use your script to investigate. Please also let us know if this behavior persists into the next release.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you Richls, I just wanted to confirm receipt of your e-mail. I am testing Ninja against the code you sent and will respond when I have more information.
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        Jessica,
                        Some additional info. I Noticed that horizontal lines are duplicating themselves hiding one beneath the other. Not sure if it happens when I reload the chart containing the strategy. The tag also is duplicated.

                        Comment


                          #13
                          Thank you Rich for your setting aside time and resources helping us track down what was occurring. This behavior was confirmed in 8.0.2.0 and no longer exists in the latest version of NinjaTrader. 8.0.3.0, released today.
                          Jessica P.NinjaTrader Customer Service

                          Comment


                            #14
                            As an update, it was discovered that while fixes made in 8.0.3.0 fix many instances, it is still possible for GetType to succeed where is does not.

                            I would like to recommend using GetType as shown in the above script to check drawing object types, and not the is keyword. I will provide a detailed explanation as to why.

                            For reference the difference between these reflection types :

                            • if a is b, a inherits from or is the same class as b. This is checked at runtime
                            • if a.typeof() == b.typeof(), a and b erase to the exact same type. This is checked at compile time
                            • if a.GetType() == b.GetType(), a and b have the exact same type. This is checked at run time, so erasure is no longer an isssue

                            With the "is" keyword, then, the .NET framework must be able to check the entire inheritance tree for a symbol. It appears that messages relaying this information are not being passed between threads. However, it appears that the base type name is retrievable at runtime reliably. The type returned by GetType is going to be the most specific type available.
                            Jessica P.NinjaTrader Customer Service

                            Comment


                              #15
                              Jessica,
                              This approach will identify real lines but what do i do when I see a line on the chart but my strategy does not see it.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              4 responses
                              12 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by terofs, Today, 04:18 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by nandhumca, Today, 03:41 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post nandhumca  
                              Started by The_Sec, Today, 03:37 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post The_Sec
                              by The_Sec
                               
                              Started by GwFutures1988, Today, 02:48 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Working...
                              X