Awesome Oscillator in NinjaTrader — NinjaScript (C#) Source

A custom AO with per-bar momentum colouring for NinjaTrader 8

Awesome Oscillator in NinjaTrader
Awesome Oscillator in NinjaTrader

The Awesome Oscillator in NinjaScript reuses the built-in SMA on the median price, plus per-bar PlotBrushes colouring. See the explainer for the concept.

What you’ll need

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

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 AlgoGenAO : Indicator
    {
        private Series<double> median;

        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name        = "AlgoGen Awesome Oscillator";
                Description = "Awesome Oscillator (5/34 median price)";
                Fast = 5; Slow = 34;
                IsOverlay   = false;

                AddPlot(new Stroke(Brushes.MediumSeaGreen, 2), PlotStyle.Bar, "AO");
            }
            else if (State == State.DataLoaded)
            {
                median = new Series<double>(this);
            }
        }

        protected override void OnBarUpdate()
        {
            median[0] = (High[0] + Low[0]) / 2.0;
            if (CurrentBar < Slow - 1)
                return;

            Value[0] = SMA(median, Fast)[0] - SMA(median, Slow)[0];

            // Green if AO rose vs the prior bar, red if it fell.
            if (CurrentBar > 0)
                PlotBrushes[0][0] = Value[0] >= Value[1]
                    ? Brushes.MediumSeaGreen : Brushes.IndianRed;
        }

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

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

Compile & apply

  1. Press F5 to compile.
  2. Add AlgoGen AO to a chart; the green/red histogram draws in its own panel, matching the output chart.

Gotchas

  • SMA(median, ...). We run the built-in SMA over a Series<double> of the median price (H+L)/2, not over close.
  • PlotBrushes[0][0] colours the current bar of plot 0 — compare Value[0] to Value[1] (momentum), not to zero.
  • median as a Series. Storing it as a Series<double> lets SMA look back over it and survives reloads.
  • Bonus — the Accelerator Oscillator. Store the AO in its own Series<double> and subtract a 5-SMA of it to get Williams’ AC:
// ao is a Series<double> holding Value[0] each bar:
ao[0] = SMA(median, Fast)[0] - SMA(median, Slow)[0];
double ac = ao[0] - SMA(ao, 5)[0];   // Accelerator Oscillator
  • Colour by momentum. For both AO and AC, compare the current value to the previous bar’s (not to zero) when choosing green vs red.

That’s the five-language set: Python, MQL5, Pine Script, EasyLanguage, and this one. Now test an AO 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. 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