Trading Tips — Measuring Close Direction Reversal Rate in NinjaTrader 8
Two price sequences can have the same net return and still exhibit very different behavior. One may move steadily in a single direction; another may alternate between advances and declines. A conventional momentum indicator measures displacement but does not directly count these changes of direction. This tutorial introduces a simple NinjaScript oscillator that measures the frequency of reversals between adjacent close-to-close movements. The objective is to describe local price behavior, rather than to predict the direction of the next move or automatically enter an order.
Distinguish movement from direction changes
Choose a window of N close-to-close movements, requiring N+1 completed closing prices. Convert each movement to a sign: +1 for an increase, −1 for a decrease and zero for an unchanged close. Compare each movement with its immediate predecessor. A pair is eligible only when both movements are nonzero. Count a reversal whenever the two signs differ. Divide the number of reversals by the number of eligible pairs and multiply by 100. When no eligible pairs exist, leave the result undefined.
This definition deliberately excludes flat movements without bridging across them. An advance followed by a flat close and then a decline is not counted as an immediate reversal. A different implementation could compress flat observations out of the sequence, but that would answer a different question. Keeping the definition explicit is particularly important when studying thinly traded instruments, where repeated closing prices can be common.

Worked examples
Consider the chronological closing prices 100, 102, 101, 103, 104 and 102. Their changes are +2, −1, +2, +1 and −2. The corresponding signs are up, down, up, up and down. The first pair reverses, the second reverses, the third continues upward and the fourth reverses. The result is three reversals divided by four eligible pairs, or 75%. Notice that the total price displacement is still positive even though direction reverses frequently.
In a separate six-close example, 100, 101, 102, 103, 104 and 105 produces no reversals and a reading of zero. Another sequence, 100, 101, 100, 101, 100 and 101, produces four reversals out of four pairs, or 100%. Now consider 100, 101, 101, 100, 99 and 100. Its signs are up, flat, down, down and up. Only the down-down and down-up pairs are eligible, yielding one reversal out of two, or 50%. All examples are hypothetical.
Complete native NinjaScript indicator
using System;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using NinjaTrader.NinjaScript;
namespace NinjaTrader.NinjaScript.Indicators
{
public class CloseDirectionReversalRate : Indicator
{
[NinjaScriptProperty]
[Range(2, 500)]
[Display(Name = "Moves", Order = 1, GroupName = "Parameters")]
public int Moves { get; set; }
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "CloseDirectionReversalRate";
Description = "Percent of adjacent nonzero moves that reverse.";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
Moves = 10;
BarsRequiredToPlot = 0;
AddPlot(Brushes.DodgerBlue, "ReversalRate");
AddLine(Brushes.Gray, 50, "Midpoint");
}
}
protected override void OnBarUpdate()
{
// Moves price changes require Moves + 1 closes.
if (CurrentBar < Moves)
{
Value.Reset();
return;
}
int eligible = 0;
int reversals = 0;
for (int i = 0; i < Moves - 1; i++)
{
// Compare two immediately adjacent close changes.
int newer = Math.Sign(Close[i] - Close[i + 1]);
int older = Math.Sign(Close[i + 1] - Close[i + 2]);
// Flat changes do not form an eligible pair.
if (newer == 0 || older == 0)
continue;
eligible++;
if (newer != older)
reversals++;
}
// No eligible pairs means no meaningful estimate.
if (eligible == 0)
Value.Reset();
else
Value[0] = 100.0 * reversals / eligible;
}
}
}
How the implementation works
The indicator defines a configurable Moves property and uses the NinjaTrader study lifecycle. State.SetDefaults establishes the default calculation mode, chart placement and output. AddPlot creates the primary oscillator, while AddLine supplies a visual midpoint at 50. The midpoint is a descriptive reference and has not been optimized as a trading threshold.
NinjaScript's historical indexing uses zero for the current calculation bar and increasing offsets for older observations. In each loop iteration the newer difference is Close[i] minus Close[i+1], while the older difference is Close[i+1] minus Close[i+2]. The loop stops before i reaches Moves−1, so its oldest reference is Close[Moves]. CurrentBar must therefore be at least Moves before the calculation begins. Resetting the output when history is insufficient or no pair is eligible avoids treating missing data as genuine zero reversal activity.
The direct counting loop takes O(N) work per calculation. It is suitable for illustrating the mathematics and for modest lookback lengths. A more elaborate version could maintain rolling reversal and eligibility counts, but would need to handle initialization, historical recalculation and the removal of the oldest pair correctly.
Installation and configuration
Open NinjaTrader 8 and select the NinjaScript Editor from the Control Center. Create an indicator named CloseDirectionReversalRate, replace its editable indicator class with the source above and preserve any platform-generated wrapper region. Compile the indicator and resolve any environment-specific errors. Add it to a chart through the Indicators dialog. Start with Moves=10, then use Moves=5 when manually reproducing the examples. The code has not been compiled or executed in NinjaTrader here, so validate it in your own installation before using it in further research.
Closed-bar timing and repainting
Calculate.OnBarClose makes the default standalone indicator evaluate after each completed bar. The resulting observation is plotted on the bar whose final close completes the window. No future closes, centered calculations or forward-looking swing confirmations are used. The indicator therefore does not intentionally repaint completed observations. When hosted inside another NinjaScript object, the parent calculation mode can control the calculation frequency; a host using intrabar updates should explicitly enforce the intended closed-bar workflow. Historical data corrections, trading-hours changes and bar types that reconstruct their history can also change historical readings.
Practical validation
Begin with a monotonic sequence, a strictly alternating sequence and a sequence containing repeated closes. Verify both the reversal count and the eligible-pair count manually. Check the first valid bar after warm-up and inspect a fully flat window, which should yield no plotted estimate. Observe the standalone indicator during a forming candle and compare its value after the close. Reload the chart and verify consistency under identical data and session settings. If a strategy consumes this indicator, log the calculation bar timestamp and value so that the strategy and chart can be compared without an indexing ambiguity.
Limitations and research extensions
A high reversal rate may describe alternating price movements, but it does not prove that a market is range-bound. A rising market can alternate small declines with larger advances. A low reversal rate can occur with insignificant price movement. Furthermore, a reading based on one eligible pair is less informative than the same reading based on many pairs. A useful extension is to publish the eligible-pair count as a separate confidence or sample-size measure, or require a minimum count before using the oscillator. A tick-size or volatility-based deadband is another possible extension, but changes the definition of an unchanged move. Evaluate such modifications independently rather than assuming they improve trading performance. The indicator does not account for volume, spread, slippage or liquidity.
For minimum-sample filters, rolling calculation optimization, alerts or automated strategy integration, contact our Custom NinjaTrader indicator Development Services.
Risk disclosure: educational material only. Trading involves risk of loss. Hypothetical examples do not establish profitability.