Bollinger Bands in NinjaTrader — NinjaScript (C#) Source

A three-plot Bollinger indicator for NinjaTrader 8

Bollinger Bands in NinjaTrader
Bollinger Bands in NinjaTrader

Bollinger Bands in NinjaScript is a nice three-plot exercise: an average, a standard deviation, and two bands drawn on price. We compute the standard deviation explicitly so we control exactly which flavour (population) it uses. See the explainer for the concept.

What you’ll need

  • NinjaTrader 8
  • New → NinjaScript Editor → Indicators → New Indicator, name it AlgoGenBollinger

The source

#region Using declarations
using System;
using System.Windows.Media;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators;
#endregion

namespace NinjaTrader.NinjaScript.Indicators
{
    public class AlgoGenBollinger : Indicator
    {
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name        = "AlgoGen Bollinger";
                Description = "Bollinger Bands (population std dev)";
                Period = 20; NumStdDev = 2.0;
                IsOverlay = true;

                AddPlot(new Stroke(Brushes.OrangeRed, 2),    PlotStyle.Line, "Middle");
                AddPlot(new Stroke(Brushes.MediumPurple, 1), PlotStyle.Line, "Upper");
                AddPlot(new Stroke(Brushes.MediumPurple, 1), PlotStyle.Line, "Lower");
            }
        }

        protected override void OnBarUpdate()
        {
            if (CurrentBar < Period - 1)
                return;

            double sum = 0.0;
            for (int i = 0; i < Period; i++)
                sum += Close[i];
            double mean = sum / Period;

            double sq = 0.0;
            for (int i = 0; i < Period; i++)
            {
                double d = Close[i] - mean;
                sq += d * d;
            }
            double sd = Math.Sqrt(sq / Period);   // population std: divide by N

            Middle[0] = mean;
            Upper[0]  = mean + NumStdDev * sd;
            Lower[0]  = mean - NumStdDev * sd;
        }

        #region Properties
        [NinjaScriptProperty] [System.ComponentModel.DataAnnotations.Range(1, int.MaxValue)]
        [System.ComponentModel.Display(Name = "Period", GroupName = "Parameters", Order = 0)]
        public int Period { get; set; }

        [NinjaScriptProperty] [System.ComponentModel.DataAnnotations.Range(0.1, double.MaxValue)]
        [System.ComponentModel.Display(Name = "NumStdDev", GroupName = "Parameters", Order = 1)]
        public double NumStdDev { get; set; }

        [Gui.Browsable(false)] [System.Xml.Serialization.XmlIgnore]
        public Series<double> Middle => Values[0];
        [Gui.Browsable(false)] [System.Xml.Serialization.XmlIgnore]
        public Series<double> Upper => Values[1];
        [Gui.Browsable(false)] [System.Xml.Serialization.XmlIgnore]
        public Series<double> Lower => Values[2];
        #endregion
    }
}

Compile & apply

  1. Press F5 to compile.
  2. Add AlgoGen Bollinger to a chart; three lines overlay price, matching the output chart.

Gotchas

  • We divide by Period, not Period − 1. That’s the population standard deviation Bollinger Bands use. Worth doing by hand because platform built-ins don’t always agree on this — computing it yourself removes all doubt.
  • Guard the warm-up. if (CurrentBar < Period - 1) return; skips bars before the window is full.
  • Built-in Bollinger(). NinjaTrader has one, but rolling your own guarantees the std-dev convention matches the rest of this series.

That’s the five-language set: Python, MQL5, Pine Script, EasyLanguage, and this one. Now test a Bollinger rule in AlgoGen.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. John Bollinger's official Bollinger Bands reference (Bollinger Capital Management)
  2. NinjaScript system indicator methods (NinjaTrader)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works