I've printed Bars.Count & CurrentBar and they differ by 2-3 -- the relationship does not seem fixed.
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
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
Code: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 bortz, 11-06-2023, 08:04 AM
|
47 responses
1,602 views
0 likes
|
Last Post
by aligator
Today, 07:22 PM
|
||
Started by jaybedreamin, Today, 05:56 PM
|
0 responses
8 views
0 likes
|
Last Post
by jaybedreamin
Today, 05:56 PM
|
||
Started by DJ888, 04-16-2024, 06:09 PM
|
6 responses
18 views
0 likes
|
Last Post
by DJ888
Today, 05:12 PM
|
||
Started by Jon17, Today, 04:33 PM
|
0 responses
4 views
0 likes
|
Last Post
by Jon17
Today, 04:33 PM
|
||
Started by Javierw.ok, Today, 04:12 PM
|
0 responses
12 views
0 likes
|
Last Post
by Javierw.ok
Today, 04:12 PM
|
Comment