Welcome to NexusFi: the best trading community on the planet, with over 200,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- discounts are available after registering.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 158 since Dec 2014
Thanks Given: 40
Thanks Received: 166
I've been getting several questions about the article I created here and the video I posted below. I didn't quite realize that the article section doesn't really allow for discussion so I've decided to open this thread where people can ask their questions here therefore everyone can benefit.
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 158 since Dec 2014
Thanks Given: 40
Thanks Received: 166
The main question so far was essentially, "what is the use case for this?"
I'll also try to answer it in another way, with a question. What is the fastest for loop you can write?....The one which hardly executes! That is why I used this custom method IsInSession which is simply checking the time you give it against the session iterator's begin and end DateTime properties. The for loop within that block is simply an example of why you may want to only do very expensive/intensive calculations during select times.
Where this really begins to pay dividends is when you need to check something every tick AND you're trying to scale up and maybe scan the entire SP500. If you had some sort of reversion to the mean strategy and were scanning 500 instruments, you could save yourself a ton of calculations when you're not in session.
Another example could be computing statistics like standard deviation, kurtosis, skewness, etc on the previous week of tick data (about 3 million data points in the ES). That's something you only want to do once at the beginning of the session. A use case like that could get plugged right in to the GetNextSession block.
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 158 since Dec 2014
Thanks Given: 40
Thanks Received: 166
Here is another example of how I use the session iterator. This example is fairly trivial compared to, say, computing statistics on a lot of historical data which you'd use for each session. That is actually what I want to show in my next example because there is a much more significant delay in processing (due to data fetching) compared to resetting 3 values like I've done with the mid price.
I'd like to submit this case and would like to know if there is a better way to do this:
Let's pretend the script runs on a chart that uses the "CME US Index Futures ETH" trading hours. On the following session, we want to read the last bar high displayed at the end of this session "CME US Index Futures RTH".
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 158 since Dec 2014
Thanks Given: 40
Thanks Received: 166
I did figure out a way to get historical information based on any trading hours you want. I don't think that would quite do what you're saying though.
I'd have to check but I think you can only change the trading hours in state configure or set defaults. I don't think you could do it once you're receiving real time data.
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 158 since Dec 2014
Thanks Given: 40
Thanks Received: 166
If you're using the built in NinjaScript editor instead of Visual Studio and the 3rd party Dll approach, it can't use "f" strings (the $ sign). Just switch to the older string.Format("your message {0}", yourVariable); and it should work fine.
If you're getting a runtime error, post and I'll take a look.
If you're using the built in NinjaScript editor instead of Visual Studio and the 3rd party Dll approach, it can't use "f" strings (the $ sign). Just switch to the older string.Format("your message {0}", yourVariable); and it should work fine.
If you're getting a runtime error, post and I'll take a look.
Thanks. That is what i did, i.e., using the string.Format("your message {0}", yourVariable); just was wondering if i needed to turn on a preference in the editor to make it work. So far, i have not install Visual Studio. Just using the NT editor. That's in my plan to install it though. I just begun to work with Ninjascripting in the last month so although i have been around for quite some time i never felt the need to code as i am a discretionary trader and just could not see how i could teach Ninja what i can do so easily with my brain.
Also, i did switch to NT8 recently and since some of the tools i were using on NT7 are not available, i felt the urge to learn Ninjascript.
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 158 since Dec 2014
Thanks Given: 40
Thanks Received: 166
I have updated this with essentially a wrapper which allows more predictable behavior. In certain bar types, NT8 internally calls NinjaTrader.Data.Bars.RemoveLastBar method. In certain situations, this caused the NT8 built in version to indicate there was a new session numerous times on the first bar even with the appropriate checks in place.
I also added a feature which allows you to pass any void method to the constructor which will be called whenever there is a new session.
Here is the updated class which I now use instead of the built in SessionIterator: SafeSessionIterator.cs
For those interested, here is the feedback I received from NT support when I tried describing this behavior. I wasn't able to convey (I guess) the problem but in their defense, it is subtle and not easy to spot.
In certain bar types, NT8 internally calls NinjaTrader.Data.Bars.RemoveLastBar method. In certain situations, this caused the NT8 built in version to indicate there was a new session numerous times on the first bar even with the appropriate checks in place.
Solid find. The RemoveLastBar interaction is one of those subtle bugs that can silently break session-based logic -- things like resetting cumulative volume, daily P&L tracking, or clearing indicator state at session open.
The core issue: Bars.IsFirstBarOfSession is designed to fire once per session, but when NT8 internally removes and re-adds a bar (most common with non-time bar types like Renko or Range), the flag can reset and re-fire. If your strategy uses that event to initialize session state, you end up initializing multiple times -- which can corrupt running totals or trigger unexpected order logic.
Your wrapper is the right approach. A few notes for anyone using it:
The void method callback in the constructor is a clean design -- much better than scattered if (IsFirstBarOfSession) checks throughout your code
Non-time bars are the main risk area -- Renko, Range, and custom bar types are where RemoveLastBar surfaces most often
On Calculate.OnEachTick strategies, pairing this wrapper with IsFirstTickOfBar adds another layer of protection
The NT support experience you described is familiar territory for anyone who has hit NT8 internal bar management edge cases -- they are genuinely hard to isolate in a minimal reproduction. Good instinct sharing the complete wrapper rather than just noting the workaround.
TGIF! Have a good weekend!
-- Fi
"The bugs that bite hardest are the ones that only appear once a day, right at session open."
Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.
Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.