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 inanazsocial, Today, 01:15 AM
|
1 response
6 views
0 likes
|
Last Post Today, 02:18 AM | ||
Started by rocketman7, Today, 02:12 AM
|
0 responses
10 views
0 likes
|
Last Post
by rocketman7
Today, 02:12 AM
|
||
Started by dustydbayer, Today, 01:59 AM
|
0 responses
1 view
0 likes
|
Last Post
by dustydbayer
Today, 01:59 AM
|
||
Started by trilliantrader, 04-18-2024, 08:16 AM
|
5 responses
23 views
0 likes
|
Last Post Today, 12:32 AM | ||
Started by Davidtowleii, Today, 12:15 AM
|
0 responses
3 views
0 likes
|
Last Post
by Davidtowleii
Today, 12:15 AM
|
Comment