The A/D Line in NinjaScript is a running total that reads its own prior value — a close cousin of the OBV implementation, but weighted by close location. See the explainer.
What you’ll need
- NinjaTrader 8
- New → NinjaScript Editor → Indicators → New Indicator, name it
AlgoGenAD
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 AlgoGenAD : Indicator
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "AlgoGen A/D Line";
Description = "Accumulation/Distribution Line (Chaikin)";
IsOverlay = false;
AddPlot(new Stroke(Brushes.MediumSeaGreen, 2), PlotStyle.Line, "AD");
}
}
protected override void OnBarUpdate()
{
double rng = High[0] - Low[0];
double mfm = rng == 0 ? 0.0 : ((Close[0] - Low[0]) - (High[0] - Close[0])) / rng;
double mfv = mfm * Volume[0];
Value[0] = CurrentBar == 0 ? mfv : Value[1] + mfv; // cumulative
}
}
}
Compile & apply
- Press F5 to compile.
- Add AlgoGen A/D Line to a chart; the cumulative line draws in its own panel,
matching the output chart. Add
EMA(AD, 3) - EMA(AD, 10)for the Chaikin Oscillator.
Gotchas
Value[1]is the running total. Reading your own prior plotted value is what accumulates the line — no separate field needed.- Flat-bar guard.
rng == 0 ? 0.0prevents a divide-by-zero on a locked bar. - Read slope, not level; volume quality matters. The absolute value is arbitrary; and on instruments without real volume the A/D Line is only a proxy.
- Chaikin Oscillator. Store the A/D in a
Series<double>and plotEMA(adSeries, 3)[0] - EMA(adSeries, 10)[0]for Chaikin’s zero-centered money-flow momentum — the same data as a bounded reading. - Divergence. Compare the A/D slope to price over a lookback (
Value[0] < Value[20]whileClose[0] > Close[20]) to flag distribution — the line’s signature use, and a cleaner signal than the raw level.
That’s the five-language set: Python, MQL5, Pine Script, EasyLanguage, and this one. Now test an A/D 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
- NinjaScript system indicator methods (NinjaTrader)
