Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is there away to automatically extend a Drawing Object

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

    Is there away to automatically extend a Drawing Object

    Is there a way to tell a Drawing Object to extend upon a new barupdate outside of coding it. For example I manually place a rectangle on a chart and I want it to extend out in time as each new bar interval is completed.

    #2
    Hello,

    Thank you for the question.

    Outside of NinjaScript there would not be a way to "move" a drawing object or add additional bars to its start and end point to keep up with the current bar.

    This would be something that can be easily accomplished using NinjaScript but is not a option just from the drawing objects in the chart tools as you specify a time for these objects to be placed at.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      When you say ninjascript are you referring to writing C# code. I have written some code for NT, but then read something about if you drew a rectangle manual then tried to update it in code that it did not work. I was just looking for the simplest solution. Do you have an example of this code?

      Comment


        #4
        Hello,

        Yes when you hear NinjaScript that is referring to NinjaTraders coding which is C#.

        You are correct, manually drawn objects can not be moved with script but you can access other properties such as the values of its points and can set its colors.

        The only way to move a drawing object would be if it was drawn with script and then modified using script.

        There is an example on how to draw a rectangle in the help guide located here which outlines the basics of drawing a rectangle. http://www.ninjatrader.com/support/h...wrectangle.htm



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Is there any code example of how to capture manually drawn rectangles. That way I can draw my rectangle manually and then in code find those manually drawn rectangles, then draw them automatically and then keep updating them.

          Comment


            #6
            Hello,

            There is a non supported way to access the drawing objects I had posted on a while back. Here is a link to the post:


            Essentially you need to cast the object as the correct type to get the correct properties for the object as seen in the example using a Ray that is a type of IRay. You could change the Ray and IRay to Rectangle and IRectangle.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              I have a basic understanding of C#, but not an expert by any means. There is a line of code in there that I do not understand. I see DrawObjects is a collection.

              And I see you find the ray and use a reference to IRay interface, And I see the need to cast the DrawObject into an IRay such as (IRay) ray, but can you explain to me what this statement does and how it does it? I assume it cast the current Draw Object into an IRay.


              IRay ray = draw as IRay;


              Code Below:

              foreach (IDrawObject draw in DrawObjects)
              {
              if(draw.UserDrawn)
              {
              //drawn by the user
              Print(draw.DrawType.ToString()); //you can get the tag of the object to help narrow down results
              if(draw.DrawType == DrawType.Ray) //if the object type is a ray do this
              {
              IRay ray = draw as IRay; //need to re define the IDrawingObject as a Ray to access the correct values
              Print(ray.Anchor1Y); //now ray has all of the Ray methods.
              }
              } else {
              //drawn by NinjaScripting
              }
              }

              Comment


                #8
                Hello,

                Thank you for the question.

                If you are referring to the line:

                IRay ray = draw as IRay;


                This is the cast, so what is happening is we defining a new variable of type IRay or the line:

                Code:
                IRay ray
                Next we assign the current object that has been iterated through the foreach statement to the variable ray or:

                Code:
                IRay ray = draw
                Next because draw is currently of type IDrawObject and not IRay, we have to do a cast to tell the compiler to treat the object draw if it were a IRay instead of a IDrawObject or:

                Code:
                IRay ray = draw as IRay;
                An alternative method of casting for inline casts would be using parenthesis or:
                Code:
                IRay ray = (IRay)draw;
                Please let me know if I may be of additional assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Follow up question. Following your advice I was able to access the drawing objects.

                  When I originally drew the rectangle (earlier in the code not shown below) I set the TAG field. When I extract using IRectangle I can extract all the information including the Tag, but I want to be able to change the TAG and it will not allow me to do this, Here is my code:

                  foreach (IDrawObject draw in DrawObjects)
                  {
                  if(!draw.UserDrawn) // Drawn by the program
                  {
                  if(draw.DrawType == DrawType.Rectangle)
                  {
                  IRectangle rect = draw as IRectangle;
                  Print("orignal Tag from rect " + rect.Tag);

                  String tag = "F1";

                  rect.Tag = tag; // This does not work


                  So how would I change the TAG field of the rectangle I drew earlier. I only want to change some of the TAGs so I loop through to find the one I want to change and all that works, but I can not figure out how to change the original new TAG. The only thing I saw was I could use RemoveDrawingObjects and then add a new Rectangle with my new Tag.


                  I tried to do RemoveDrawObject(rect.Tag) and then draw a new object and I get the following error.

                  Error on calling 'OnBarUpdate' method for indicator 'Test11' on bar 58: Collection was modified; enumeration operation may not execute.
                  Last edited by rwbil; 03-06-2015, 08:41 AM.

                  Comment


                    #10
                    Hello,

                    Thank you for the question.

                    This is due to the way the objects are designed.

                    A Drawing object has only a few items you can set, in this case the Tag has no setter or is read only from script.

                    the only way to change the tag would be to remove the object and re create a new object with the new tag and parameters.

                    if you choose to use RemoveDrawObject instead of removing them all you will run into a problem when doing this inside the foreach statement as shown previously. Here is the error you would receive:

                    Code:
                    Error on calling 'OnBarUpdate' method for indicator: Collection was modified; enumeration operation may not execute.
                    This is because we effectively change the count of the collection while looping through it which foreach specifically does not really like, it will remove the single object but then exit the loop and throw the error in the output.

                    To get around this you could use a list, I will provide a simple example below on how to create the list and then remove the objects without causing the error.

                    this requires adding the using statement:

                    Code:
                    using System.Collections.Generic;
                    Code:
                    List<IRectangle> tempList = new List<IRectangle>();
                    
                    foreach (IDrawObject draw in DrawObjects)
                    {
                    	if (draw.UserDrawn) continue; // continue looping but skip userdrawn objects
                    	if (draw.DrawType != DrawType.Rectangle) continue; // continue looping but skip objects that are not a rectangle 
                    	tempList.Add(draw as IRectangle); //add the object to a list so we can manipulate it later
                    }
                    
                    foreach (IRectangle rect in tempList) //now loop through the collected objects outside of the original collection so there is no error
                    {
                    	String tag = "F1";
                    	if (rect == null) continue;
                    	DrawRectangle(tag, rect.StartBarsAgo, rect.StartY, rect.EndBarsAgo, rect.EndBarsAgo, rect.Pen.Color); //create the new rectangle before removing the old rectangle
                    	RemoveDrawObject(rect);
                    }
                    This would loop through and find rectangles, add them to a list that we can modify without error, and then creates a new object based on the old object but with a new tag and then removes the old object.

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      That makes sense. I will try it later today, but wanted to ask you another question about drawing objects. If I move just one bar over in some cases my rectangle just disappears. And I have tried it for both manually and programmed drawing. Here is a manually drawn rectangle

                      Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


                      I move over just one bar and the scale is not changing and I get




                      The rectangle just disappears.

                      Is there a way to stop the rectangles from disappearing.

                      Comment


                        #12
                        Hello,

                        This looks to be what happens when a object is larger than the viewable area and moves outside the viewable area.

                        What is happening is internally there is a check for performance, if a drawing object is outside of the viewable area it is hidden or not drawn. This is expected and unfortunately can not be changed with drawing objects specifically.

                        What can be done is the use of the Plot override, the only side effect is that the Plot override is much more advanced than just using drawing objects.

                        The Plot override bypasses drawing objects and allows you to use the Graphics component in C# which in turn lets you draw whatever you want. You also can check if the object is in the viewable area or not to display it or not, or just not check at all and continue drawing.
                        There is an example in the indicators CustomPlotSample, you will find the Miscellaneous region which holds the Plot override.

                        This would really be the only way to get around that but again this is more advanced and there is not really any documentation on this so it assumes you know about using C# drawing with Graphics.

                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Jesse View Post
                          Hello,

                          This looks to be what happens when a object is larger than the viewable area and moves outside the viewable area.
                          This seems odd. The rectangle I drew was not outside the viewable area in the Y axis and the Scale X or Y did not change due to moving the bar over one. It was outside the area in the X axis on both charts. I do not see how it was draw being outside the X axis viewable area in one chart but yet not in the other as nothing changed between charts except to move one bar over. Just hard to see how performance changed one bar over.

                          Comment


                            #14
                            Hello,

                            It would still be removed even if it was one bar.

                            When one side of the object or two points on the rectangle are outside of the viewable area it can be hidden in certain cases.
                            The way I know that is repeatable with this is actually if all 4 points on the rectangle are outside of the viewable area or if you are looking at the center of the object and its left points are outside of the viewable area by at least 1 bar and the right points are out of the viewable area by at least 1 bar.

                            This would make the object disappear even though the vertical scaling did not change only the object has moved 1 bar outside of the viewable area and has no visible anchors.

                            This is a check that is done internally and there is no way around it aside from drawing on your own using the plot override.

                            I look forward to being of further assistance.
                            JesseNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by traderqz, Today, 12:06 AM
                            2 responses
                            3 views
                            0 likes
                            Last Post traderqz  
                            Started by RideMe, 04-07-2024, 04:54 PM
                            5 responses
                            28 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by f.saeidi, Today, 08:13 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by DavidHP, Today, 07:56 AM
                            1 response
                            6 views
                            0 likes
                            Last Post NinjaTrader_Erick  
                            Started by kujista, Today, 06:23 AM
                            3 responses
                            11 views
                            0 likes
                            Last Post kujista
                            by kujista
                             
                            Working...
                            X