Improved LastBarOnChart for BarType > 0 Charts

eKam has pointed out that the LastBarOnChart function does not work correctly on type 0 (tick) bars.

The problem is that the time function called by LastBarOnChart does not resolve time finer than 1 minute increments. Therefore, this function may become true up to 1 minute before the last bar prints on a tick chart since several tick bars may print with the same minute time stamp. The same problem occurs for multiple volume bars which may print with the same minute time stamp.

However, there is also a problem using LastBarOnChart with real-time data for all bar types > 0.

When the last bar on the chart has printed, LastBarOnChart does not become true until the next tick of data is received. The same can be said for function GetAppInfo(aiRealTimeCalc). Therefore GetAppInfo(aiRealTimeCalc) can not be used to trigger LastBarOnChart immediately when the last bar has printed on a real-time chart. Both LastBarOnChart and GetAppInfo(aiRealTimeCalc) must wait for the next tick of data to arrive before they are triggered.

If you are using LastBarOnChart to trigger the plotting of an indicator or performing a calculation, this action will be delayed until the next tick of data arrives. In some cases, this delay will be objectionably long if the symbol is trading infrequently.

The following code can be used to force an immediate trigger of the LastBarOnChart condition when the last bar has printed on a real-time data chart of BarType > 0:

if LastBarOnChart or BarStatus(1) = 1 then begin

{ code here executes immediately when last bar loads on real-time chart


An optimized version of this code can be found below as Function _LastBarOnChart. This code was modified from in-line code posted in the Wiki and also included below for comparison.

Note that the overhead involved with the GetAppInfo(aiRealTimeCalc) function is avoided.

Function _LastBarOnChart does not resolve the issue with LastBarOnChart not working with type 0 charts, however, it improves it's performance with real-time data charts of BarType > 0.




Initial posted version: 01/20/10

Latest Update: 01/20/10

*.ELD files are compiled for TS 8.7.


All ELD and code text files packaged here:


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

The code may be visualized here:

Wiki Inline Code for LastBarOnChart

Function LastBarOnChart

Indicator LastBarOnChart




Bookmark and Share