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

Last Bar and Rectangle Properties

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

    Last Bar and Rectangle Properties

    Hello,

    I have manually drawn a rectangle on my chart and have written code to try to get the rectangle properties.
    When I run the indicator, I can print the object properties for the rectangle using DrawObject but it does this for every bar.
    I can get the properties for the object but do not know how to get the properties for the rectangle (such as StartBarsAgo, StartTime, StartY, etc).

    How do I print the properties for the rectangle on the last bar of the chart so I do not end up with multiple print statements for the same rectangle ? When the properties have been printed once on the last bar, I would like to continue to print in real time for each successive bar when it has closed.
    I also want to print the rectangle properties in the same way.

    I have attached my code below.

    Any help or suggestions would be most appreciated.

    Thanks
    Attached Files

    #2
    Hello sun100,

    Thank you for your inquiry.

    If you would like it so your prints do not occur on historical bars, you would want to wrap your entire code in an if (!Historical) statement.

    Example:
    Code:
    if (!Historical)
    {
         foreach (IDrawObject draw in DrawObjects)
         .....
    }
    Please take a look at the NinjaTrader help guide at this link for further information about using Historical: http://ninjatrader.com/support/helpG...historical.htm

    In order to access the properties for the rectangle, what you could do is create a new IRectangle object and cast your draw IDrawObject into an IRectangle.

    Example:
    Code:
    if (draw is IRectangle)
    {
         Print(.....);
         IRectangle rectangle = (IRectangle)draw;
         Print(rectangle.StartY);
         .....
    }
    For further information about casting, please take a look at this link: http://www.dotnetperls.com/cast

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hello Zachary

      Thank you very much for your prompt reply.
      I tried out the code as you suggested and it worked fine.

      I found that if I used the Historical = false setting, it would process the data in real time but if my bar interval was quite long, I would need to wait a long time for the rectangle to be drawn. So I added the test for CurrentBar == 1 to draw the rectangle on 1 bar of Historical data before processing in real time. This seems to work.

      I now have all of the rectangle properties that I wish to print out. I am trying to extend the rectangle to the current date and time using the DrawRectangle method. It can be called in three different ways and I am trying to call it using the third way so I can modify the EndTime. This requires a color input parameter before the AreaColor parameter but I am not sure how to obtain the Color value for the rectangle I am processing.

      Does the Color parameter refer to the rectangle outline border and how do I get it for my manually drawn rectangle ?
      Also, is the DrawRectangle method the only way to modify an existing rectangle by using its tag to reference it, or is there any shortcut way to change the EndTime value ?


      I have attached my new code below.
      Any help or pointers would be much appreciated.

      Thanks

      Sun100
      Attached Files

      Comment


        #4
        Hello sun100,

        The color property in this case would be the Pen that draws the outline. Using your code, you can access:

        RectColor = rectangle.Pen.Color;

        You might be better off using the 2nd overload method of just identifying bars rather than time. In this way you can simply put the end bar at 0 bars ago rather than fussing with time. Using 0 will also the rectangle to keep up with the current bar. This also works better historically as well.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hello

          Thank you for your reply Paul.
          I have amended the code as you suggested and am getting the following error message :

          Error on calling 'OnBarUpdate' method for indicator 'MyTest1': Collection was modified; enumeration operation may not execute.

          I have no idea what this means and am a bit stuck.
          Any help or suggestions would be most appreciated.

          Thanks.
          Attached Files

          Comment


            #6
            Hello sun100,

            Thanks for your reply.

            The issue is that you are attempting to modify (redraw) the properties while you are inside the foreach loop (where you have collected the properties of the object). If I understand correctly your goal is to determine a manually drawn rectangles properties and then extend that rectangle. The foreach loops gets you the properties of the manually drawn object. The draw method would need to be done outside of the foreach loop to avoid changing the properties as they are known within the collection.

            What you may want to do is to add a bool (init to true) to process the original objects once to get the properties (then set the bool to false) and then on each onbarupdate, extend the length of the rectangle by using 0 representing the current bar.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hello Paul

              Thank you for your reply.
              I have been working on this for a few days but still cannot get anywhere with this. I have modified the code as you suggested (I think).
              When I have manually drawn rectangles on the chart, the DrawObjects.Count returns 0, so it is not even seeing them. I don't know why that is happening. I have tried compiling and saving the code, then reloading the indicator but it makes no difference.
              I have attached the code below.
              Any help would be most appreciated as I seem to be stuck.

              Thanks
              Attached Files

              Comment


                #8
                Hello sun100,

                Thanks for your reply.

                I have attached sample indicator file I made from your previous code. Please note that for the purpose of demonstration, first manually draw a rectangle on the chart, then add the demo indicator. What will happen is that is will find the one drawn rectangle object, get its properties and then extends the rectangle to the right bar by bar. The bool doitonce is to scan for objects once and then after it no longer looks for objects and the onbarupdate merely causes the draw object to execute from the original coordinates. (You will need to add the cs file to your indicators folder and then edit/compile)

                I've created a (no sound) video of the application to a 10 second chart just to show quick updates. http://screencast.com/t/WA2l6s5v2c
                Attached Files
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Paul

                  Thank you for your reply.
                  The screen video and code have been very useful.
                  I am now able to carry on developing my code.

                  Sun100

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by techgetgame, Yesterday, 11:42 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post techgetgame  
                  Started by sephichapdson, Yesterday, 11:36 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post sephichapdson  
                  Started by bortz, 11-06-2023, 08:04 AM
                  47 responses
                  1,613 views
                  0 likes
                  Last Post aligator  
                  Started by jaybedreamin, Yesterday, 05:56 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post jaybedreamin  
                  Started by DJ888, 04-16-2024, 06:09 PM
                  6 responses
                  20 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Working...
                  X