namespace NinjaTrader.NinjaScript.Indicators
{
public class RangePM : Indicator
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "RangePM";
Calculate = Calculate.OnBarClose;
IsSuspendedWhileInactive = true;
IsOverlay = false;
AddPlot(Brushes.Goldenrod, "RangePM");
}
}
protected override void OnBarUpdate()
{
DateTime currentBarTime = Bars.GetTime(0);
DateTime previousBarTime = Bars.GetTime(1);
double currentBarDuration = (currentBarTime - previousBarTime).TotalSeconds;
Values[0][0] = currentBarDuration;
}
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Time elapsed per bar for Range bars
Collapse
X
-
Time elapsed per bar for Range bars
I am trying to develop an indicator that will show how long it has taken each bar to fully draw. It seems pretty straight forward but the code below isn't working for me. The result on the 25 Range is one value (-0.1) for every bar. On a 50 Range the value is -.832. Where am I going wrong?
Code:Tags: None
-
You are using GetTime() incorrectly.
An 'absolute bar' index is not the same as a 'bars ago' index.
It's a matter of the '0th' index starting at the far left of the
chart vs the far right of the chart.
GetTime(0) is the very first bar of the chart, on the far left.
GetTime(1) is the second bar of the chart.
Your code is wrong.
You're wanting the most recently closed bar and the prior bar,
these bars are on the far right of the chart.
Try this,
double currentBarDuration = (Time[0] - Time[1]).TotalSeconds;
Make sense?
PS:
Many times you'll find 'bars ago' indexes used with square brackets,
because these are usually indexes for a data series.
But 'absolute bar' indexes tend to be with parentheses, because these
are passed as arguments into functions.
Knowing the difference between the two is absolutely critical.
Last edited by bltdavid; 10-04-2023, 12:40 PM.
-
And you'll need to add the guard code,
to the the top of OnBarUpdate, to protectCode:if (CurrentBar < 1) return;
against accessing Time[1] when this value
doesn't exist.
Huh?
On the very first bar of the chart, when that
very first bar closes, there is no previous bar yet.
And so, on the very first bar, accessing Time[1]
generates an error -- because, duh, there is no
previous bar on the very first bar, ie, there is no
previous slot at the '1' index on the very first bar.
Comment
-
Hello Fbraun378,
DateTime.Subtract will return a timespan if you are not wanting a number of seconds.
Returns the value that results from subtracting the specified time or duration from the value of this instance.
This timespan can use the datetime format strings.
Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.
Print(Time[0].Subtract(Time[1]).ToString("H:mm:ss"));Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
636 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
366 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
107 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
568 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
571 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment