This function displays the number of bars visible on a chart.

Users have the functions GetAppInfo(AiHighestDispValue) and GetAppInfo(AiLowestDispValue) to determine the highest and lowest values that are currently displayed on a chart. These are useful for positioning custom text on the chart, and also to determine which indicators are close enough to the price action to warrant their display on the chart. With the highest and lowest values known, users can code the vertical position of text as a percentage of total vertical height, to maintain the relative position of the text as the price range on the chart varies.

Similar functions GetAppInfo(AiLeftDispDateTime) and GetAppInfo(AiRightDispDateTime) are available to determine the leftmost bar and the rightmost bar DateTime. However, these are more awkward for use in positioning text horizontally on a chart, since the position must be converted to an “offset” number of bars for the functions Text_New(Date(Offset), Time(Offset), Price, “Text To Display”) and Text_SetLocation(Text_ID, Date(Offset), Time(Offset), Price).

If the number of bars displayed on the chart is known, then the offset for the leftmost bar is also known. Once the offset of the leftmost bar is known the user could position text on a chart as a percentage of the horizontal time range as conveniently as the vertical position can now be specified. This would allow the text to maintain it’s relative horizontal position if the chart were resized or the density of its bar spacing were altered.

The indicator custom text displays the input parameters used to generate the Bollinger ribbons. The input parameters of this indicator have specified that this custom text should be positioned at the 50% horizontal time range of the chart. After function _BarsDisplayed calculates the number of bars displayed in the above chart, the horizontal midpoint bar offset is easily calculated so the custom text can be correctly displayed.

This text will automatically maintain a center position on the horizontal axis, regardless of the size of the chart or the bar spacing density.

Function Method

Function GetAppInfo(AiLeftDispDateTime) is called only once to determine the DateTime of the leftmost bar of the chart.

The function _BarsDisplayed is called from the indicator each time a new bar occurs. The current bar’s date and time is compared to the DateTime of the leftmost margin. When the current bar’s date and time are equal to or greater to DateTime, then the _BarsDisplayed function is incremented by 1 with each subsequent bar until LastBarOnChart is reached. At this point, the value of _BarsDisplayed will equal the total number of bars displayed in the chart window.

When LastBarOnChart is reached, the value of function _BarsDisplayed is used to calculate the offset needed for a chart label to be positioned at the percentage of the horizontal range specified by the user.

This improved method does not back reference bars all the way to the leftmost bar, as the original method does. Because this full backreferencing has been eliminated from the technique, the MaxBarsBack value calculated for the indicator is smaller. This means that the indicator will start plotting earlier without skipping over as many initial historical bars. It also means that a smaller quantity of historical data is required to permit the indicator to display properly.

A final benefit of the new method is that the function _BarsDisplayed starts counting up from zero starting with the leftmost displayed bar, all the way to the LastBarOnChart, at which time it will report the total number of bars displayed in the chart. This allows the function to be used to horizontally position text a specified number of bars from the leftmost bar without using any back referencing of bars. This is more efficient and results in the smallest MaxBarsBack value to be auto-calculated by the indicator.

Once the chart is loaded and a LastBarOnChart condition occurs, a loop walks toward the left from the rightmost bar until the DateTime of the loop’s target bar equals the leftmost bar DateTime, and counts the associated offset to the leftmost bar.

Although a binary search method would be faster, it is questionable whether the additional complexity would be warranted, as the _BarsDisplayed function is called once and only once when the chart is either loaded or refreshed.

This method works for all time frames: monthly, weekly, daily, intraday (minute) and tick bars. It produces the correct result regardless of short trading days, holidays, or session length. Its simplicity makes it easy for users to understand and also to modify for other purposes.

When there is an inadequate number of historical bars loaded into the chart, the indicator calling _BarsDisplayed may fail to display or the chart label may print in a horizontal position other than that intended by the input parameters of the indicator.



Initially posted version:  05/07/09

Latest Update:  07/04/10

*.ELD files are compiled for TS 8.6

All ELD and code text files packaged here:


Users of earlier versions fo TradeStation may compile the code
from the text files included in the above *.zip file.

The code may be visualized here:

Function BarsDisplayed

Indicator BarsDisplayed