I've printed Bars.Count & CurrentBar and they differ by 2-3 -- the relationship does not seem fixed.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
detect last bar?
Collapse
X
-
detect last bar?
Is there a way to run code ONLY on the very last bar on a chart?
I've printed Bars.Count & CurrentBar and they differ by 2-3 -- the relationship does not seem fixed.Tags: None
-
Well "if (Historical) return;" will stop the code from running on ALL the bars --
in cases where I am connected to a data feed but the session is closed or slow (no incoming ticks), there is no output on the very last bar either.
If that is the best I can get, let me know. But it makes it hard to run tests after trading hours or on week-ends -- I would like the final result to calculate at those times as well.
Any other suggestions?
Comment
-
Hello,
Well "if (Historical) return;" will stop the code from running on ALL the bars --
in cases where I am connected to a data feed but the session is closed or slow (no incoming ticks), there is no output on the very last bar either.
>>Yes, that is why I mentioned "do you consider the last bar to be the current bar", meaning the building bar. I see what you want now.
If that is the best I can get, let me know. But it makes it hard to run tests after trading hours or on week-ends -- I would like the final result to calculate at those times as well.
Any other suggestions?
>>I suggest comparing the current time with the time stamp of the last bar on the chart. If the current time is >= than the last bar time stamp you have the last bar on the chart. Try it and post your code and I'll help if needed.DenNinjaTrader Customer Service
Comment
-
Thanks, Ben.
I also tried using " if (CurrentBar >= Bars.Count-3) "
thinking if (CalculateOnBarClose = true
then the last bar would be Bars.Count-2;
or if (CalculateOnBarClose = false
then the last bar would be Bars.Count-1
but sometimes there was no output, until I changed it to 3. As I said the last bar # did not seem consistent even when the Bar count was unchanged.
I'll keep playing.
Comment
-
-
Here you go.
Please note that this trick only works with CalculateOnBarClose=true
if you find one with CalculateOnBarClose=false, I'm interested...
Code:if(Bars.LastBarOfSession && Time[0].Date.CompareTo(DateTime.Now.Date)==0)
Comment
-
SARdynamite, thanks for the code, I'll play with it. It is odd but I cannot find Bars.LastBarOfSession discussed in the help files.
Ben, when I want to know what a value is, I print it to the output window. In this case, it is very simple:
msg = "Statistics: Bar Count = " + Bars.Count;
msg += " & cb = " + CurrentBar + " Price = " + Close[0] ;
Print(Time[0] + " : " + msg);
The Bars.Count was always constant (after the session closed for the week-end) but on the last line printed, the CurrentBar tended to stop on slightly different numbers, when re-applying indicators for example.
Comment
-
This is it.
Initialize
OnBarUpdateCode:private DateTime myDateTime;
Code:[COLOR=#000000][COLOR=#007700]if (Bars.LastBarOfSession && [/COLOR][COLOR=#0000bb]myDateTime[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Date[/COLOR][COLOR=#007700]==[/COLOR][COLOR=#0000bb]DateTime[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Now[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Date[/COLOR][COLOR=#007700]) [/COLOR][/COLOR]
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
597 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
343 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 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
556 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
555 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment