Time between two points

Hello!

I would like to create a chart tool to check calendar time and bars between two points in a chart.

Can you see what is wrong here?

SetChartOptions(0,chartShowDates,ChartgridMiddle ); 
Plot( Close, "C", colorBlack, styleBar); 

bi = BarIndex(); 
d1900 = DaysSince1900(); 

bars = EndValue( bi ) - BeginValue( bi ); 
Days = EndValue( d1900 ) - BeginValue( d1900 ); 

Title = "Bars in selected range: "+bars +"\nDays in selected range: "+Days;

What is it that you expect to happen and it isn't ?

I ran the code and i seem to get the right values on the Daily chart.

For this to work, you have to use the Range Selector, ie. F12 and Shift+F12 ( Green and Red Range lines.) or by double-clicking.

image

You have not specified what Chart interval, but if you want time, what time, Hours, mins or secs ?
you could multiply with the respective units.

Another way is to use DateTimeDiff() and use the DateTime instead, it will return value in Secs that you can convert to your desired time.

bi = BarIndex(); 
d1900 = DateTime(); 

bars = EndValue( bi ) - BeginValue( bi ); 
Secs = DateTimeDiff(  EndValue( d1900 ), BeginValue( d1900 )); 

Title = "Bars in selected range: "+bars +"\nSecs in selected range: "+Secs;

image
Daily Chart.

1 Like

@jopa the number of Bars (though not calendar time) between two points has been reviewed with multiple code solutions here,

2 Likes