NexusFi: Find Your Edge


Home Menu

 



How to NT8 Session Iterator with Trading Hours Templates

Using the built in session iterator combined with NinjaTrader's numerous Trading Hours templates, you can quickly filter data for more specific time periods. This can be very useful in indicators and strategies where you may want to exploit an edge during a certain time period as well as helping you avoid other market sessions.

For example, @FT71 does all of his market profile building based on tick data and the ES day session only. Based on the same idea, this combination allows us to do the same in NinjaTrader 8. To expand even further, NT8 allows us the quick and easy ability to create our own trading hour templates without touching a single line of code!

I created this quick video tutorial outlining the code needed to get up and running. In addition, I also cover the few "gotchas" when trying to add this functionality to your trading tools. Creation and usage code samples are below. All code (including the full Visual Studio solution) is freely available on my GitLab page.


Creating the session iterator:
 
Code
        protected override void OnStateChange()
{
base.OnStateChange();

switch (base.State)
{
case State.Undefined:
break;

case State.SetDefaults:
base.Name = $"JDT_{this.GetType().Name}";
base.Description = @"Simple indicator template to demo SessionIterator.";

base.Calculate = Calculate.OnEachTick;
base.IsSuspendedWhileInactive = true;
base.IsAutoScale = true;
base.IsOverlay = true;

// REQUIRED: setting this to false and this indicator used by a strategy, this indicator's OnBarUpdate will not fire properly
//base.IsVisible = true;

base.BarsRequiredToPlot = 0;

break;

case State.Configure:
// REQUIRED: necessary to add at least 1 plot so strategies using this indicator
// the OnBarUpdate will be called in this indicator.
base.AddPlot(System.Windows.Media.Brushes.Transparent, "touchPlot");

// add any data series necessary
//base.AddDataSeries(BarsPeriodType.Minute, 1);
break;

case State.Active:
break;

case State.DataLoaded:
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
if (this.Bars != null)
{
this.sessionIterator = new SessionIterator(this.Bars);
ClearOutputWindow();
}
else
{
// Heads up in the NinjaTrader Control Center that something went wrong.
NinjaScript.Log($"Bars object not populated in {base.State}.", Cbi.LogLevel.Error);
}

break;

case State.Historical:
break;

case State.Transition:
break;

case State.Realtime:
break;

case State.Terminated:
break;

default:
break;
}
}
Getting the next session information:
 
Code

protected override void OnBarUpdate()
{
base.OnBarUpdate();

if (base.CurrentBars[0] < base.BarsRequiredToPlot)
{
return;
}

if (base.IsFirstTickOfBar && base.Bars.IsFirstBarOfSession)
{
if (this.sessionIterator != null && this.sessionIterator.GetNextSession(base.Time[0], false))
{
this.sessionBeginDate = this.sessionIterator.ActualSessionBegin;
this.sessionEndDate = this.sessionIterator.ActualSessionEnd;

// let's print the new session info to the Output tab
// https://ninjatrader.com/support/helpGuides/nt8/?output.htm
base.Print($"Session begin: {sessionIterator.ActualSessionBegin}, end: {sessionIterator.ActualSessionEnd}");
}
}

// REQUIRED: necessary to assign any value so strategies using this indicator the
// OnBarUpdate will be called in this indicator.
base.Value[0] = double.NaN;
}
Happy building,

Jasonnator



© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top