Counting Bars Within a Specified Time Range in ProBuilder

08 Nov 2022
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to count the number of bars on a chart that occur within a specified time range and display this count on the chart. The example focuses on counting bars between 09:00:00 and 23:00:00.


There you go:
Once Tally = 0
If OpenTime = 090000 Then Tally = 0
Endif
If (OpenTime >= 090000) And (OpenTime <= 230000) Then
    Tally = Tally + 1
    drawtext("#Tally#",barindex,low - range)
Endif
return

Explanation of the Code:

  • Initialization: The Once keyword is used to set the variable Tally to 0 when the script is first executed. This ensures that the count starts from zero each time the chart or indicator is loaded.
  • Reset Condition: The script checks if the OpenTime of the bar is exactly 09:00:00. If true, it resets Tally to 0. This line seems redundant given the initial setup and could be intended for specific scenarios not detailed here.
  • Counting within Time Range: The main conditional statement checks if the OpenTime of the bar is between 09:00:00 and 23:00:00, inclusive. If the condition is met, it increments the Tally by 1.
  • Displaying the Count: The drawtext function is used to display the current value of Tally on the chart. It positions the text at the barindex (current bar's index) and just below the lowest price of the bar by subtracting the range (difference between high and low prices of the bar).

This snippet is useful for traders or analysts who want to track the number of bars during specific trading hours or any other activities that are time-bound on their charts.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/bar-counting-to-help-analysing/#post-180696

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
33
indicator
132
strategy
171

Recent Snippets

How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...