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

Drawing Rectangles. Not sure what I'm doing wrong

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

    Drawing Rectangles. Not sure what I'm doing wrong

    Hi,

    There are two things with the rectangles in this chart that doesn’t seem right:-
    1. There seems to be a new rectangle being drawn as every new bar is formed, rather than extending the original rectangle. How do I get that to happen?
    2. The current rectangle stops extending when a new rectangle is being drawn even though the condition to stop drawing the current rectangle is not valid. How do I get them to draw concurrently till the condition to stop drawing is fulfilled?
    What am I doing wrong here?
    I’ve attached the code and the chart.
    Attached Files

    #2
    Hello kaywai,

    Thanks for your post.

    If you pass in a value of say "myTag" for the Tag parameter of the Draw.Rectangle() method, each time this tag ("myTag") is used when the Draw.Rectangle() method is called, the same draw object is modified. If unique tags are used each time, a new draw object will be created each time this method is called.

    You could see the information above noted on the Draw.Rectangle() help guide page linked here: https://ninjatrader.com/support/help..._rectangle.htm

    This means that you could call Draw.Rectangle() with the same Tag name to modify the drawing object with that Tag name on the chart. Otherwise, if a new unique Tag name is used when Draw.Rectangle() is called, a new rectangle will be drawn on the chart.

    Please let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi BrandonH,

      Thank you for your reply.
      I thought I had shared my entire code in my post but I may have forgotten to attach it.

      I did have a unique tag name for each Draw.Rectangle......"GapUp"+CurrentBar but I didn't get the desired result. The entire code is the pasted below:-

      Code:
      if (CurrentBar < (2)) return;
                  int count = 0;
      
                  if( Low[0] > High [1])
                  {
                      GapUp = true;
                      GapHigh = High[1];
                      GapLow = Low[0];
                      GapLength = CurrentBar;
                      count++;
      
                  }
                      if (GapUp == true)
                      {
                          if( Low[0] > GapHigh)
                          {
                              Draw.Rectangle(this, "GapUp"+CurrentBar,false, 1, GapLow, CurrentBar-GapLength, GapHigh, Brushes.Green, Brushes.Green,10);
                              count++;
                          }
                          else if(Low[0] <= GapHigh)
                          {
                              Draw.Rectangle(this, "GapUp1"+CurrentBar, false, count+2, GapLow, 0, GapHigh, Brushes.Green, Brushes.Green, 10);
                              GapUp=false;
                              count = 0;
                          }
                      }​

      Comment


        #4
        Hello kaywai,

        Thanks for your note.

        The reason new rectangle drawing objects are being drawn instead of the existing drawing object being modified is cause you are using a unique tag name each time the Draw.Rectangle() method is called.

        If you would like to modify an existing drawing object, you would need to call Draw.Rectangle() and use the same exact Tag name as the drawing object on the chart that you want to modify. This information could be seen in my previous post.

        "This means that you could call Draw.Rectangle() with the same Tag name to modify the drawing object with that Tag name on the chart."

        See the Draw.Rectangle() help guide page linked here for more information: https://ninjatrader.com/support/help..._rectangle.htm

        Please let me know if I may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi BrandonH,

          Thank you for your reply.

          What I'm trying to achieve is to draw a rectangle whenever there is a gap between the previous high and the current low and to have the rectangle extend to the point where the current low is less than or equal to the Gap High. While waiting for the gap to close, there may inadvertently be other gap ups that occur and each time a gap occurs, to have a new rectangle to be drawn and extended till the current low is less than or equal to the Gap High. So, there would be occasions where rectangles would be drawn concurrently.

          Currently, everytime a new gap occurs, a new rectangle is drawn but the old gap stops extending even though the low is not yet less than the relevant Gap High.

          But when there are no new gaps occuring, I am getting the desired result - the rectangles draw till the low is less than its relevant Gap High.

          The other thing which is really more cosmetic is the little rectangles being drawn after a new bar is completed. I'm trying to get 1 long rectangle rather than many rectangles with the width of a bar.

          Hope you can assist me.

          Regards

          Kay Wai

          Comment


            #6
            Hello Kay Wai,

            Thanks for your note.

            "I'm trying to get 1 long rectangle rather than many rectangles with the width of a bar."

            This behavior is occurring because you are drawing multiple rectangle drawing objects on the chart one after another, not modifying the same drawing object to extend to a further value.

            To modify the same rectangle drawing object, you would need to call the Draw.Rectangle() method and provide the exact same Tag name that that specific rectangle drawing object is using.

            For example, say you call Draw.Rectangle(this, "tag1", 1, Low[0] - TickSize, 0, High[0] + TickSize, Brushes.Blue);

            To modify this rectangle drawing object, you would need to call Draw.Rectangle() again and use the same Tag name.

            Draw.Rectangle(this, "tag1", 2, Low[1] - TickSize, 0, High[0] + TickSize, Brushes.Blue);

            Note that the Tag name is the exact same and we provide different values for the startBarsAgo and startY arguments to modify that drawing object to use new values. This would 'extend' that rectangle to be 2 bars wide instead of the previous 1 bar wide.

            We could extend the drawing object further by calling the Draw.Rectangle() method again within a condition and use the same Tag name ("tag1") and different values

            This demonstration video shows the above code in action: https://brandonh-ninjatrader.tinytak...MF8yMDg2Nzc1Mg

            You could get a drawing object by its specific tag name by looping through the DrawObjects collection or your could loop through the collection to find specific draw objects. Once you have that drawing object's tag name, you could call Draw.Rectangle() using that Tag name to modify that specific draw object.

            See this help guide page for more information and sample code: https://ninjatrader.com/support/help...rawobjects.htm

            To understand why the script is behaving as it is, such as drawing objects when expected or not expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

            Debugging prints should be added to the script (outside of the condition) that prints out each value used in the condition used to draw the object. You should also add prints to the script that prints out the values being used for the Draw.Rectangle method. Prints will appear in a New > NinjaScript Output window.

            Below is a link to a forum post that demonstrates how to use prints to understand behavior.

            https://ninjatrader.com/support/foru...121#post791121

            Please let me know if I may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Hi BrandonH,

              Sorry for the delayed response.

              I got the "tags" to work the way I intended them to do after you explained it to me. Please see attached.

              The only part I'm stuck with now is to have concurrent rectangles drawing at the same time. I've attached a chart of that too. The blue shaded rectangles are the parts I'm missing because as discussed above (earlier part of the thread), it stops extending after a new rectangle starts drawing. I'm not sure what I can do to make that happen. Could you please point me in the right direction?

              My entire code is posted below:-

              Code:
              protected override void OnBarUpdate()
                      {
                          if (CurrentBar < (2)) return;
              
              
                          if( Low[0] > High [1])
                          {
                              GapUp = true;
                              GapHigh = High[1];
                              GapLow = Low[0];
                              GapLength = CurrentBar;
                              drawTag = "GapuUP" +CurrentBar;
                          }
              
                          if (GapUp == true)
                          {
                              if( Low[0] > GapHigh)
                              {
                                  Draw.Rectangle(this, drawTag, false, 1, GapLow, CurrentBar-GapLength, GapHigh, Brushes.Green, Brushes.Green,10);
              
                              }
                              else if(Low[0] <= GapHigh)
                              {
                                  Draw.Rectangle(this, drawTag, false, CurrentBar-GapLength, GapLow, 0, GapHigh, Brushes.Green, Brushes.Green, 10);
                                  GapUp=false;
              
                              }
                          }​
                      }​
              Regards
              Kay Wai
              Attached Files

              Comment


                #8
                Hello Kay Wai,

                Thanks for your note.

                To have multiple rectangles drawn on the chart at the same time, you would need to call the Draw.Rectangle() method multiple times and have each Draw.Rectangle method use a different Tag name.

                As stated in the Draw.Rectangle help guide page: "Tag is a user defined unique id used to reference the draw object. For example, if you pass in a value of "myTag", each time this tag is used, the same draw object is modified. If unique tags are used each time, a new draw object will be created each time."

                See this help guide page for information about Draw.Rectangle: https://ninjatrader.com/support/help...gle.htm​

                For example, the first Draw.Rectangle() method could have a Tag name of something like "Rectangle1". The second Draw.Rectangle() method could have a Tag name of something like "Rectangle2". A simple example that draws 2 rectangles on the chart when the Close price is greater than the Open price could be seen below.

                if (Close[0] > Open[0]
                {
                Draw.Rectangle(this, "Rectangle1", 10, Low[10] - TickSize, 5, High[5] + TickSize, Brushes.Blue);
                Draw.Rectangle(this, "Rectangle2", 4, Low[4] - TickSize, 0, High[0] + TickSize, Brushes.Red);
                }


                Then if you want to modify a rectangle draw object, you would call Draw.Rectangle() again and use the Tag name of the rectangle you want to change. In the example below, we modify the Draw.Rectangle() method with a Tag name of "Rectangle1".

                if (Close[0] < Open[0]
                {
                Draw.Rectangle(this, "Rectangle1", 7, Low[7] - TickSize, 3, High[3] + TickSize, Brushes.Blue);
                }


                Please let me know if I may assist you further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  HI BrandonH,

                  Thank you for your reply.

                  Technically the rectangles do not commence at the same time and only one commences at any one time. But while one is still drawing (as the condition to stop extending is not triggered yet), and a subsequent condition resulting in a new rectangle to be drawn, this is when the rectangles should be drawing concurrently.

                  So the tags should always be unique for each rectangle. But why the initial rectangle stops extending when a new one is drawn remains a mystery to me.

                  Regards
                  Kay Wai

                  Comment


                    #10
                    If one rectangle is drawn when if( Low[0] > GapHigh) and the other is drawn when if(Low[0] <= GapHigh), they will not draw at the same time because only one condition exists at a time. Is that the problem?
                    eDanny
                    NinjaTrader Ecosystem Vendor - Integrity Traders

                    Comment


                      #11
                      Hello Kay Wai,

                      Thanks for your note.

                      To understand how a script is behaving, such as drawing objects when expected or not expected, you need to add debugging prints to the script.

                      Below is a link to a forum post that demonstrates how to use prints to understand behavior.

                      https://ninjatrader.com/support/foru...121#post791121

                      Let us know if we may assist further.​
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        Ok. Will take another look again.

                        Comment


                          #13
                          Hi BrandonH,

                          I have added debugging prints to the script and made some changes as a result.

                          I am however still stuck at when the condition/criteria becomes true - the old rectangle stops drawing when the new rectangle begins. I note there is a change of tag to the new tag. I assume the old tag has been replaced and hence the old rectangle has stopped drawing.

                          How am I able to keep the old tag so that the old rectangle continue drawing till the condition to stop drawing becomes true while at the same time have the new rectangle continue to draw until the condition for it to stop drawing becomes true?

                          Regards
                          Kay Wai

                          Code:
                          if (CurrentBar < (2)) return;
                          
                          
                                      if( Low[0] > High [1])
                                      {
                                          GapUp = true;
                                          Gaps++;
                          
                                          drawTag = "GapUP" +CurrentBar;
                                          GapHigh = High[1];
                                          GapLow = Low[0];
                                          GapLength = CurrentBar;
                          
                                          Print( Time[0].ToString());
                                          Print (drawTag.ToString());
                                          Print (Gaps.ToString());
                          
                                      }
                          
                          
                          
                                      if ((GapUp == true) )
                                      {
                                         if( Low[0] > GapHigh)
                                          {
                                              Draw.Rectangle(this, drawTag, false, -1, GapLow, CurrentBar-GapLength+1, GapHigh, Brushes.Green, Brushes.Green,10);
                          
                                          }
                                          else if( Low[0] <= GapHigh )
                                          {
                                              GapUp = false;    
                                              Gaps = 0;
                                          }
                          
                                      }​

                          Comment


                            #14
                            Hello kaywai,

                            You need further uniqueness in your names.

                            like "GapUP" + objectIncrement + "-" + CurrentBar

                            and increment objectIncrement on each new current object.

                            if (/* new object condition*/)
                            {
                            objectIncrement++;
                            }
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi ChelseaB,

                              Not sure I follow you.

                              1) What is wrong with the current id that it requires further uniqueness? Why isn't + CurrentBar unique enough?
                              2) What do you mean by increment ojectIncrement on each new current object mean?
                              3) I have Gaps which is a counter to count the number of gapups. Should I be adding that to the unique id?

                              Regards
                              Kay Wai

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              167 responses
                              2,260 views
                              0 likes
                              Last Post jeronymite  
                              Started by warreng86, 11-10-2020, 02:04 PM
                              7 responses
                              1,361 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by Perr0Grande, Today, 08:16 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post Perr0Grande  
                              Started by elderan, Today, 08:03 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post elderan
                              by elderan
                               
                              Started by algospoke, Today, 06:40 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post algospoke  
                              Working...
                              X