Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Looping through DrawObjects odd behaviour

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

    Looping through DrawObjects odd behaviour

    I am looping through all the DrawObjects on my chart and I am noticing that testing whether a DrawObject is a particular type doesn't always work. These are all user drawn Rays and they were all drawn at the same time using the defaults.
    Code:
    if (draw is DrawingTools.Ray || draw is DrawingTools.Line) {
    					ChartAnchor c = ((NinjaTrader.NinjaScript.DrawingTools.ChartAnchor[])(draw.Anchors))[0];
    					ChartAnchor c2 = ((NinjaTrader.NinjaScript.DrawingTools.ChartAnchor[])(draw.Anchors))[1];
    					
    					//runCalc2(c.Time, c.Price);
    					Stats s = new Stats(c.Price,c2.Time);
    					s.runCalc2(TickSize,BarsArray[0],false);
    					
    				}
    World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


    The object type looks like it is a Ray, but that Ray never enters the if statement block

    #2
    Hello habibalex,

    I would recommend comparing the IDrawObject to IRay.

    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      would this be the same for NT8?

      Comment


        #4
        Hello habibalex,

        The classnames are different for NinjaTrader 8.



        This thread is posted in the NinjaTrader 7 Indicator Development section, was your original question for NinjaTrader 8?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          yes sorry it was supposed to be in the NT8 forum

          Comment


            #6
            Hello habibalex,

            I've remade this for NinjaTrader 8.

            Using this test script are you able to reproduce this behavior?
            (The test script will print the tagname and start time of all rays already on the chart)
            Attached Files
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Yes I can reproduce. I made the following change to your code

              I made the rays in 1 chart using the CME US INDEX FUTURES ETH template. Rays are drawn on all charts globally.

              Then I opened a different chart and added your indicator.



              Code:
              foreach (DrawingTool draw in DrawObjects)
              			{
              				// Finds line objects that are attached globally to all charts of the same instrument
              				if (draw is DrawingTools.Ray)
              				{
              					Ray ray = (Ray)draw;
              
              					Print(string.Format("{0} | {1}", ray.Tag, ray.StartAnchor.Time));
              				}else if(draw.GetType().Name == "Ray"){
              					Print(draw.Tag + " draw is NOT a DrawingTools.Ray");
              				}
              			}
              HTML Code:
              @Ray 19 draw is NOT a DrawingTools.Ray
              @Ray 23 draw is NOT a DrawingTools.Ray
              @Ray 27 draw is NOT a DrawingTools.Ray
              @Ray 18 draw is NOT a DrawingTools.Ray
              @Ray 22 draw is NOT a DrawingTools.Ray
              @Ray 26 draw is NOT a DrawingTools.Ray

              Comment


                #8
                Hello habibalex,

                Each time I add the indicator the first time (without modifying the script) I am unable to reproduce this behavior.

                But I am able to reproduce if I make a change to the script and compile and then remove and re-add the script.

                Also, I am seeing some different behavior when reloading ninjascript (F5).

                I've made a video and reported the behavior to our development.
                World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


                Once I have a tracking ID for the behavior I will post in this thread.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello habibalex,

                  I've received a tracking ID for this behavior.

                  This issue is being tracked with ID #NTEIGHT-11857.

                  There are two issues. Here's the skinny.

                  First, when the chart is reloaded the DrawObjects collection does no longer hold the old instances of objects, but neither does it hold the new instances yet.

                  The work around is to detect the objects after they are added to the collect in real-time.

                  Below is a link to an example.



                  Second, as the script is compiled a temporary assembly is created (so as to not interrupt the scripts currently running) including the objects that are on the charts. Because of this, the cast does not work until nt is restarted.

                  The workaround is to use the dynamic type and compare the ToString() to the full namespace.
                  Code:
                  foreach (dynamic ray in DrawObjects)
                  {
                  	// Use ToString().Equals() to detect the object's Type 
                  	if (ray.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Ray"))
                  	{
                  		// Access the object by assuming that it is the Type we expect
                  		Print(String.Format("Ray {0} detected!", ray.Tag));
                  	}
                  }
                  This is shown in our help guide.
                  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
                  646 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  367 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  107 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  569 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  573 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X