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

Draw Object counter DrawObjects.Count

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

    Draw Object counter DrawObjects.Count

    There is very little information on DrawObjects.Count in the Help.

    1. How fast it gets updated on object creation or removal?
    2. What can cause it lagging in time or be just way off?
    3. Why it can have a negative a value (when some of the draw objects on a chart a global)?

    #2
    Helo LeonK,

    Thank you for your post.

    1. How fast it gets updated on object creation or removal?
    The DrawObjects collection itself is updated live/in real-time and for that reason it is not thread-safe to access the collection directly as it could be modified when you try to read it. There is a note about this in the help guide:
    "DrawObjects.ToList() is thread safe. DrawObjects collection itself is still dynamic (meaning it updates live) and as a result you can still run the risk of the collection being modified while you try to read it (and thus would see the related C# log entry) However, DrawObjects.ToList() is a snapshot of DrawObjects collection at the time the call is made."


    2. What can cause it lagging in time or be just way off?
    In what context are you seeing lagging or unexpected values for DrawObjects.Count? Is this happening in a script of your own, and are you accessing a thread-safe snapshot of DrawObjects such as DrawObjects.ToList()? Please provide a snippet that results in lagging or unexpected values so we may better understand and assist you with this question.

    3. Why it can have a negative a value (when some of the draw objects on a chart a global)?​
    When are you getting a negative value? Please provide a snippet or a reduced sample script that results in a negative value. Where are you seeing the negative value? Do you get a negative value when using Print() statements?


    I look forward to your reply.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Emily View Post
      Helo LeonK,

      Thank you for your post.

      1. How fast it gets updated on object creation or removal?
      The DrawObjects collection itself is updated live/in real-time and for that reason it is not thread-safe to access the collection directly as it could be modified when you try to read it. There is a note about this in the help guide:
      "DrawObjects.ToList() is thread safe. DrawObjects collection itself is still dynamic (meaning it updates live) and as a result you can still run the risk of the collection being modified while you try to read it (and thus would see the related C# log entry) However, DrawObjects.ToList() is a snapshot of DrawObjects collection at the time the call is made."
      https://ninjatrader.com/support/help...rawobjects.htm

      2. What can cause it lagging in time or be just way off?
      In what context are you seeing lagging or unexpected values for DrawObjects.Count? Is this happening in a script of your own, and are you accessing a thread-safe snapshot of DrawObjects such as DrawObjects.ToList()? Please provide a snippet that results in lagging or unexpected values so we may better understand and assist you with this question.

      3. Why it can have a negative a value (when some of the draw objects on a chart a global)?​
      When are you getting a negative value? Please provide a snippet or a reduced sample script that results in a negative value. Where are you seeing the negative value? Do you get a negative value when using Print() statements?
      https://ninjatrader.com/support/foru...ing#post791121

      I look forward to your reply.
      Hi Emily,

      1. Let me explain why I was asking how fast the counter gets updated on object removal and creation.
      The RemoveDrawObject() function does not return any value, and I wanted make sure the object has been removed, so I compare the value of the DrawObjects.Count before and after the function call. It seemed to reflect the removal right away, so I thought it is a reliable way of making sure the object has been successfully removed. Now to lagging.

      2. Since the counter was decremented immediately after the object has been removed I thought it would be incremented immediately when a new drawing has been created. Below code proved me wrong.

      Log(LNreDrawZone + "we have " + DrawObjects.Count + " drawings. Remove rectangle " + Z.Z.Tag + " for zone " + Z.szTag, LogLevel.Information); // Log 1
      RemoveDrawObject('@' == Z.Z.Tag[0] ? Z.Z.Tag.Substring(1) : Z.Z.Tag);

      Log(LNreDrawZone + "we have " + DrawObjects.Count + " drawings left after removal", LogLevel.Information); // Log 2
      ForceRefresh();

      Rectangle newRect = Draw.Rectangle(this, szNewTag , ST, SY, ET, EY , bMakeGlobal, "");
      ForceRefresh();

      Log(LNreDrawZone + "we have " + DrawObjects.Count + " including the new one", LogLevel.Information); // Log 3

      Say Log1 shows 100. Then Log 2 would show 99, an this has been very reliable in my tests. however, Log 3 could show either 99 or 100, intermittently, and 99 was much more often than 100.

      3. The counter get totally screwed and can even show negative numbers when some of the drawing objects are global. Please, see attached code example.

      TestDrawingCounter.cs

      Thanks
      Attached Files

      Comment


        #4
        Hello LeonK,

        Thank you for your reply.

        Before I test on my end, I would like a couple more details from your end.
        • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
        • What instrument symbol (and expiry if applicable) have you selected? For example, ES 03-22, AAPL, EURUSD, etc.
        • (For a chart) what interval is selected? For example, 5 minute, 1 day, 2000 volume, 4 Renko, etc.
        • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
        • Is there a particular reason you are using Log() instead of Print()? Log() can be very resource-intensive and should be reserved for more important messages and not for debugging purposes. As noted in the help guide:
        • What are the results if you use Print() instead of Log()? Please test on your end and include the output saved as a text file by right-clicking the NinjaScript Output window and selecting Save as. You may then attach the text file to your reply here.
        I look forward to your response.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hello, Emily,

          please see my replies below.

          Thank you

          > What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
          Version 8.0.28.0 on one machine and 8.1.2.1 on another, same behavior.

          > What instrument symbol (and expiry if applicable) have you selected? For example, ES 03-22, AAPL, EURUSD, etc.
          ES 03-24 or NQ 03-24

          >
          (For a chart) what interval is selected? For example, 5 minute, 1 day, 2000 volume, 4 Renko, etc.
          I tested on different intervals. Make one tab 1 mon and the other 5 min (you need two tabs for the test).

          > Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
          No, no errors.


          > Is there a particular reason you are using Log() instead of Print()?
          Our production code no Log() or Print(). I made this little tiny demo for you to see the issue. Feel free to comment out the Log().

          > What are the results if you use Print() instead of Log()?​​
          For the demo I sent to you the "results" are on the screen, you do not need any log or print at all.

          I am attaching a few screenshots. One chart, two tabs. MNQ 60 min and 5 min.
          The counter is displayed in the upper right corner in cyan. It is correct on both tabs, See attached 1,2.
          Change enGlobal parameter on one of the tabs. See attached 3.
          This makes the rectangles global and screw up the counter ON BOTH tabs. See attached 4 and 5.
          Hit F5 on one of the tabs. The numbers will change, though still will be wrong. Click image for larger version

Name:	DC2.png
Views:	42
Size:	128.6 KB
ID:	1284359Click image for larger version

Name:	DC1.png
Views:	42
Size:	147.6 KB
ID:	1284360Click image for larger version

Name:	DC4.png
Views:	44
Size:	209.3 KB
ID:	1284364Click image for larger version

Name:	DC5.png
Views:	43
Size:	189.9 KB
ID:	1284365Click image for larger version

Name:	DC3.png
Views:	44
Size:	46.3 KB
ID:	1284363
          Attached Files
          Last edited by LeonK; 01-01-2024, 06:27 PM. Reason: Added screenshots

          Comment


            #6
            Hello LeonK,

            Thank you for your patience.

            I believe that the following note needs to be taken into account here:
            "When reloading NinjaScript, all objects (including manual drawing tools) are reloaded at the same time. There is no guarantee a manually drawn object will be added to the DrawObjects collection before an indicator starts processing data."


            When an indicator is first loaded, such as when you first apply it or when you change the enGlobal bool, it reloads NinjaScript. I am seeing that the first two passes of OnBarUpdate() in State.Realtime show the magenta counters, but after that the counters seem to be in sync and it displays the counter in cyan. I added the following print and I see that the value of DrawObjects.Count is negative during State.Historical in your script, then it is a positive number once it calculates in State.Realtime:
            Code:
                        iDrawingCounter = DrawObjects.Count;
                        Print("State: " + State + " DrawObjects.Count: " + DrawObjects.Count + " iDrawingCounter: " + iDrawingCounter + " RGRects.Count: " + RGRects.Count);
                        if (iDrawingCounter != RGRects.Count + 1)
                            Draw.TextFixed(this, "DrawingCounter", RGRects.Count.ToString() + " " + iDrawingCounter.ToString(), TextPosition.TopRight, Brushes.Magenta, myFont, Brushes.Transparent, Brushes.Transparent, 0);
                        else
                            Draw.TextFixed(this, "DrawingCounter", RGRects.Count.ToString(), TextPosition.TopRight, Brushes.Cyan, myFont, Brushes.Transparent, Brushes.Transparent, 0);​
            It also seems that since you are drawing rectangles via the RGRect() class, they are not being counted in the DrawObjects collection historically:
            Code:
                            RNew.R = Draw.Rectangle(this, szTag, Time[0], dblStart, Time[0].Add(interval), dblEnd, bGlobal, "");
            ​
            If the rectangles are drawn this way instead, I am no longer getting a negative number for DrawObjects.Count:
            Code:
                        if (bDraw)
                        {
                            Rectangle RNewR = Draw.Rectangle(this, szTag, Time[0], dblStart, Time[0].Add(interval), dblEnd, bGlobal, "");
            //                Print(LNOnBarUpdate + "Created " + RNew.R.Tag + " " + RNew.R.StartAnchor.Price + " - " + RNew.R.EndAnchor.Price);
                            RNewR.AreaBrush = Close[0] < Open[0] ? Brushes.Red : Brushes.Green;
                            RGRects.Add(RNew);
                        }​
            I suspect that the underlying issue with the DrawObjects.Count discrepancy has to do with the typing of RGRect Rectangles vs. DrawingTools.Rectangle objects and how those are counted in the DrawObjects collection.

            Please let us know if we may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by fx.practic, 10-15-2013, 12:53 AM
            5 responses
            5,403 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by Shai Samuel, 07-02-2022, 02:46 PM
            4 responses
            94 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by DJ888, Yesterday, 10:57 PM
            0 responses
            6 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by MacDad, 02-25-2024, 11:48 PM
            7 responses
            158 views
            0 likes
            Last Post loganjarosz123  
            Started by Belfortbucks, Yesterday, 09:29 PM
            0 responses
            8 views
            0 likes
            Last Post Belfortbucks  
            Working...
            X