Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help Counting Bars

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

    Help Counting Bars

    Hi all,

    I have a question which I believe is simple.

    What is the best way to do the following.

    Get the High of the last bar High[1] and compare it to the High 2 bars ago High[2] and then if it's higher, store the value and begin counting how many bars this occurs for?

    if (High[1] > High[2])
    {
    // It's true, so store it and start counting
    }

    else
    {
    //reset counter?
    }

    Thanks,

    #2
    This was where I started and I'm getting all zeros:

    if (High[1] > High[2])
    {
    counter = counter++;
    }
    else
    {
    counter =
    0;
    }

    Print(
    "Counter " + counter.ToString());

    Comment


      #3
      r2kTrader,

      Print inside the if-statement to see if you ever increment at all.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Josh View Post
        r2kTrader,

        Print inside the if-statement to see if you ever increment at all.
        I would like the value accessible outside of the nest.

        Should I set a bool and then do my further testing within the nest and then set the bool?


        Thanks for the reply btw,

        Comment


          #5
          FYI, it's still Zeros Josh.

          I tried this to force an increment, but still zeros

          Under Initialize() I have
          int counter = 0;

          if (Position.MarketPosition == MarketPosition.Flat)
          {
          counter = counter++;
          Print(
          "Counter " + counter.ToString());

          }
          Last edited by r2kTrader; 06-05-2009, 11:08 AM.

          Comment


            #6
            You should not set its value in Initialize(). Please do it in the Variables region of your code as:

            private int counter = 0;
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by r2kTrader View Post
              FYI, it's still Zeros Josh.

              I tried this to force an increment, but still zeros

              Under Initialize() I have
              int counter = 0;

              if (Position.MarketPosition == MarketPosition.Flat)
              {
              counter = counter++;
              Print(
              "Counter " + counter.ToString());

              }
              Don't use counter = counter++
              Just
              counter++;

              Comment


                #8
                2 things.

                Josh, yes, that is what I meant, not under initialize. Under vars. Sorry about that.

                Roonius,

                Thank you for the reply.

                I found this to be more elegant:

                if (CountIf(delegate {return High[0] > High[1];}, 10) > 2)
                Print(
                "Yes");
                else
                Print("no");

                Comment


                  #9
                  Doh!

                  no so elegant. It doesn't track consecutively :-(

                  Back to counter++ without the counter = counter++;

                  Thanks!

                  UPDATE:
                  Actually, it works great!!!

                  The key was to setup my test value as a DataSeries (Yes Josh, I FINALLY UNDERSTAND HOW TO USE A DATASERIES!! I might be very dangerous now).

                  Anyway, instead of doing a counter with a loop, I used CountIf which proved to be more elegant. I just reduced the value to the total count I want and made it == the number I want to match. Works perfect.

                  DataSeries are so elegant and powerful, I'm now thinking of everything I can track. I'm scared of this new found power. It may be too much for one trader to have so much power.
                  Last edited by r2kTrader; 06-05-2009, 03:38 PM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by argusthome, Yesterday, 10:06 AM
                  0 responses
                  14 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  11 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  9 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post TheRealMorford  
                  Started by Mindset, 02-28-2026, 06:16 AM
                  0 responses
                  31 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Working...
                  X