Indicator Default Settings for NinjaTrader 8
Efficient use of NT8 indicators with large parameter sets in NinjaScript

In the dynamic world of futures trading, NinjaTrader's NinjaScript allows for easy integration of existing or third-party indicators into your trading strategies. Our Indicator Manager add-on significantly simplifies the process of repeatedly implementing parameter-rich indicators in your trading strategies.

Understanding the Challenge - Indicators with Large Parameter Sets

Often, the process of writing and refining trading strategies involves a preferred set of indicators which sometimes come with a plethora of parameters. This makes it notoriously cumbersome to use when coding a strategy. The best way to add such indicators is typically through the strategy wizard, which ensures the parameters are properly set to instantiate the indicator. 

That process involves Opening the Strategy Builder, creating a new strategy, clicking through the Wizard, adding the indicator etc. etc. The resulting code to instantiate the indicator then looks as follows:

PriceActionSwing pas;
pas = PriceActionSwing( Close, SwingStyle.Standard, 7, 20, false,                         SwingLengthStyle.Ticks, SwingDurationStyle.Bars, true,                         false, false, SwingTimeStyle.False,                         SwingBase.SwingVolumeStyle.Absolute,                         VisualizationStyle.Dots_ZigZag,                         new SimpleFont("Courier", 15) {Bold = false, Italic = false},                         15, 30, 45, 60, 75, 90,                         NinjaTrader.Gui.DashStyleHelper.Solid, 3, true, true
                        );

The IndicatorMgr class simplifies this by establishing templates for commonly used indicators which can then be called easily through a static method such as the below without having to supply all indicator parameters each time:

private ATR atr;

if(atr == null) {
    atr = IndicatorMgr.GetIndiATR(this, Close);
}

Print("ATR of the last bar was: " + atr[0].ToString());

This not only saves time in your strategy development, but also reduces potential errors in coding.

The Indicator Manager Codebase

A rather simple utility class does the job of providing an extendable framework for your custom indicators to be added later. 

The IndicatorMgr base class:

using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.Indicators.ScaleInTrading;
using PriceActionSwingBase;
using System;

namespace NinjaTrader.Custom.AddOns.ScaleInTrading
{
    internal class IndicatorMgr
    {       
        internal static void AddIndicatorToBase(NinjaScriptBase n, Indicator indi)
        {
            try
            {
                indi.SetState(State.Configure);
            }
            catch (Exception exp)
            {
                Cbi.Log.Process(typeof(Resource), "CbiUnableToCreateInstance2", new object[] { indi.Name, exp.InnerException != null ? exp.InnerException.ToString() : exp.ToString() }, Cbi.LogLevel.Error, Cbi.LogCategories.Default);
                indi.SetState(State.Finalized);
            }

            indi.Parent = n;
            //pas.SetInput(series);

            lock (n.NinjaScripts)
                n.NinjaScripts.Add(indi);

            try
            {
                indi.SetState(n.State);
            }
            catch (Exception exp)
            {
                Cbi.Log.Process(typeof(Resource), "CbiUnableToCreateInstance2", new object[] { indi.Name, exp.InnerException != null ? exp.InnerException.ToString() : exp.ToString() }, Cbi.LogLevel.Error, Cbi.LogCategories.Default);
                indi.SetState(State.Finalized);
            }
        }
        
        //Indicator Template: Here for the ATR which automatically sets the lookback period         internal static ATR GetIndiAtr(NinjaScriptBase n, ISeries<double> series, int lookbackPeriod = 5) { var indi = new ATR(); indi.Period = lookbackPeriod; indi.SetInput(series);
    
            AddIndicatorToBase(n, indi)
return indi;
}
    }    
  } 

Doing this for the ATR indicator that ships with NT is ok to demo the concept, but it doesn't really represent the type of indicator this was built for. Hence here is another example for the PriceActionSwing indicator which more clearly shows the benefits. 

internal static PriceActionSwing GetIndiPAS(ISeries<double> series)
{
    var pas = new PriceActionSwing();
    pas.SwingType = SwingStyle.Standard;
    pas.SwingSize = 1;
    pas.DtbStrength = 1;
    pas.UseCloseValues = useCloseValues;
    pas.SwingLengthType = PriceActionSwingBase.SwingLengthStyle.False;
    pas.SwingDurationType = SwingDurationStyle.False;
    pas.ShowSwingPrice = false;
    pas.ShowSwingLabel = false;
    pas.ShowSwingPercent = false;
    pas.SwingTimeType = SwingTimeStyle.False;
    pas.SwingVolumeType = SwingVolumeStyle.False;
    pas.VisualizationType = PriceActionSwingBase.VisualizationStyle.Dots_ZigZag;
    pas.TextFont = new SimpleFont("Courier", 15) { Bold = false, Italic = false };
    pas.TextOffsetLength = 15;
    pas.TextOffsetVolume = 30;
    pas.TextOffsetPrice = 45;
    pas.TextOffsetLabel = 60;
    pas.TextOffsetTime = 75;
    pas.TextOffsetPercent = 90;
    pas.ZigZagStyle = NinjaTrader.Gui.DashStyleHelper.Solid;
    pas.ZigZagWidth = 3;
    pas.IgnoreInsideBars = true;
    pas.UseBreakouts = true;
    pas.Calculate = Calculate.OnPriceChange;
    //pas.Parent = n;
    pas.SetInput(series);

    AddIndicatorToBase(n, pas)

    return pas;
}

You would define the above once only and then next time you want to use the PAS indicator with your default settings just call:

private PriceActionSwing pas;
if(pas == null) {
    pas = IndicatorMgr.GetIndiPAS(this, Close); 
}

Extending the Concept 

As you can see the above approach hugely simplifies the process of calling indicators with a large parameter set. Now typically out of the 25 input parameters there are the odd 2 which you still want to be able to adjust when instantiating the indicator. No worries. Check out the Method definition of the GetIndiATR Method above, it reads:

internal static ATR GetIndiAtr((...), int lookbackPeriod = 5)         

For the ATR indicator you would typically provide a lookback period, so we make this available as an optional parameter in the method definition. That way we can supply a value when getting our indicator, but we don't have to. Your can use the same approach for all other parameters where it is required.

How to install

Getting the above set up in your Ninjatrader 8 install is fairly straight forward. Create a new add-on in the addon folder (C:\Users\<username>\Documents\NinjaTrader 8\bin\Custom\AddOns) of your NT8 install. Copy the code from the IndicatorMgr Class from above and save the addon. Confirm the security pop-up. Then use within your favorite indicator as already outlined above:

private ATR atr;
if(atr == null) {
    atr = IndicatorMgr.GetIndiATR(this, Close); 
}

Print("ATR of the last bar was: " + atr[0].ToString());

In Summary - Key Features

So let's summarize why this might be a handy utility to use:

  • Efficient Indicator Integration: Add indicators efficiently to NinjaScriptBase objects (Indicators / Strategies) with minimal coding.

  • Customizable Indicator Parameters: Easily set standard parameters but allow for customizable parameters for the indicator, tailoring it to specific trading needs.

  • Time saver: Whether you're a hobbyist or a professional trader, IndicatorMgr allows for fast development of scalable and sophisticated trading strategies. 

  • If you're already familiar with NinjaScript, this utility class fits seamlessly into your existing workflows, enhancing your development process.

Conclusion: A Step Forward in Trading Automation

If you would like to discuss any of the above, don't hesitate to reach out to us.

Keywords: NinjaTrader, NinjaScript, ScaleInTrading, Indicators, Trading Strategies, Futures Trading, Automated Trading, C# Development, Trading Automation, Efficiency in Trading.
Sign in to leave a comment
TDD and Unit Testing with NT8
The definite Guide to Test Driven Development with NinjaTrader 8