Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Counting the number of touches of the rectangle.

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

    Counting the number of touches of the rectangle.

    The idea is to count every single touch of the rectangle drawn on the chart with the indicator. The first idea was to use the “for” loop and the number of the bars corresponding to the width of the rectangle. The problem here is that the number of bars makes the indicator lagging a lot on the chart.
    The second idea was just to use the Bar[0].

    When I have the platform off, the retests are zero, even if the retest has already occurred at least once before. When the platform is on, and another touch takes place, this touch is counted as one and every subsequent touch of this rectangle is also counted. If I go off and after a while the platform is on again, all numbers representing the retests turn to zero.

    Also, a zone can be resized in its height, increasing the number of touches. But when this happens, the number of touches remains the same as for the resize.
    I am wondering why the indicator does not count the already existing retest when I first go on.

    I have the following code:

    if(Close[0] touchesTheRectangle)
    {
    int Count1 += 1;
    }

    I have illustrated this behavior on the attached file.
    I would deeply appreciate any suggestion.
    Attached Files
    Last edited by Stanfillirenfro; 10-08-2024, 07:02 AM.

    #2
    Hello Stanfillirenfro,

    To see if the price has touched a rectangle, you would want to compare the market price, the anchor prices.

    For example:

    if (Close[0] >= myRectangle.StartAnchor.Price && Close[0] <= myRectangle.EndAnchor.Price)



    As far as lagging, this might indicate that the logic is overloading the CPU.
    You could choose to only run the logic on the first tick of a new bar when IsFirstTickOfBar is true.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Many thanks NinjaTrader_ChelseaB for your reply.

      The comparison:
      HTML Code:
      if (Close[0] >= myRectangle.StartAnchor.Price && Close[0] <= myRectangle.EndAnchor.Price)
      is exactly what I have done. It is also the reason why I am having some counts different from zero.

      Allow me please to explain again what is wrong with this code:
      Let´s imagine that I had a zone, let´s say a resistance yesterday with retests = 3. At the end of the trading session, I switch off the computer. Today, I switch on the computer and go online. The resistance zone of yesterday is still in play, but the retests = 0. After three hours, the prices come back and retest the resistance zone again. This time, I have retest = 1. That means, the other three retests of yesterday are not taken into account.

      My probblem is that I am failing to actualize the count so that all count of a zone are taking into account when I go online or when the zone is resized. Could you please advise me how to overcome this problem? Is there a condition to be writen somewhere so that this problem is fixed?

      Thanks in advance!

      Comment


        #4
        Hello Stanfillirenfro,

        I'm not able to suggest logic to use, but I can assist you through the debugging process to understand why the behavior is not as you expect.

        Below is a link to a support article on adding debugging prints to understand behavior.


        Are these values recalculated in the historical data?

        If you are wanting to store values that are not recalculated in the historical data, you could write the values to a text file and then read from the file using a C# StreamWriter and StreamReader.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Many thanks again NinjaTrader_ChelseaB for your help.

          I do not have any problem with the debugging. I have attached an example of the result of the Print() function that I am following since I have switched on the computer today.
          I think I will be looking the problem in the historical data maybe?

          I have also foung a zip file on the rectangle in the forum, but the code was generated using an old version of Ninjatrader. I would be grateful to you if you could convert this version so that I can use it with the newest version of Ninjatrader I have currently. File also attached.

          I have specified here two issues.

          Many thanks in advance.
          Attached Files

          Comment


            #6
            Hello Stanfillirenfro,

            The print output should be the values used in the condition with labels for the comparison operators to show how the values are being compared in the condition.
            The bar datetime is also included to know what bar the print is for.
            All values compared in the condition should be included with the print.
            Further, the value of the variable should also be printed after being assigned.

            The videos in the support article demonstrate creating an informative print for the condition and how to use this to analyze the behavior.

            With the screenshot you have provided, this doesn't show how the values are being compared in the condition or what value the retests variable is being assigned.


            The script you provided is created from NinjaTrader 7.

            To convert this I recommend that you create a new script in NinjaTrader 8 using the Indicator Wizard and add any public inputs using the wizard.
            Then copy and paste the private variables in the scope of the class, then copy and paste the logic in OnBarUpdate().
            From there, correct any code breaking changes.


            Note, Historical is now State.Historical, DrawRectangle() is now Draw.Rectangle(), and FirstTickOfBar is now IsFirstTickOfBar.




            Note, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one on one educational support in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

            Through email or on the forum we are happy to answer any specific questions you may have about NinjaScript if you decide to code this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

            You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.​
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Many thanks NinjaTrader_ChelseaB for your reply.

              1) Regarding the conversion of the zip file. I even do not know what is in the script, since I could not import it. I thought there was a way at Ninjatrader to convert such file from Ninja 7 to Ninja 8.

              2) Regarding my main topic, the count of touches of the rectangle:
              NinjaTrader_ChelseaB, I do not have any problem with debugging. I have included in the Print() function since yesterday the bar datetime when the condition is met. I could even draw an arrow there.

              My problem is somewhere else, and I am failing to understand where and why.

              1) Why can I count the touches only when I am live during a session?
              2) If I switch off the computer, why I am losing all previous counts?
              3) Why the old counts are not taking into consideration when I switch on the computer?

              The Print function pups up ONLY the counts of the current session, not the old counts before the computer is switched on.

              Comment


                #8
                Hello Stanfillirenfro,

                It would not be something I could convert for you, but you can hire a consultant to convert it for you if you would like.

                The script can be imported and opened in NinjaTrader 7, or you could extract the file and open this with notepad or another text or code editor.


                With the screenshot you have provided I am seeing:

                Product8: 16
                Count8: 1
                Product8: 16
                Count8: 2
                etc

                I am not seeing the bar date and time (Time[0]) is in this output.

                There is not a way to see what bar the print is for. There is also no way to know what is being compared in the condition in this print.


                "Why can I count the touches only when I am live during a session"

                This may be due to the logic in the script. This is something that could be done in historical if the script is programmed to.

                To confirm, the script is drawing the rectangle in State.Historical and the return Rectangle object from the method call is being assigned to a variable?

                "If I switch off the computer, why I am losing all previous counts?"

                All private variables are instantiated when the script is enabled with the default value. The logic would have to assign the variables a value.
                If you wanted to store a value, this could be written to a text file and read from the text file and assigned to the variable when the script is enabled as previously suggested.


                "Why the old counts are not taking into consideration when I switch on the computer?"

                Possibly the value is not being read from a file, and was not calculated in the historical data with the logic written into the script.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Many thanks NinjaTrader_ChelseaB for your reply.

                  1) I have extracted the file and openedt it with editor. Many thanks for the avdice.

                  2) Regarding my rectangles, I will revert to you tomorrow when I will have a retest. I am not not sure there will be enough movement on the market until the end of this session so that I can have a zone retested.

                  Comment


                    #10
                    Hello NinjaTrader_ChelseaB!

                    Many thanks again for your help. I have attached a file showing the Time[0] and also the prices with which the rectangle is drawn.

                    As I went live today, the retests of the zone was zero. Minutes later, the prices came back to retested the level and on the output window, I could have the first three lines (with count3 = 1), count3 being the variable for this zone.
                    I went to the indicators panel and I turned my indicator to invisible. After a while (two minutes), I turned the indicator to visible. The indicator was in place, but the retests was turned to zero again. The prices came back again and retested the zone, so that I could have the three last lines on the output window. The count was 1, although this zone was previously tested, meaning that the previous test was just not taken into account.

                    I am really failing to understand the situation. I am not geting why the eprevious retests are not counted.

                    I would really appreciate any idea to fix this problem.
                    Attached Files

                    Comment


                      #11
                      Hello Stanfillirenfro,

                      Unfortunately, with the screenshot there are no comparison operators showing how the values in the condition are being compared.
                      I'm not sure what this output means.

                      Please provide the condition and / or calculation you are investigating and I can help assist you with making an informative print that includes comparison operators to show how the values in the condition are being compare.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello NinjaTrader_ChelseaB and thanks for your reply.

                        Below is the code I am using:

                        HTML Code:
                        if(Open[0] > endArY2 && Open[0] < startArY2 && Close[0] < endArY2)
                        {
                              Count2 += 1;    
                                
                              Print("Count2: " + Count2 + " Time " + ":  " + Time[0]);    
                              Print("startArY2" + " Size " + ":  " + startArY2 + " Time " + ":  " + Time[0]);
                              Print("endArY2" + " Size " + ":  " + endArY2 + " Time " + ":  " + Time[0]);
                        }​

                        Comment


                          #13
                          Hello Stanfillirenfro,

                          For this condition a print that prints all values with comparison operators would appear as:

                          Print(string.Format("{0} | Open[0]: {1} > endArY2: {2} && Open[0]: {1} < startArY2: {3} && Close[0]: {4} < endArY2: {2}", Time[0], Open[0], endArY2, startArY2, Close[0]));

                          I would also recommend including the State property to show if this is historical or realtime.

                          Print(string.Format("{0} | State: {1} | Open[0]: {2} > endArY2: {3} && Open[0]: {2} < startArY2: {4} && Close[0]: {5} < endArY2: {3}", Time[0], State, Open[0], endArY2, startArY2, Close[0]));​

                          However, is this condition you are investigating?
                          Were you not looking for why the 'retests' variable is not being set to a specific value?

                          Is this script designed to only update in realtime?
                          Is this screenshot showing the full output of the historical data?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello NinjaTrader_ChelseaB for your reply.

                            I have attached the screenshot of the Print() function.

                            However, is this condition you are investigating?
                            Yes it is the condition I am investigating.

                            Were you not looking for why the 'retests' variable is not being set to a specific value?
                            Because it is occuring already, but only if I do not refresh the indicator by making it invisible and after visible. My main problem being that the old touches before I go online are not counted.

                            Is this script designed to only update in realtime?
                            Absolutely not. The rectangles are drawn at all times, only their touches by the candles are counted weird.

                            Is this screenshot showing the full output of the historical data?
                            Unfortunately yes.
                            Attached Files

                            Comment


                              #15
                              Hello Stanfillirenfro,

                              To save the output from the output window to a text file right-click the output window, select Save as.

                              In the screenshot there are no prints that say State: Historical. There is only one print that says State: Realtime.

                              The logic is not evaluating in historical.

                              Do you have code to prevent the code from evaluating in historical? (like if (State == State.Historical) return; )

                              Where is the retests variable?
                              Is this in the code?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              580 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              335 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              102 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              554 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              552 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X