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

Removing Objets Code

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

    Removing Objets Code

    Hello. I'm making a code for a button that erases drawn objects. The button is properly created and looks functional.

    private void ExampleButtonClick(object sender, RoutedEventArgs e)
    {

    foreach (DrawingTool drawObject in DrawObjects)
    {
    // if (drawObject.Tag.Contains("SuperRiskReward"))
    if (drawObject.Tag.Contains("Line"))
    {
    RemoveDrawObject(drawObject.Tag);
    }
    }


    }​

    the point is that if I put "Line" in the tag for example, it works correctly by removing all line objects

    However, with the RiskReward Modified, the objects are not erased

    I'm attaching the screenshot of the properties of the object I'm drawing and I want to delete. Can you help me? Thank you!

    Attached Files

    #2
    Hello manueldecastro,

    Thank you for your post.

    Make sure you are passing in the correct tag name to into RemoveDrawObject() for the specific drawing object you're trying to remove.

    I recommend looping through the DrawObjects collection, then draw as DrawingTools.<drawing tool name> and printing out the tag name of the object to see how it is evaluating. Similar to the sample code at the bottom of this page:



    Additionally, please note that when using RemoveDrawObject, it cannot remove drawing objects drawn by the user. From the Help Guide:

    "This method will ONLY remove DrawObjects which were created by a NinjaScript object. User drawn objects CANNOT be removed from via NinjaScript."



    When looping through the DrawObjects collection you can also check draw.IsUserDrawn.

    Please let us know if you have any further questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post
      Hello manueldecastro,

      Thank you for your post.

      Make sure you are passing in the correct tag name to into RemoveDrawObject() for the specific drawing object you're trying to remove.

      I recommend looping through the DrawObjects collection, then draw as DrawingTools.<drawing tool name> and printing out the tag name of the object to see how it is evaluating. Similar to the sample code at the bottom of this page:



      Additionally, please note that when using RemoveDrawObject, it cannot remove drawing objects drawn by the user. From the Help Guide:

      "This method will ONLY remove DrawObjects which were created by a NinjaScript object. User drawn objects CANNOT be removed from via NinjaScript."



      When looping through the DrawObjects collection you can also check draw.IsUserDrawn.

      Please let us know if you have any further questions.

      As you can see in the print. When entering the foreach it seems to detect the tag, but does not delete the object. Could you give me some solution

      Thanks!
      Attached Files

      Comment


        #4
        Hello,

        Is this object drawn by a NinjaScript, or it is user drawn? If it is user drawn, and not drawn by another NinjaScript like an indicator or a strategy, then you would not be able to remove it via RemoveDrawObject().

        I look forward to your response.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Gaby View Post
          Hello,

          Is this object drawn by a NinjaScript, or it is user drawn? If it is user drawn, and not drawn by another NinjaScript like an indicator or a strategy, then you would not be able to remove it via RemoveDrawObject().

          I look forward to your response.
          The objects I want to delete are drawn by the user, not by any indicator or strategy.

          I think the problem is something to do with tags. I started with the Risk Reward tool by creating a copy and modifying what was necessary until it was compiled. The difference I notice is that the properties of the object drawn with the copy of the Risk are different from those generated by the native ninja objects. Where could the mistake be?
          Attached Files
          Last edited by manueldecastro; 05-03-2024, 03:58 AM.

          Comment


            #6
            Hello manueldecastro,
            The objects I want to delete are drawn by the user, not by any indicator or strategy.
            The problem is that you cannot delete user drawn object with RemoveDrawObject(). From the Help Guide:

            "Note: This method will ONLY remove DrawObjects which were created by a NinjaScript object. User drawn objects CANNOT be removed from via NinjaScript"

            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              I'm not sure I understand your message correctly. It tells me that it is not possible to delete objects drawn by the user. However, with the code implemented in the button I'm trying to program, for a "Line" object it does correctly by removing all user-drawn "Line" objects on the chart.

              The Code:
              private void ExampleButtonClick(object sender, RoutedEventArgs e)
              {


              foreach (DrawingTool drawObject in DrawObjects)
              {
              if (drawObject.Tag.Contains("Line"))
              {
              RemoveDrawObject(drawObject.Tag);
              }
              }


              }​

              Comment


                #8
                Hello,

                Are you able to confirm this by looping through the drawing objects collection and printing out if this a manually drawn line or not?

                Code:
                protected override void OnBarUpdate()
                {
                // Loops through the DrawObjects collection via a threadsafe list copy
                foreach (DrawingTool draw in DrawObjects.ToList())
                {
                // Finds non-global line objects
                if (draw is DrawingTools.Line)
                {
                // Indicates if this is a manually drawn or script generated line
                Print("Line Object: " + draw.Tag + " Manually Drawn: " + draw.IsUserDrawn);
                }
                }
                ​
                Please provide the output by right-clicking within the NinjaScript output window > Save As.

                I look forward to assisting further.
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  This is what the output with the code in the capture tells me.
                  I've drawn 2 Line 2 ninja native RiskReward and 2 modified RiskReward ones that I can't clear by code. They all seem to indicate their Tag correctly, but the button still doesn't clear my custom RiskReward.

                  Line and Risk Reward are successfully cleared when using the button with the code.

                  private void ExampleButtonClick(object sender, RoutedEventArgs e)
                  {


                  foreach (DrawingTool drawObject in DrawObjects)
                  {
                  if (drawObject.Tag.Contains(("Line")))
                  {
                  RemoveDrawObject(drawObject.Tag);
                  }
                  }

                  foreach (DrawingTool drawObject in DrawObjects)
                  {
                  if (drawObject.Tag.Contains(("Risk")))
                  {
                  RemoveDrawObject(drawObject.Tag);
                  }
                  }

                  foreach (DrawingTool drawObject in DrawObjects)
                  {
                  if (drawObject.Tag.Contains(("RR")))
                  {
                  RemoveDrawObject(drawObject.Tag);
                  }
                  }​
                  }

                  Attached Files

                  Comment


                    #10
                    Hello,

                    Thank you for your response.

                    I wouldn't expect any manually drawn objects at all to be removed. I'm not able to replicate that behavior in my own platform.

                    Can you provide a sample script that demonstrates this behavior to test?

                    If you aren't comfortable sharing your code here, you can send it to us privately at scriptingsupport[AT]ninjatrader[DOT]com. In the subject include: "ATTN Gaby 04393678".
                    Gaby V.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by sdauteuil, 09-23-2021, 10:16 AM
                    7 responses
                    1,247 views
                    0 likes
                    Last Post marcus2300  
                    Started by sofortune, 05-18-2024, 11:48 AM
                    2 responses
                    32 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by Zach55, 02-19-2024, 07:22 PM
                    2 responses
                    66 views
                    0 likes
                    Last Post lbadisa1  
                    Started by JGriff5646, Yesterday, 05:47 PM
                    1 response
                    15 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by AlphaOptions, 06-18-2013, 08:24 AM
                    9 responses
                    2,203 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Working...
                    X