Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Count Number of Ticks Per Bar

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

    Count Number of Ticks Per Bar

    I would like to be able to count the number of ticks in each bar. Like volume but instead of contracts the total number of ticks (excluding any volume). I would like this to work historically as well as real time.

    I have an indicator "BarTimes" attached which measures the time of each bar. (I use a lot of range and renko charts so time is helpful).

    Wondering if anybody can assist with changing the code from measuring time to counting ticks.

    This is the line of code I think I would be changing.

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;
    if (Bars.IsFirstBarOfSession)
    {
    BarTime[0] = 0;
    }
    else
    {
    BarTime[0] = (Time[0] - Time[1]).TotalSeconds;
    }
    }

    Thank you for any help
    Attached Files

    #2
    Hello sdauteuil,

    Thanks for your post.

    To get the historical ticks, you would need to enable Tick replay for the charts data series as historically you would only get 1 tick per bar without tick replay.

    You would need to create an integer counter variable to accumulate the ticks and use IsFirstTickOfBar to trigger resetting the count for the new bar, Something as simple as this:

    if (IsFirstTickOfBar)
    {
    tick_count =0; // reset counter on new bar
    }
    tick_count++; // increment counter

    BarTimes[0] = tick_count; // plot the ticks


    You could delete all of the other code related to time as it would not be needed. You would not need the IsFirstBarOfsession coding section.




    Comment


      #3
      Unfortunately when reviewing the tick replay help guide it says that it will not function with Renko bars which is what I am using.

      The system bar types "Line Break" and "Renko" cannot be used with Tick Replay

      Comment


        #4
        I also need it for minute bars so I will see if I can get those to work and post if I have any other questions.

        Comment


          #5
          Hello sdauteuil,

          Thanks for your reply.

          Yes, you are correct.

          Renko bars also support RemoveLastBar which means it will call IsFirstTickOfBar 3 times per bar in the process of drawing the final bar form.

          What you want will work for Range bars but not for Renko bars.

          Comment


            #6
            Where do I define the variable tick_count?

            I keep getting an error message "the name tick_count does not exist in the current context"

            Comment


              #7
              I was able to define it in the OnBarUpdate() section.

              I was trying to define as an int which was throwing an error message when I defined it as a long it was able to compile.

              Is the declaration below correct?


              protected override void OnBarUpdate()

              {
              long tick_count = 0;

              // remainder of code

              }

              thank you

              Comment


                #8
                I was able to get everything to work.

                appreciate your time!

                Comment


                  #9
                  Hello sdauteuil,

                  Thanks for your replies.

                  Your solution works,

                  To answer your original question, out of habit I define my variables at the class level.

                  I should have specified that but again what you are doing works.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  648 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  369 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  108 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  572 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  573 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X