A breakout strategy can miss every legitimate entry even when price is visibly above the previous trading range. The culprit is sometimes neither market conditions nor the order API, but a single indexing mistake: calculating the range with the bar being tested. In MultiCharts .NET, a C# signal also needs enough history reserved before it accesses older bars.
Separate the reference window from the decision bar
Inside a SignalObject's CalcBar(), the index zero refers to the bar in the calculation context. Under ordinary bar-close evaluation, Bars.Close[0] is the completed decision bar; Bars.High[1] is the preceding bar's high. A 20-bar breakout therefore compares the current close with the highest high among offsets 1 through 20, not 0 through 19. The latter contains the decision bar and discards the oldest intended reference bar.

Why a correct range still needs MaxBarsBack
The largest offset accessed is 20, so the calculation needs the decision bar plus 20 preceding bars. Configure the signal's maximum historical reference setting to accommodate offset 20, and any deeper offsets used by other logic. Verify the first eligible calculation in the actual chart rather than assuming a particular bar number. Automatic detection can be convenient, but for a parameterized lookback, explicitly reserve the maximum tested offset when practical. Load additional pre-test history so the intended evaluation period begins with a valid reference window.
C# calculation inside CalcBar()
Illustrative excerpt, not a complete executable strategy. It assumes the study has sufficient historical data, the required reference setting is configured, and intrabar order generation is disabled for this bar-close example. Include System for Math in the enclosing signal if required. No order is submitted by this excerpt.
protected override void CalcBar()
{
const int lookback = 20;
double priorHigh = double.NegativeInfinity;
for (int i = 1; i <= lookback; i++)
priorHigh = Math.Max(priorHigh, Bars.High[i]);
bool breakout = Bars.Close[0] > priorHigh;
// Apply position and order-state checks before entry.
}
The condition is a state, not necessarily a unique event. If a strategy should enter only once, add its own position, pending-order, and signal-transition checks. A crossover test may need an additional offset of 21 to evaluate the previous bar against its own prior range. Do not simply increase the loop bound without also updating the history requirement.
Worked example and failure cases
| Hypothetical calculation | Reference high | Close | Result |
|---|---|---|---|
| Offsets 1–20 | 106 | 107 | Breakout |
| Offsets 0–19 | 108 | 107 | No breakout |
| Insufficient history | Unavailable | 107 | Do not evaluate |
Since a bar's close cannot exceed its own high under normal OHLC data, including that high in the maximum makes a strict close-above-maximum rule impossible. This is a logical error, not evidence of a weak trading edge.
Validate before deployment
- Load a short known dataset and independently calculate the previous 20 highs.
- Compare the first calculated bar and prior maximum with the expected window; log bar time, reference maximum, close and decision.
- Change the lookback from 20 to 40 and ensure both the data reservation and test warm-up expand accordingly.
- Repeat with different amounts of pre-test history. Compare signals over the same valid evaluation interval, not the entire loaded chart.
- Test in simulation with realistic execution assumptions. A bar-close signal is not a guarantee of a fill at the closing price. If enabling intrabar generation later, explicitly redesign confirmation and order handling for repeated CalcBar calls.
See the MultiCharts .NET data access documentation and calculation modes. The excerpt has not been compiled or run on the platform. The numerical data are illustrative; historical signals do not guarantee profitable or attainable live execution, and gaps, fees and slippage can materially alter outcomes.
For a tailored signal architecture, historical-data validation or execution safeguards, contact our Custom Trading Software Development Services.