Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need Help Understanding Misbehavior After Removing Drawing Objects

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

    Need Help Understanding Misbehavior After Removing Drawing Objects

    Hello fellow traders,

    I am currently facing an issue with the behavior of my script in Ninjatrader, specifically when I remove drawing objects using the "RemoveDrawObject(_tagName)" command. I am seeking your assistance in understanding and resolving this problem.

    To provide some context, my script draws horizontal lines on the chart, which can be removed when the user clicks on them. My script can detect a mouse click on the horizontal line and remove the line using the 'RemoveDrawObject()' command. However, after successfully removing a horizontal line, an unexpected behavior occurs, albeit intermittently.

    When the problem occurs, I am unable to select any other drawing objects on the chart. It seems as if Ninjatrader becomes locked in a "drag and move" mode with the cursor showing a four-headed arrow pointing in all four directions (up, down, left, and right). However, Ninjatrader remains responsive for selecting a menu item or clicking on any of the chart trader buttons or another chart etc..

    I have discovered this temporary workaround for this issue. By right-clicking on the chart background and selecting the "Properties" menu item, then clicking "OK" without making any changes, or by pressing F6 to add another horizontal line, or even clicking on the 'Close button' on the chart trader panel, the cursor returns to its normal behavior and I can continue using the platform as usual.

    I have verified that this problem is not a script error, as the horizontal line is successfully removed. However, the issue lies in the cursor behavior afterwards. I have thoroughly examined the documentation and searched for similar cases, but I have been unable to find a definitive solution.

    Thank you once again for your willingness to help. Your expertise and support are truly appreciated.

    P.S, I would like to mention that if sharing my indicator code or any relevant information would assist in diagnosing and resolving this intermittent misbehavior, I am more than willing to provide it. Please let me know if you believe that examining my indicator code or any specific details would be beneficial, and I will gladly share them with the community.

    I have designed my indicator with the aim of providing users with a convenient and efficient method to attach any order type to visible plots on the chart. This functionality eliminates the need for users to navigate through the complexity of right-clicking on the chart background and selecting the indicator from a pop-up menu.​

    #2
    Hello trendisyourfriend,

    It sounds like your script is pretty complex, do you see the same problem when just removing the objects using logic and not user mouse events? You may be doing something in your custom logic that is causing the outcome described. In general we would need a specific use case + associated code to help in this case.

    Comment


      #3
      This is the heart of my script remove an Object. Not too complex as you can see.

      Code:
      protected void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
      {
          NinjaTrader.NinjaScript.DrawingTools.HorizontalLine _extendedLineOrder = null;
          _extendedLineOrder = DrawObjects.OfType<NinjaTrader.NinjaScript.DrawingTools.HorizontalLine>()
                              .FirstOrDefault(line => line.IsSelected && !line.IsUserDrawn);
          if (_extendedLineOrder != null)
          {
              listOfDrawingToolsToDispose.Add( ((DrawingTool)_extendedLineOrder).Tag );
          }
      }
      
      protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
      {
          // call the base.OnRender() to ensure standard Plots work as designed
          base.OnRender(chartControl, chartScale);
      
          // clear all useless drawing tools
          disposeOfDrawingTools();
      }
      
      private void disposeOfDrawingTools()
      {
          foreach(string _tagName in listOfDrawingToolsToDispose)
          {
              RemoveDrawObject(_tagName);
          }
          listOfDrawingToolsToDispose.Clear();
      }
      ​
      ​
      Originally posted by NinjaTrader_Jesse View Post
      Hello trendisyourfriend,

      It sounds like your script is pretty complex, do you see the same problem when just removing the objects using logic and not user mouse events? You may be doing something in your custom logic that is causing the outcome described. In general we would need a specific use case + associated code to help in this case.

      Comment


        #4
        Hello trendisyourfriend,

        Do you have a sample that produces the problem which does not involve OnRender removing the objects such as using OnBarUpdate?

        OnRender is run very frequently and can be invoked by mouse events. It may be run at the same time as your mouse event. In general it wouldn't be expected to be trying to remove an object at the same time as you are trying to draw an object as you have posted.



        Comment


          #5
          When i started to code my indicator i was using the removeobject command directly within the mouse event handler i experienced the misbehavior. I decided to put the remove command within the OnRender event handler and still experienced the same misbehavior. Finally, i moved the removal of drawing objects at the start of the OnBarUpdate and i still get this misbehavior.

          Problem is, it occurs intermittently. I can't reproduce it at will but it does occur every time i test after a certain quantity of trials.

          Originally posted by NinjaTrader_Jesse View Post
          Hello trendisyourfriend,

          Do you have a sample that produces the problem which does not involve OnRender removing the objects such as using OnBarUpdate?

          OnRender is run very frequently and can be invoked by mouse events. It may be run at the same time as your mouse event. In general it wouldn't be expected to be trying to remove an object at the same time as you are trying to draw an object as you have posted.


          Comment


            #6
            I'd say put the RemoveDrawObject back in the mouse event handler,
            but use a ChartControl Dispatcher to call it.
            Last edited by bltdavid; 07-02-2023, 11:54 AM. Reason: fix typos

            Comment


              #7
              Subject: Reporting a Potential Bug in NinjaTrader - Issue with Removing Horizontal Lines

              FAO: NinjaTrader Support,

              I am writing again to bring to your attention a potential bug that I have encountered while working with Ninjascript, specifically related to the removal of horizontal lines programmatically.

              To provide some context, I have developed an indicator that utilizes horizontal lines to represent working or accepted orders on the chart. These lines serve as a visual reference for the orders and can be freely dragged and dropped by users without affecting the original orders themselves. The process of drawing these horizontal lines is achieved through the following command:

              _hLine = Draw.HorizontalLine(...);

              The problem arises after having removed such a drawing object using the command:

              RemoveDrawObject("tagName");

              While the command successfully removes the horizontal lines from the chart, NinjaTrader continues to behave as if the line was still present. The cursor icon remains as a crosshair pointing in all four directions instead of returning to its previous state, indicating that an object is still selected. As a result, the user is unable to select any other objects or perform additional actions within NinjaTrader until the issue is resolved. To regain normal functionality, the user must either press the ESC key and click on an empty area on the chart background or manually add a new drawing object.

              Normally, to manually remove a Horizontal Line or any other drawing object, you just need to select the drawing object, the cursor then changes to a crosshair pointing in all four directions and you press the deletec key. After it is done, the cursor returns to its normal shape. But if you do it using the RemoveDrawObject(...), it seems to be different.

              I have made several attempts to address this issue by exploring different solutions, but I have been unsuccessful in finding a suitable resolution. Given the impact it has on user experience and workflow, I believe it is crucial to address this bug and ensure the smooth functioning of NinjaTrader.

              I did a small video to illustrate the issue:
              In this example, the user (me) manually adds a limit order then click on a button (Tie), the script draws a Horizontal Line next to the order and unlock it to let the user drag it around. When the user releases it, the script removes the Horizontal Line as it is no more needed but Ninjatrader continues to 'think' it is still there. As mentionned, to regain normal functionality, the user needs to manually adds a Horizontal Line by pressing the short cut "F6" or he could have press the ESC key and click on the chart background.
              ---

              ---

              I kindly request your assistance in investigating and resolving this matter. Any guidance or updates you can provide regarding this issue would be greatly appreciated.

              Sincerely,
              - TrendIsYourFriend​

              Comment


                #8
                Hello trendisyourfriend,

                We would not be able to investigate anything here because you have not provided a sample of code that demonstrates a problem. What you are doing in the video is fairly complex and if there is some problem with that you would need to reduce what you are doing to a sample script that shows the exact problematic code. Based on that code we could submit a bug report if something which is expected to work in a certain way is working differently than normal or try to suggest ways that you can debug the code otherwise.

                The previous code you added was drawing a line based on a mouse event and removing lines based on the render pass. The render pass can be activated by mouse events so it would not be expected for that specific code to work right. You are essentially adding and removing a line within the same event which in some cases it may leave the line or may remove the line depending on when the collection is updated.

                Comment


                  #9
                  This is no more the case, i don't use the OnRender anymore. This was just a test to see if it would help,, i tried to move the removal into the OnBarUpdate and it did not help either so i went back to remove the Horizontal Line in the mouseUp handler this way:

                  Code:
                  protected void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
                  {
                      NinjaTrader.NinjaScript.DrawingTools.HorizontalLine _extendedLineOrder = null;
                  
                      if (isTieInProgress)
                      {
                          // ...
                      }
                      else if (isUntieInProgress)
                      {
                          // get the selected Horizontal line object which was clicked
                          _extendedLineOrder = DrawObjects.OfType<NinjaTrader.NinjaScript.DrawingTools.HorizontalLine>()
                                                  .FirstOrDefault(line => line.IsSelected && !line.IsUserDrawn);
                          if (_extendedLineOrder != null)
                          {
                              // the Horizontal Line is no more needed
                              RemoveDrawObject( ((DrawingTool)_extendedLineOrder).Tag );
                          }
                      }
                  }
                  ​​
                  Would that help if i provide the indicator ?

                  Originally posted by NinjaTrader_Jesse View Post
                  Hello trendisyourfriend,

                  We would not be able to investigate anything here because you have not provided a sample of code that demonstrates a problem. What you are doing in the video is fairly complex and if there is some problem with that you would need to reduce what you are doing to a sample script that shows the exact problematic code. Based on that code we could submit a bug report if something which is expected to work in a certain way is working differently than normal or try to suggest ways that you can debug the code otherwise.

                  The previous code you added was drawing a line based on a mouse event and removing lines based on the render pass. The render pass can be activated by mouse events so it would not be expected for that specific code to work right. You are essentially adding and removing a line within the same event which in some cases it may leave the line or may remove the line depending on when the collection is updated.

                  Comment


                    #10
                    Hello trendisyourfriend,

                    We can't debug your full indicator for you so you would need to make a small sample of code that demonstrates a specific problem when used in a certain way. With that type of sample we can either know if its something you are logically doing that is not correct or we can make a bug report in case something that is expected to work in a certain way is not working in that way.

                    If you are not using OnRender then we would not want any samples that involve OnRender, we would instead want a sample of the exact code that causes the problem so we can investigate that.

                    Comment


                      #11
                      I think i found out something. It appears, we can't mix both commands:

                      RemoveDraw("tagName");
                      and RemoveDrawObjects();

                      For example, if i call RemoveDraw("tagName") as i did;
                      and a bit further in code i call RemoveDrawObjects();

                      It causes the problem i have encountered. After removing all RemoveDrawObjects() of my code. Everything, is fine.

                      Originally posted by NinjaTrader_Jesse View Post
                      Hello trendisyourfriend,

                      We can't debug your full indicator for you so you would need to make a small sample of code that demonstrates a specific problem when used in a certain way. With that type of sample we can either know if its something you are logically doing that is not correct or we can make a bug report in case something that is expected to work in a certain way is not working in that way.

                      If you are not using OnRender then we would not want any samples that involve OnRender, we would instead want a sample of the exact code that causes the problem so we can investigate that.

                      Comment


                        #12
                        Hello trendisyourfriend,

                        Yes that wouldn't be expected to try and double remove an object. RemoveDrawObjects should only be used when all objects need cleared. RemoveDrawObject("") should only be used when you want to remove specific objects.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        576 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        334 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        101 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        553 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        551 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X