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

Reretting counting variable to zero

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

    Reretting counting variable to zero

    Hi guys

    I've tried everything here - looked all around Help and the Forum - and not for the first time I'm still stuck! So any assistance much appreciated.

    Following great advice I've had previously, I've got a strategy to count the number of occurrences of certain conditions. It all works great except for the essential at the end.

    This is the essentials of the coding:

    Code:
    private int risingCount = 0;
    
    ...
    
    if ( [I]conditions [/I])
                {
                    RisingCount++;
                    Print(RisingCount);
                }
    Question 1):

    This works and prints a list of the increasing numbers to the Output window. But next time I run the strategy, the numbering in the window resumes where it left off. What I'd like to happen of course is that, every time it is run, RisingCount is reset to zero.

    (Obviously, "private int risingCount = 0;" in Variables doesn't do the trick.)

    Question 2):

    What I'd really like is for there to be just one number representing the total number of occurrences. I've tried (with the first Print(RisingCount) commented out) :

    Code:
    if (!Historical)
    {
    Print(RisingCount);
    }
    but nothing is printed.

    Any suggestions greatly appreciated.

    #2
    Originally posted by arbuthnot View Post
    Hi guys

    I've tried everything here - looked all around Help and the Forum - and not for the first time I'm still stuck! So any assistance much appreciated.

    Following great advice I've had previously, I've got a strategy to count the number of occurrences of certain conditions. It all works great except for the essential at the end.

    This is the essentials of the coding:

    Code:
    private int risingCount = 0;
     
    ...
     
    if ( [I]conditions [/I])
                {
                    RisingCount++;
                    Print(RisingCount);
                }
    Question 1):

    This works and prints a list of the increasing numbers to the Output window. But next time I run the strategy, the numbering in the window resumes where it left off. What I'd like to happen of course is that, every time it is run, RisingCount is reset to zero.

    (Obviously, "private int risingCount = 0;" in Variables doesn't do the trick.)

    Question 2):

    What I'd really like is for there to be just one number representing the total number of occurrences. I've tried (with the first Print(RisingCount) commented out) :

    Code:
    if (!Historical)
    {
    Print(RisingCount);
    }
    but nothing is printed.

    Any suggestions greatly appreciated.
    Your variable is set as risingCount and yet in your conditional you refer to RisingCount. Is this a typo here or in your code? Also, there are issues with using HIstorical. The first option with risingCount seems betetr choice from my perspective.

    Comment


      #3
      Hello arbuthnot,
      To clarify further by "But next time I run the strategy" do you mean,
      1. Are you re-initializing the strategy or
      2. Your condition is turning false and you want to reset the variable value to 0 (zero) again.
      JoydeepNinjaTrader Customer Service

      Comment


        #4
        Thanks for getting back to me on this, Joydeep.

        I think the answer is that I'm re-initializing the strategy (by pressing F5).

        What happens is that, when I first run it, I get a set of numbers in the Output running from 0 to n, say. Then I press F5 when I'm on the chart (and I assume this is what is meant by 're-initializing) and the next set of numbers in Output are roughly (n+1) to a higher number.

        The questions are: how can the variable to be totally reset to zero? And how can I make only one number (the end of the list) appear?

        A reason to doing the F5 (or re-initializing, I guess) is that I can change the size of the range bars and get an independent result for that.

        Thanks again for your further advice.

        Comment


          #5
          Your variable is set as risingCount and yet in your conditional you refer to RisingCount. Is this a typo here or in your code? Also, there are issues with using HIstorical. The first option with risingCount seems betetr choice from my perspective.
          Thanks very much for your suggestion, Zeos.

          I had thought that all variables must be entered in 'Variables' with a lower-case first letter, then in 'OnBarUpdate', the variable must be referenced with an upper-case letter. I tried changing the name as you suggest but the same problem arose.

          Much obliged for your time

          Comment


            #6
            Hello arbuthnot,
            Thanks for the information.

            If you reinitialize the strategy (by pressing F5) then the variable will lose the value unless you store the value somewhere (say write it in a xml file, or a txt file etc) and read it again when you reapply/reinitialize it.

            You can refer to this sample code which demonstrates how to read and write a txt file
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Thanks, Joydeep. for the information and the link. I've downloaded that SampleFileReadWrite file. It'll take some study, obviously, to master the concepts therein.

              Could you very kindly clarify, if possible, a couple of points:

              You say:

              If you reinitialize the strategy (by pressing F5) then the variable will lose the value unless you store the value somewhere
              a) The problem, as in see it at the moment, is that the value is not being lost, if you see what I mean. Putting aside SampleFileReadWrite for a second, am I right in assuming there's no way of resetting to the variable in question to zero?

              b) In SampleFileReadWrite, there's this section of code:

              Code:
              if (Historical)
                              return;
              In my original post, I quoted a section from my own strategy:

              Code:
              if (!Historical)
              {
              Print(RisingCount);
              }
              and I'm wondering why this doesn't work, giving the latest returned number.

              I'll be very happy to read any further thoughts you may have.

              Comment


                #8
                The lower/upper case issue you cite applies to Properties. In the case of properties the lower case represents the backer field for the public property which is designatd with the upper case. Furthermore, how are you setting the value of risingCount? It appears that it is acting as a global variable and retaining its previous value without being reset to zero. Try initializing the variable in the OnStartUp() section. Declare the variable as private in risingCount; and then initialize its value in OnStartUp().
                Last edited by Zeos6; 01-21-2013, 03:52 PM.

                Comment


                  #9
                  Hello arbuthnot,
                  Ok, I got your point.

                  Please set the below code in the Initialize section of the code to reset the value of the variable to 0.

                  Code:
                  protected override void Initialize()
                  {
                     RisingCount = 0;
                     //rest of the code
                  }
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    Much obliged, Joydeep, but, unfortunately, putting RisingCount = 0; in Initialize() still doesn't make the variable reset...

                    Maybe this reflects seem deep fault, shortcoming or glitch within NT.

                    Unless you have any further ideas, Joydeep, I may put this aside for a moment and/or try to think of a completely different approach.

                    Thanks again.

                    Comment


                      #11
                      Check my response below. It may need to go into the OnStartUp() section. However, there likely is no glitch in NT in this area. It is likely something you are doing in your code. If you show us the code we may be able to help you pinpoint it.

                      Comment


                        #12
                        Thanks very much again, Zeos. Your suggestion seems very promising.

                        I've never used the 'OnStartUp()' section before, though I've seen it in various scripts I've downloaded.

                        This will be new territory for me in every sense...

                        I'll have a go at this tomorrow or soon after if I can as it's getting late here in the UK. I'll report back to this thread if I do make any progress!

                        Again, your ideas are much appreciated.

                        (This posted before I saw Zeos's post just below, for which thanks. I'll try and post some code. Much obliged.)
                        Last edited by arbuthnot; 01-21-2013, 04:28 PM.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by CortexZenUSA, Today, 12:53 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post CortexZenUSA  
                        Started by CortexZenUSA, Today, 12:46 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post CortexZenUSA  
                        Started by usazencortex, Today, 12:43 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post usazencortex  
                        Started by sidlercom80, 10-28-2023, 08:49 AM
                        168 responses
                        2,266 views
                        0 likes
                        Last Post sidlercom80  
                        Started by Barry Milan, Yesterday, 10:35 PM
                        3 responses
                        13 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Working...
                        X