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

How to stop drawing Draw.Rectangle()

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

    How to stop drawing Draw.Rectangle()

    Hi, I have tried to write a little code that will start and stop the Draw.Rectangle() function. I would like the Rectangle to start plotting when the current EMA(Low) is broke and then stop ploting when the current EMA(High) is broke future bars from now. This is what I have so far. Just can't seem to get it to work.



    This dose not work. Any suggestions would be helpful?
    Thanks, Woody.

    #2
    Hello woodyfox,

    Thanks for your post.

    Typically you would want to use print statement in your code to debug. While it can be tedious it is one way to identify for certain what is happening in your code. Here is a link to our debugging tips: https://ninjatrader.com/support/foru...ead.php?t=3418

    You would add print statements to print the value of the variables in your conditions statements to help understand why or why not conditions are true.

    Taking a quick look at your code, it appears that you are setting Upper[0] and Lower[0] to the exact same value of EMA(High, 20).
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Paul for getting back quick,
      I changed the Lower values to the EMA(Low,20). I also added a condition to store the values of Upper and Lower, so it will not change on every bar update. I tried debugging it and didn't find anything? Compiles fine, but still will not draw.Rectangle.
      Could you check it once more and see if you see my mistake somewhere. It would be really appreciated.
      Thanks Woody,



      Comment


        #4
        Hello woodyfox,

        Thanks for your reply.

        Please check the "log" tab of the control center, do you see any errors related to your indicator?

        If you put a print statement just before Draw.Rectangle, do you see the print statement's output in the output window?
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Paul,
          I am getting no error logs and my output window is blank. Could it be something to do with how I used the stopDrawing bool? Or maybe Calculate.OnBarClose.
          Thanks,

          Comment


            #6
            Hello woodyfox,

            Thanks for your reply.

            Please add a print statement like this:

            if (Close[0] < EMA(Low, 20)[0] && Close[1] >= EMA(Low, 20)[1])
            {
            Print ("Condition found on "+CurrentBar);
            Draw.Rectangle(this, "rect"+CurrentBar, false, barsAgo, Lower[0], 0, Upper[0], Brushes.Blue, Brushes.Blue, 70);
            }


            Please note that I added {} to encompass both the print and the draw statement so they both will execute when the condition is true.

            Open an output window with New>output window. After you have recompiled the indicator, remove it from the chart then reapply it to the chart to ensure that you are working with a fresh instance (important during development/debug work).

            Do you see any output that shows, "Condition found on "? If not then:

            Please review your logic of if (Close[0] < EMA(Low, 20)[0] && Close[1] >= EMA(Low, 20)[1]) , should both be based on the low of the series?

            Other observations:

            The barsAgo does nothing except increment, meaning it will count the same number of bars as CurrentBar. Do you mean to reset the counter at some point?

            Once stopDrawing is set true, you have no means to set it false again.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Paul,
              Starting from the beginning with my Values? I added an else statement to keep the values until the next condition is true.
              But,
              Using a print statement, I find my Upper values are printing fine, but my Lower values are not. The Lower Values stay a zero?

              Code:



              Would my CurrentBar Statement be incorrect?
              Thanks again, Woody

              Comment


                #8
                Hello woodyfox,

                Thanks for your reply.

                Please note that the "else" condition would only impact the first line after because you did not use braces like you did in the If statement to include both statements, so the second line is executed on every OnbarUpdate().
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Paul, That did fix my values problem. Thanks.
                  Before I try to go any further with this, Is it even possible to start Draw.Rectangle() and stop Draw.Rectrangle() using OnBarUpdate or do you have to use OnRender?

                  If you can use OnBarUpdate, is there and example of this for NT 8?
                  Thanks, Woody.

                  Comment


                    #10
                    Hi woodyfox,

                    Thanks for your reply.

                    No, you do not need to use OnRender. I've taken the liberty to create a sample of what *I think* you are wanting to do or at least will get you closer. Please take a look at the attached, you should be able to replicate and test.

                    If nothing else it should show you how to create individual rectangles that auto adjust per bar over the bar period (rather than drawing a new rectangle per bar which your code would do). Used bools to control if it was a new rectangle or use/extend existing rectangle.
                    Attached Files
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Paul,
                      I will study your example and see how it can fit what I intend to do. Thanks for being patient with me as I am fairly new to coding. Believe it or not I have come along way? Thanks for taking the time to help.
                      Woody,

                      Comment


                        #12
                        Paul,
                        The sample you posted is to stop Draw.Rectangle barsAgo. So start Draw.Rectangle when condition happens and then draw barsAgo until stop Draw.Rectangle condition happens.

                        Is it possible to start Draw.Rectangle when condition happens and then draw barsForward and then stop when stop Draw.Rectangle condition happens?

                        I guess my next question would be? Since the Upper[0] value is Dynamic, would it be possible to keep the Upper[0] value Static for that Draw.Rectange?

                        Are there examples of this on the forum or an indicator that does something similar?
                        Thanks, Woody.

                        Comment


                          #13
                          Hello woodyfox,

                          Thanks for your reply.

                          What the sample code is doing is drawing a rectangle when the Close price is less than the EMA20 based on the low price series and will continue drawing the same rectangle from that first bar to the current bar using the current width of the Lower to Upper spread until such time as the Close Price crosses the Upper EMA 20 on the high series. So the width dynamically adjusts according to the spread between Upper[0] and lower[0].

                          You can keep the spread static by saving the values of the Upper[0] & Lower[0] into double variables in the section of code under if(firstBar). Then use those variables in the Draw.rectangle inplace of the Upper[0] and Lower[0] .
                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            Paul,
                            I created new Doubles to keep the spread static, but the Draw.Rectangle only plots on firstBar and does not continue ploting to stopDrawing? I'm wondering if I need to add conditions to keep the Double Values after firstBar.



                            Thanks, Woody.

                            Comment


                              #15
                              Hello woodyfox,

                              Thanks for your reply.

                              You are correctly saving the values however you are saving them at some index that will change on the next bar and to continue drawing you need the values to remain constant. You do not need to create two data series and this is causing your drawing errors.

                              You need only create two double variables (declare them in the same area where you declare your series, at the class level: private double uplev, lowlev;

                              Then, in the OnbarUpdate area:

                              if (firstBar)
                              {
                              startBar = CurrentBar;
                              firstBar = false;
                              uplev = Upper[0]; // Save upper value on first bar
                              lowlev = Lower[0]; // save lower value on first bar

                              }
                              }
                              if (Close[0] > Upper[0])
                              {
                              firstBar = true;
                              stopDrawing = true;
                              }
                              if (!stopDrawing)
                              {
                              Draw.Rectangle(this, "rect"+startBar, false, CurrentBar - startBar, lowlev,
                              0, uplev, Brushes.Blue, Brushes.Blue, 50);
                              }
                              Paul H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Stanfillirenfro, Yesterday, 09:19 AM
                              7 responses
                              51 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by TraderCro, 04-12-2024, 11:36 AM
                              4 responses
                              70 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Mindset, Yesterday, 02:04 AM
                              1 response
                              15 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by agclub, 04-21-2024, 08:57 PM
                              4 responses
                              18 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by Irukandji, Today, 04:58 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Irukandji  
                              Working...
                              X