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

Get rectangles from chart and their upper and lower prices

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

    Get rectangles from chart and their upper and lower prices

    Hi,

    This seems a strange problem, for me at least.
    I've used the drawingtools_drawobjects from the helpguide and an old article LINK
    The debug print output shows many found objects:

    Found: Line with tag: Line 8891Userdrawn is True
    Found: Line with tag: Line 8892Userdrawn is True
    Found: Line with tag: Line 8893Userdrawn is True
    Found: Rectangle with tag: Rectangle 2567Userdrawn is True
    Found: Rectangle with tag: Rectangle 2568Userdrawn is True
    Found: Rectangle with tag: Rectangle 2569Userdrawn is True​


    But the condition is never true and it never passes any of the statements I tried: if (draw is DrawingTools.Rectangle)

    What am I doing wrong? (Ultimately I want to call playsound when price touches a rectangle in realtime )
    2nd question, how to make this faster so it doesnt go through every single drawing object, this DrawObjects.ToList() seems very slow if only looking for rectangles?

    Code:
    //Print("in onbarupdate");
    //if (State != State.Realtime) return; // waits until real time connected then finds rectangle (only did for this test)
    Print("realtime in onbarupdate");
    if (DrawObjects.Count > 0) // Make sure we have an objects first
    {
    Print("count is more than zero");
    foreach (DrawingTool draw in DrawObjects.ToList()) // Go through objects
    {
    // Retrieve the type of the current object
    Type objectType = draw.GetType();
    
    // Print the type of the object found
    Print("Found: " + objectType.Name + " with tag: " + draw.Tag + "Userdrawn is " + draw.IsUserDrawn);
    
    if (objectType.Equals(typeof(DrawingTools.Rectangle)) && draw.IsUserDrawn)
    {
    Print("found rectangle");
    DrawingTools.Rectangle rectangle = draw as DrawingTools.Rectangle;
    
    // Get the tag of the rectangle
    string rectangleTag = rectangle.Tag;
    
    // Get the anchor points of the rectangle
    double upperPrice = rectangle.StartAnchor.Price;
    double lowerPrice = rectangle.EndAnchor.Price;
    
    // Print the type, tag, upper price, and lower price of the manually drawn rectangle
    Print("Object Type: " + objectType.Name + ", Rectangle Tag: " + rectangleTag + ", Upper Price: " + upperPrice + ", Lower Price: " + lowerPrice);
    }
    
    if (draw is DrawingTools.Rectangle) // test to find rectangle
    {
    Print("found rectangle");
    Print ("Tag name: "+draw.Tag); // priint tag name
    DrawingTools.Rectangle temp = draw as DrawingTools.Rectangle; // create copy to get anchorpoints
    
    Print("startY: " + temp.StartAnchor.Price);
    Print("startX: " + temp.StartAnchor.Time);
    
    Print("endY: " + temp.EndAnchor.Price);
    Print("endX: " + temp.EndAnchor.Time);
    }
    }
    }
    Attached Files
    Last edited by Creamers; 02-25-2024, 02:12 PM.

    #2
    Hello Creamers,

    Thanks for your post.

    That is correct, you would need to loop through the DrawObjects collection to detect a drawn object on the chart window and get information about a draw object.

    To detect only rectangles you could loop through the DrawObjects collection and check if (draw is DrawingTools.Rectangle) to only check for a rectangle draw object on the chart. Then, you could create a DrawingTools.Rectangle variable and assign draw as DrawingTools.Rectangle to the variable. Once you've done that you could access values from the draw object on the chart.

    If you want to use the StartAnchor.Price and EndAnchor.Price for conditions later in the script, you could save those values to class-level double variables and then use those variables later in the script.

    I have attached a simple example script that detects a rectangle on the chart and prints out the draw object's Tag, IsUserDrawn, StartAnchor.Price, and EndAnchor.Price properties to the NinjaScript Output window.

    Also attached to this post is a screenshot of the indicator working.

    See this help guide page for more information about the DrawObjects collection: https://ninjatrader.com/support/helpGuides/nt8/drawingtools_drawobjects.htm​
    Attached Files
    Last edited by NinjaTrader_BrandonH; 03-03-2024, 06:39 PM.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Think you didn't read my complete writing.
      Your example is like mine except you don't use a if statement.
      Before the if statement everything is indeed showing up correctly in the printlog.
      But the if-sattement condition is never true when there is a rectangle found.

      Comment


        #4
        Hello Creamers,

        Thanks for your notes.

        So we may accurately assist, which if statement are you referring to from the code you shared in post # 1?

        Note that .GetType() and .Equals(Type o) are not documented/supported NinjaScript methods or properties so using those methods in a script would go outside of the support we would be able to provide you with in the Support Department at NinjaTrader.

        The supported way to get the StartAnchor.Price and EndAnchor.Price of a rectangle DrawingTool on the chart could be seen in the sample code attached to post # 2.

        The documented/supported DrawingTool properties could be seen in the Help Guide documentation linked below.

        IDrawingTool: https://ninjatrader.com/support/help...rawingtool.htm
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Was just looking for you attached example but only see the screenshot. I do remember seeing it last time. Am I correct ?

          In my code you can see two statements but the problem exits in both of them:

          Code:
          if (objectType.Equals(typeof(DrawingTools.Rectangle)) && draw.IsUserDrawn)
          Code:
          f (draw is DrawingTools.Rectangle)
          To repeat, it will find objects, but the statement to find only the rectangles is never true.
          Last edited by Creamers; 03-02-2024, 10:24 AM.

          Comment


            #6
            Hello Creamers,

            Thanks for your notes.

            It seems the example was hidden from the forum post. I have corrected this so the example script appear on post # 2.

            I have also attached the example script to this forum post.

            objectType.Equals() is not a supported NinjaScript method or property so using this in your script would be up to your own discretion. Since it is unsupported we cannot guarantee how it would function in your script.

            To detect rectangle draw objects in the script, you could use if (draw is DrawingTools.Rectangle) when looping through the DrawObjects collection as seen in the DrawObjectsRectangle example script attached.

            Please modify the test script so that it reproduces the behavior and then share the modified copy with us so we may test it on our end.
            Attached Files
            Last edited by NinjaTrader_BrandonH; 03-03-2024, 06:46 PM.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Your example works. When I pick my piece of example and put that in your code it also works. Strange thing though but I can only admit that it works.
              Except, it's very slow. Or is it just because I print them all in the debug window. ?
              Could this be faster if I wanted the last one drawn on the chart ?
              Thank you again for your patience!
              Last edited by Creamers; 03-04-2024, 02:14 PM.

              Comment


                #8
                Hello Creamers,

                Thanks for your notes.

                The more drawing objects that are drawn on the chart, the more processing the script will need to do to find rectangle draw objects on the chart.

                You should remove any unneeded drawing objects from the chart window. Especially over time as you have more drawing objects on your charts the performance can decline since there is more to render.

                Ensure that the indicator applied is using the Calculate setting of On price change or On bar close. There is no benefit to using On each tick unless the indicator deals with volume or tick counting.

                Performance Tips could be found on this help guide page to improve overall performance: https://ninjatrader.com/support/help...ance_tips2.htm
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you again and you're right. Decreased the total chart days and finally solved my case with your help!
                  A rectangle with the name sound wil change color and play a sound to alert me. Thank you very much!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Haiasi, 04-25-2024, 06:53 PM
                  2 responses
                  17 views
                  0 likes
                  Last Post Massinisa  
                  Started by Creamers, Today, 05:32 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post Creamers  
                  Started by Segwin, 05-07-2018, 02:15 PM
                  12 responses
                  1,786 views
                  0 likes
                  Last Post Leafcutter  
                  Started by poplagelu, Today, 05:00 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post poplagelu  
                  Started by fx.practic, 10-15-2013, 12:53 AM
                  5 responses
                  5,408 views
                  0 likes
                  Last Post Bidder
                  by Bidder
                   
                  Working...
                  X