Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Calculate time in seconds between the close of two bars
Collapse
X
-
imported post
I added the statement below but when I do PRINT stops working (nothing prints). If I comment outthe statement you suggested PRINT works okay.
seconds = Time[0].Subtract(Time[1]).Seconds;
Print("Bar Time " + CurrentBar.ToString("00000"));
I also tried using just the PRINT statement and nothing prints.
Print(Time[0].Subtract(Time[1]).Seconds.ToString());
Comment
-
imported post
When something does not work always check the Log tab to see what errors may have been generated.
I suspect the issue is that Time[1] is throwing an exception since on the 1st bar, Time[1 bar ago] does not yet exist. Therefore, try something like:
if (CurrentBar > 0)
Print(Time[0].Subtract(Time[1]).Seconds.ToString());
Ray
RayNinjaTrader Customer Service
Comment
-
imported post
That was it. Thanks for your help.
I notice that by adding the following statement that some bars exceed 60 seconds and therefore have a value in the minutes variable. Is there a way to return the "total elapsed seconds" including minutes and seconds?
Time[0].Subtract(Time[1]).Minutes.ToString() + " " +
Comment
-
imported post
Under variables:
WithinInitialize()private DataSeries barInterval = null;
Within OnBarUpdate()barInterval = new DataSeries(this);
barInterval.Set(CurrentBar > 0 ?(double) Time[0].Second - Time[1].Second : 0);
if (Time[0] > OrderTime.AddSeconds((int) SMA(barInterval, 5)[0])
// Do something....
RayNinjaTrader Customer Service
Comment
-
turning this into a histogram?
Still learning ninjascript but how would I turn this into a simple histogram?Originally posted by OnePutt View PostI added the statement below but when I do PRINT stops working (nothing prints). If I comment outthe statement you suggested PRINT works okay.
seconds = Time[0].Subtract(Time[1]).Seconds;
Print("Bar Time " + CurrentBar.ToString("00000"));
I also tried using just the PRINT statement and nothing prints.
Print(Time[0].Subtract(Time[1]).Seconds.ToString());
So far I have this in void Initialize()
Add(new Plot(new Pen(Color.Red, 3), PlotStyle.Bar, "seconds"));
And this in void OnBarUpdate()
Value.Set(Time[0].Subtract(Time[1]).Seconds);
It compiles ok but nothing displays on the chart.
Comment
-
I suggest you check the Control Center logs for errors when you run it. I suspect you may run into an index error outlined in this tip: http://www.ninjatrader-support.com/v...ead.php?t=3170Josh P.NinjaTrader Customer Service
Comment
-
Thanks Josh. That did it.Originally posted by Josh View PostI suggest you check the Control Center logs for errors when you run it. I suspect you may run into an index error outlined in this tip: http://www.ninjatrader-support.com/v...ead.php?t=3170
if (CurrentBar < 1)
return;
if (CurrentBar >= 1)
Value.Set(Time[0].Subtract(Time[1]).Seconds);
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by CarlTrading, 03-31-2026, 09:41 PM
|
1 response
152 views
1 like
|
Last Post
|
||
|
Started by CarlTrading, 04-01-2026, 02:41 AM
|
0 responses
87 views
1 like
|
Last Post
by CarlTrading
04-01-2026, 02:41 AM
|
||
|
Started by CaptainJack, 03-31-2026, 11:44 PM
|
0 responses
131 views
2 likes
|
Last Post
by CaptainJack
03-31-2026, 11:44 PM
|
||
|
Started by CarlTrading, 03-30-2026, 11:51 AM
|
0 responses
127 views
1 like
|
Last Post
by CarlTrading
03-30-2026, 11:51 AM
|
||
|
Started by CarlTrading, 03-30-2026, 11:48 AM
|
0 responses
106 views
0 likes
|
Last Post
by CarlTrading
03-30-2026, 11:48 AM
|

Comment