NinjaTrader 8 Relative Bar Volume Indicator: Compare Each Closed Bar with Its Prior Volume Average
Beginner Trading Tutorial | NinjaTrader 8 | NinjaScript indicator
A volume bar tells you how much activity occurred during one chart bar, but it does not by itself reveal whether that activity is unusually high. A volume reading of 1,000 may be ordinary for an active futures contract and unusual for a less active market. Even on the same instrument, activity can change during the trading session. A useful first step is to compare each observation with a recent reference level rather than interpret its raw value in isolation.
In this tutorial, we build a NinjaTrader 8 indicator that divides the volume of each completed bar by the mean volume of the preceding N bars. The result is a dimensionless ratio, often called relative volume or RVOL. The implementation uses NinjaScript's built-in SMA with volume as its input and deliberately excludes the evaluated bar from the reference average.
1. Define the baseline before evaluating the observation
Let V(t) denote the volume of the bar being evaluated and N the lookback period. The reference is the arithmetic mean of V(t−1) through V(t−N). Relative bar volume is RVOL(t) = V(t) / mean of the previous N bar volumes. The default lookback is 20, but the following worked example uses five bars so every number can be verified by hand.

Suppose the five earlier volumes, from oldest to newest, are 80, 100, 120, 90 and 110. Their sum is 500, so their mean is 100. A newly completed bar with volume 180 has an RVOL of 1.80. This means that the evaluated bar contains 1.8 times the average volume of the preceding bars, or 80% more than that reference.
Including the evaluated bar in a five-bar mean would instead drop the oldest value of 80 and include 180. The mean becomes 120, and the ratio falls to 1.50. Both formulas can be defined intentionally, but they are not equivalent. The prior-only version keeps the benchmark fixed before the new observation enters it, which makes the comparison easier to interpret. All values in this tutorial are hypothetical.
| Current volume | Prior mean | RVOL | Interpretation |
|---|---|---|---|
| 180 | 100 | 1.80 | Above baseline |
| 100 | 100 | 1.00 | Equal to baseline |
| 50 | 100 | 0.50 | Below baseline |
| 0 | 100 | 0.00 | No recorded volume |
| 180 | 0 | Undefined | No valid ratio |
2. What the indicator displays
The indicator appears in a separate panel and plots a blue ratio series alongside a gray horizontal reference at 1.0. Readings greater than one identify bars with above-average activity relative to the selected lookback; values below one identify lower activity. The value is expressed as a multiple rather than a percentage. No automatic entry, exit or directional signal is generated.
We select Calculate.OnBarClose by default. This means the standalone chart indicator evaluates the bar when it closes, avoiding a comparison between an unfinished bar's accumulated volume and the totals of earlier completed bars. This is especially important when using time bars: a five-minute bar that has existed for only ten seconds has not had the same opportunity to accumulate volume as a full five-minute bar.
3. Complete NinjaScript source
// PatternSmart Relative Bar Volume - NinjaTrader 8
using System;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using NinjaTrader.NinjaScript;
namespace NinjaTrader.NinjaScript.Indicators
{
public class PSRelativeBarVolume : Indicator
{
private SMA volumeMean;
[NinjaScriptProperty]
[Range(1, 500)]
[Display(Name = "Lookback", Order = 1,
GroupName = "Parameters")]
public int Lookback { get; set; }
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "PSRelativeBarVolume";
Description = "Closed-bar volume relative to prior bars.";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
BarsRequiredToPlot = 0;
Lookback = 20;
AddPlot(Brushes.RoyalBlue, "RVOL");
AddLine(Brushes.Gray, 1.0, "Baseline");
}
else if (State == State.DataLoaded)
{
// Use volume, not price, as the SMA input.
volumeMean = SMA(Volume, Lookback);
}
}
protected override void OnBarUpdate()
{
// Need N preceding bars plus the evaluated bar.
if (CurrentBar < Lookback)
{
Value.Reset();
return;
}
// Shift one excludes the evaluated bar.
double mean = volumeMean[1];
double current = Volume[0];
if (double.IsNaN(mean) || double.IsInfinity(mean)
|| mean <= 0.0 || double.IsNaN(current)
|| double.IsInfinity(current) || current < 0.0)
{
Value.Reset();
return;
}
Value[0] = current / mean;
}
}
}
4. Understand the NinjaTrader event structure
In State.SetDefaults we define the indicator name, description, display mode, calculation frequency, input and plot. The Lookback property is exposed as an input with a valid range of one to 500. The AddPlot call creates the output series and AddLine adds the reference level without requiring a second calculated series.
State.DataLoaded is where we instantiate SMA using Volume rather than Close. NinjaTrader supports passing a suitable data series into its built-in indicators. Holding a reference to this SMA avoids reconstructing the indicator on every bar update. The moving average performs the underlying rolling calculation; our indicator only needs to retrieve its earlier result.
Within OnBarUpdate, CurrentBar is a zero-based index. We need N earlier bars plus the evaluated bar, so we return until CurrentBar is at least Lookback. BarsRequiredToPlot controls when plots become visible, not whether accessing an unavailable history element is safe. The explicit history check is therefore necessary even though that display property is set to zero.
In standalone OnBarClose operation, Volume[0] represents the bar that triggered the closed-bar calculation. volumeMean[1] is the SMA value at the preceding bar and therefore includes exactly the N bars immediately before the evaluated bar. The numerator and denominator do not include future observations. When the baseline is zero or invalid, Value.Reset() marks the current output as unset instead of inventing a ratio of zero or one.
5. Choose a practical lookback
| Lookback | Reference behavior |
|---|---|
| 5 | Responds quickly to a burst of recent activity |
| 20 (default) | Balances recent activity with a modest smoothing window |
| 50 | Changes more slowly; may span different session conditions |
A short baseline quickly adapts after a large volume bar enters the reference, so the next bar may appear less exceptional. A longer baseline retains the influence of older observations for longer. Neither setting is universally superior. Begin by holding the chart type, session template and timeframe constant while comparing the results across several periods. A ratio of 1.5 or 2.0 may be useful for exploratory filtering but should not be treated as an optimal threshold without testing.
6. Install the indicator
- In the NinjaTrader Control Center, open New → NinjaScript Editor.
- Create a new indicator named PSRelativeBarVolume through the editor. Replace the user-authored indicator class with the source above; keep the platform-generated wrapper region if present.
- Compile using the editor and review any diagnostics.
- Open a chart, choose Indicators and add PSRelativeBarVolume.
- Keep Calculate set to On bar close, select a lookback and apply the settings.
- Compare the output with a standard volume indicator and the underlying bar volumes.
7. Validation checklist and common mistakes
Set Lookback to five and manually read the volume of six consecutive completed bars. Average the first five and divide the sixth by the result. Verify that the plotted value matches the calculation. Repeat across several different bars, including one with low volume. The first N bars should have no calculated ratio. Where data includes a zero baseline, no valid ratio should be plotted. Change the lookback and repeat the manual comparison rather than assuming the indicator is correct merely because it draws a line.
When used inside a strategy or another NinjaScript, the parent script's Calculate setting may govern the update behavior of hosted indicators. Do not assume the closed-bar default alone enforces completion in every hosting arrangement. Verify the parent event model or explicitly sample completed bars when integrating. Also avoid using OnPriceChange as a substitute for complete intrabar volume updates: volume can change without a price change.
8. Interpret activity in context
The ratio compares consecutive bars, not the same time of day across different sessions. A market's opening period may naturally be more active than its midday period, and a rolling baseline can mix both regimes. Volume-based bars have a fundamentally different interpretation because their construction itself uses volume. The data provider, contract rollover, session settings and historical data corrections can also affect the reported volumes. High activity does not distinguish aggressive buying from aggressive selling, and does not guarantee trend continuation.
If you want to extend this free indicator with a time-of-day baseline, volume alerts or strategy integration, contact us through our Custom Trading Software Development Services.
The code was reviewed against the NinjaTrader API documentation, but has not been compiled or run inside NinjaTrader 8. Validate it with your own installation and data. Trading involves risk, and relative volume alone cannot guarantee trading results.