Advanced Trading Tutorial — cTrader Automate custom indicator development
When a new candle opens away from the previous close, the opening discontinuity creates a clearly defined price distance. By the time that candle closes, price may have moved back toward the previous close, continued away from it, or crossed it completely. These outcomes are not equivalent, even if the candle has a large intrabar range. The Close Gap Repair Share indicator measures the proportion of the opening discontinuity that has been repaired at the close. It uses a deliberately bounded definition, ignores insignificant gaps, and evaluates completed bars only. The resulting series is intended for market structure research and indicator integration, not automated order placement.
A signed formula that works in both directions
Let P denote the previous candle's closing price, O the candidate opening price, and C the candidate closing price. The signed opening gap is G = O minus P. A positive G represents an upward gap and a negative G represents a downward gap. The raw repair fraction is (O minus C) divided by G. Because the denominator retains its sign, the formula works without two separate directional branches. For an upward gap, a decline toward P yields a positive fraction. For a downward gap, a rise toward P also yields a positive fraction.
Clamp the raw fraction to the interval from zero to one, then multiply by one hundred. A close that remains at the opening price or moves farther away produces zero. A close that returns exactly to the previous close produces one hundred. A close that moves beyond the previous close also produces one hundred. The clamping convention intentionally discards the magnitude of overshoot and extension. If those outcomes matter to your application, the unclamped fraction can be exported as a separate measurement rather than silently changing the definition of the bounded plot.
Before dividing, require the absolute gap to be at least the configured number of minimum price ticks. The default is two ticks, converted to price units with Symbol.TickSize. Zero gaps and gaps smaller than the threshold yield a missing value, not zero. This distinction prevents an ineligible observation from being confused with an eligible gap that has not been repaired at all. The reference line at fifty is fixed and purely illustrative.
Worked example: an upward opening gap
Suppose the preceding close is 100, the candidate opens at 104, and it closes at 101. The opening gap is four price units, and the movement back toward the previous close is three units. The raw repair fraction is three divided by four, or 75%. If the instrument's tick size is 0.25, the default two-tick threshold is 0.50, so the candidate is eligible. The remaining unrepaired distance at the close is one price unit.

Now consider the mirror case: previous close 100, open 96, and close 99. Both numerator and denominator are negative: (96 minus 99) divided by (96 minus 100) equals 0.75. The same indicator value is produced. For an upward gap from 100 to 104, a close of 105 gives a negative raw fraction and is displayed as zero; a close of 99 gives a raw fraction above one and is displayed as one hundred. These examples are hypothetical and are provided to establish the calculation contract.
Complete cTrader C# indicator
using System;
using cAlgo.API;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
public class CloseGapRepairShare : Indicator
{
[Parameter("Minimum Gap (Ticks)", DefaultValue = 2, MinValue = 1)]
public int MinimumGapTicks { get; set; }
[Output("Repair (%)", LineColor = "RoyalBlue", PlotType = PlotType.Points)]
public IndicatorDataSeries Repair { get; set; }
[Output("Reference", LineColor = "Gray")]
public IndicatorDataSeries Reference { get; set; }
public override void Calculate(int index)
{
// Calculate the previous bar when the latest bar is forming.
if (index == Bars.Count - 1)
{
Repair[index] = double.NaN;
Reference[index] = double.NaN;
if (index > 0)
Evaluate(index - 1);
return;
}
Evaluate(index);
}
private void Evaluate(int i)
{
Repair[i] = double.NaN;
Reference[i] = 50.0;
if (i < 1)
return;
double prior = Bars.ClosePrices[i - 1];
double opening = Bars.OpenPrices[i];
double closing = Bars.ClosePrices[i];
double gap = opening - prior;
double magnitude = Math.Abs(gap);
double minimum = MinimumGapTicks * Symbol.TickSize;
if (magnitude <= 0 || magnitude < minimum)
return;
// Positive means movement toward the prior close.
double raw = (opening - closing) / gap;
double bounded = Math.Max(0, Math.Min(1, raw));
Repair[i] = 100.0 * bounded;
}
}
}
Installation and use
In cTrader, open the Algo workspace, create a new custom indicator named CloseGapRepairShare, and replace the template with the supplied source. Build the project and add the indicator to a standard chart. The blue points show eligible completed gap observations, while the gray line marks 50%. Points are used rather than a continuous line to avoid implying measurements on bars with missing values. Minimum Gap (Ticks) is the sole adjustable parameter. The code has not been compiled in a running cTrader environment for this tutorial; perform that build and verify the plotted output locally before integration.
Handling the forming bar
The Calculate method is called for historical bars and also for the newest bar as market data changes. A historical index before the last available bar is evaluated normally. When the method receives the newest index, both outputs at that index are set to NaN and the preceding bar is evaluated again. This ensures that when the latest bar advances, the newly completed candle receives its value. The forming candle is deliberately excluded, even if the session has ended and no subsequent bar exists yet. This conservative convention can delay the last observation until the next bar becomes available. It also prevents the visible score for the newest candle from fluctuating with each tick.
The calculation reads the candidate opening and closing prices and one preceding close. It does not inspect future highs or lows, use secondary timeframes, or inherit a state from a future candle. Earlier observations may nevertheless change when the provider revises historical data or when the chart construction or session settings change. Always compare identical datasets when investigating discrepancies. Consumers of the output series must treat NaN as missing, rather than replacing it with zero before calculating averages or filters.
Parameter selection and market context
A one-tick threshold admits the smallest detectable discontinuity and can be useful for inspecting how the formula behaves. It can also produce many observations whose economic size is negligible relative to transaction costs. Larger thresholds isolate more substantial gaps but reduce the sample. Tick size is a price increment, not a measure of economic risk or volatility. A fixed number of ticks can have very different significance across instruments and chart periods. If a cross-instrument comparison is required, investigate a separate volatility-normalized threshold and validate it independently. Do not assume the same numerical setting has equivalent meaning everywhere.
Opening gaps are defined between adjacent bars, not exclusively at exchange session openings. On continuous markets or short intraday charts, the eligible sample may be sparse. On synthetic or non-time-based charts, bar opening prices may reflect chart construction rather than an actual tradeable discontinuity. If the research question specifically concerns overnight gaps, introduce an explicit session-boundary filter and specify the exchange calendar.
Validation checklist
- Reproduce the upward and downward 75% examples.
- Check an exact return to the prior close, an overshoot, and movement farther from the prior close.
- Test a zero gap and a gap below the minimum threshold; both should remain missing.
- Verify that the first historical bar has no repair value and that the forming bar remains empty.
- Confirm the previous bar receives its completed value when a new bar appears.
- Compare identical historical data after a chart reload and inspect session boundaries, adjusted prices and different tick sizes.
Limitations and customization
This is a close-based measurement. A candle may touch the previous close intrabar and finish farther away; it will not necessarily receive a full repair score. Conversely, a close beyond the previous close does not prove that an executable order could have been filled at that level. The indicator omits spread, liquidity, trading costs and intrabar price sequence. The fifty reference is not a calibrated probability or an entry signal. If you want to add session filters, separate upward and downward outputs, or integrate this free indicator into a cBot, contact us through our Custom cTrader indicator Development Services.
Trading involves risk of loss. Hypothetical examples do not establish expected performance. Validate the implementation and all derived trading assumptions before using them with real capital.