Beginner Trading Tutorial — Confirmed Close Direction Streak Indicator for cTrader

A chart can rise over several candles without every candle being bullish. Likewise, a succession of lower closes may contain green candle bodies when prices open below the preceding close. This distinction provides an excellent beginner programming exercise: rather than counting candle colors, count consecutive changes in closing price. In this tutorial we develop a native cTrader C# indicator that presents the current sequence as a signed histogram. The implementation uses completed candles only and makes the reset behavior explicit, so a reader can calculate the results independently.

Define the sequence

Compare the closing price of each completed bar with the close immediately before it. A higher close extends a positive sequence by one, or starts a new positive sequence at one if the previous sequence was negative or zero. A lower close extends a negative sequence by subtracting one, or starts a new sequence at minus one if the previous sequence was positive or zero. Equal closes reset the result to zero. The first available historical candle has no preceding close, so its initial result is zero.

This definition differs from comparing the open and close within a single candle. Suppose the preceding close is 100, the next candle opens at 105 and closes at 102. The candle has a bearish body, but its close is higher than the preceding close. Our indicator counts that observation as an upward step. The magnitude of each price change is irrelevant; both a one-tick increase and a ten-point increase add one to the streak.

Confirmed Close Direction Streak Indicator
Hypothetical completed closes of 100, 101, 103, 102, 101, 101 and 104 produce signed streaks of 0, +1, +2, −1, −2, 0 and +1.

Work through the example

Start with 100 and initialize the score to zero. The close of 101 starts an upward streak of one. The next close of 103 extends it to two. A close of 102 reverses the direction, so the new result is minus one, not one or zero. Another decline to 101 extends the negative streak to minus two. A second close at 101 resets the count to zero, and the move to 104 starts a fresh upward streak of one.

These seven observations are hypothetical and represent consecutive completed candles. Notice that the last three-point increase generates a score of only one, even though the earlier upward streak of two includes changes of one and two points. The score measures persistence, not cumulative return. A streak of five tiny increases can be larger than the score associated with a single substantial price move.

Complete native C# indicator

using System;
using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false,
        AccessRights = AccessRights.None)]
    public class ConfirmedCloseStreak : Indicator
    {
        [Output("Signed Streak", LineColor = "SteelBlue",
            PlotType = PlotType.Histogram, Thickness = 3)]
        public IndicatorDataSeries Result { get; set; }

        public override void Calculate(int index)
        {
            // First loaded bar has no preceding close.
            if (index == 0)
                Result[0] = 0;

            // The last bar is forming. Publish the preceding
            // completed bar, then leave the live bar empty.
            if (index == Bars.Count - 1)
            {
                if (index > 0)
                    UpdateCompleted(index - 1);
                Result[index] = double.NaN;
                return;
            }

            UpdateCompleted(index);
        }

        private void UpdateCompleted(int i)
        {
            if (i == 0)
            {
                Result[i] = 0;
                return;
            }

            double previous = Result[i - 1];
            // Recover from an uninitialized preceding value.
            if (double.IsNaN(previous))
            {
                previous = 0;
                int j = i - 1;
                // Recover the preceding streak from history.
                while (j > 0)
                {
                    double d = Bars.ClosePrices[j] -
                               Bars.ClosePrices[j - 1];
                    if (d == 0 ||
                        (previous > 0 && d < 0) ||
                        (previous < 0 && d > 0))
                        break;
                    previous += d > 0 ? 1 : -1;
                    j--;
                }
            }

            double current = Bars.ClosePrices[i];
            double prior = Bars.ClosePrices[i - 1];

            // Compare closes, not candle body direction.
            if (current > prior)
                Result[i] = Math.Max(previous, 0) + 1;
            else if (current < prior)
                Result[i] = Math.Min(previous, 0) - 1;
            else
                Result[i] = 0;
        }
    }
}

How the implementation works

The Indicator attribute places the output in a separate panel and declares that no additional access rights are needed. The Output attribute defines an IndicatorDataSeries with a histogram plot. This series stores the signed result for each chart index. cTrader calls Calculate with the index being evaluated. Its bar collection uses chronological indexing: index zero represents the oldest loaded bar, unlike the reverse indexing often used in MetaTrader examples.

For historical completed bars, Calculate invokes UpdateCompleted directly. That method reads the previous result and compares the current closing price with its predecessor. Math.Max ensures that an upward step after a negative sequence starts at one. Math.Min ensures that a downward step after a positive sequence starts at minus one. Equal closes explicitly reset the sequence. The method writes a value derived from prior bar state instead of incrementing a mutable global counter. Consequently, repeated calculations for the same bar do not accidentally count that bar multiple times.

A fallback reconstructs the preceding streak from earlier closing prices if the previous output is NaN. It walks backward until encountering an equal close, a reversal, or the beginning of loaded history. Under ordinary sequential historical calculation this fallback should rarely be needed. The normal calculation requires a constant amount of work per bar; recovery can require more iterations for a long uninterrupted sequence.

Install in cTrader

Open the Algo workspace in cTrader, locate the Indicators section and create a new C# indicator. Name it ConfirmedCloseStreak, replace the generated source with the complete example, and build it. Resolve any compiler messages in your installed version. Add the indicator to a chart and inspect the separate histogram panel. The positive bars represent rising-close sequences, while negative bars represent falling-close sequences. The plot uses one color so the sign and position relative to zero carry the directional meaning. This source has not been compiled or executed in cTrader here.

Closed-bar confirmation and repainting

During live calculation the last bar can change with every incoming quote. The implementation deliberately leaves that bar's output as double.NaN. When a new bar appears, the preceding bar is explicitly calculated again from its final close. Its confirmed output becomes available when the new bar is recognized, even though it is drawn under the preceding candle. For example, an automated consumer should read the most recently completed index rather than assume that the newest output is a usable number.

There are no future price references in the calculation of a completed bar, and no deliberate repainting of fixed historical prices. Nevertheless, the displayed history may change if the underlying data is corrected, if the chart type changes, or if older history is loaded. Since the streak is initialized at the first available candle, a sequence already in progress before that boundary will be truncated. Loading more history can extend the initial streak until the next reset or reversal. At a market closure, the last candle remains excluded until the platform recognizes a subsequent bar.

Testing checklist and practical interpretation

Manually compare the seven sample closes with the expected results. Test a long uninterrupted upward sequence, a long downward sequence, alternating closes, and several equal closes in succession. Confirm that a reversal changes the count directly to one or minus one. During live ticks check that the forming bar remains blank and that the preceding bar becomes visible after a new candle starts. Reload identical chart data and compare output beyond the initial history boundary. Test unusual chart types separately because their bar formation rules may differ from ordinary time bars.

A long streak is neither a guaranteed continuation signal nor a guaranteed reversal signal. It describes the number of consecutive price changes in one direction, not their size, liquidity, volatility or statistical significance. Consider pairing this indicator with separate measurements of price movement and volatility during research. Any thresholds should be evaluated against independent historical samples, realistic costs and market regimes rather than selected from a single attractive chart.

For configurable flat-price handling, separate histogram colors, streak alerts or cBot integration, contact our Custom cTrader indicator Development Services.

Risk disclosure: educational content only. Trading involves risk of loss; hypothetical examples do not demonstrate profitability.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.