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
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
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,
Code: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 cre8able, Yesterday, 01:16 PM
|
3 responses
11 views
0 likes
|
Last Post
by cre8able
Today, 08:28 AM
|
||
Started by ChartTourist, Today, 08:22 AM
|
0 responses
6 views
0 likes
|
Last Post
by ChartTourist
Today, 08:22 AM
|
||
Started by LiamTwine, Today, 08:10 AM
|
0 responses
2 views
0 likes
|
Last Post
by LiamTwine
Today, 08:10 AM
|
||
Started by Balage0922, Today, 07:38 AM
|
0 responses
5 views
0 likes
|
Last Post
by Balage0922
Today, 07:38 AM
|
||
Started by JoMoon2024, Today, 06:56 AM
|
0 responses
6 views
0 likes
|
Last Post
by JoMoon2024
Today, 06:56 AM
|
Comment