New free code from MQL5: indicators, EAs, and scripts for traders.
Candle Streak Statistics: What Happens After N Same-Color Bars
The idea behind it
Almost every trader has an opinion about runs of candles. After five green bars in a row, "it is overextended and must turn", or "it has momentum and will go on". Both opinions can be tested, and the test is simple: take every moment in the history at which a run of N bars of the same color had just ended in a bar, and look at what the next bar did. Most streak indicators only count the bars of the current run. This one counts what came after the run, and it does so for your symbol, your timeframe and your broker's data.
The definitions are short:
- Bar color. Up if the close is above the open, down if it is below. A bar whose body is at most InpDojiPoints points is a doji. With the default of 0, only a bar with open and close at the same price is a doji. A doji has no color and ends a run.
- Run. The number of bars of the same color in a row, ending in a given bar. It is positive after up bars and negative after down bars.
- Row N. The moments at which the run that ends in a bar is exactly N bars long so far. The last row, for example "7+", takes every run of InpMaxLen bars or more. A bar belongs to one row only, so the rows do not overlap, and a run of five bars passes through rows 1, 2, 3, 4 and 5 one after the other.
- Outcome. The bar that follows either continues the run (same color), reverses it (opposite color), or is a doji. cont% is the share of cases in which the run continued: the probability that a run which is N bars long gets one bar longer.
- Baseline. If bars were independent of each other, a run would continue exactly as often as bars of that color occur at all. So the baseline for up runs is the share of up bars in the whole history, and for down runs the share of down bars. A cont% above the baseline means that runs tend to go on, and a cont% below it means that they tend to turn.
- next. The average move of the next bar in the direction of the run, from its open to its close, measured in ATR of the bar before it. A positive value means that the next bar moved on average with the run, a negative value against it.
- z. How many standard errors the cont% is away from the baseline. A row is marked with * when it has at least InpMinCases cases and the z-score is at least InpZFlag in size. Rows with fewer cases show "--", because a percentage of ten cases means nothing.
There is no look-ahead. Each bar is shown with the statistics of the pairs of bars that were completed before it, and its own outcome is added to the table only after it has been shown. A bar that is still forming is shown with the statistics of the closed bars, but it is never counted, because its color can still change. The calculation is incremental: on every call only the new bars and the bar that was still forming are processed, and the counts live in small fixed tables, so the history is never rescanned.
Smooth out market noise with the Heiken Ashi Expert Advisor for MT4 and MT5. Reliable trend-following automation. Details here.
How to interpret it
- Histogram (indicator window). One bar per chart bar. Its height is the length of the run that ends in that bar: white bars above zero are up runs, gray bars below zero are down runs. A doji has no bar. The dotted line is zero.
-
Data Window.
- "Continued before, same streak (%)": how often a run of this length had continued in the history before this bar.
- "Cases before, same streak": the number of cases that figure is based on.
- "Next bar before, in streak direction (ATR)": the average move of the next bar in the direction of the run, in the history before this bar.
-
Panel (top left of the chart).
- Line 1: symbol, timeframe and the number of closed bars analysed.
- Line 2: the column names. UP n and DN n are the numbers of cases after up runs and after down runs.
- One row for every run length up to InpMaxLen , with the four figures for up runs on the left and for down runs on the right: cases, cont%, next and the * mark.
- Baseline line: the shares of up bars, down bars and doji in the history, and the settings behind the * mark.
- Now line: the run of the newest bar and what history says about runs of that length.
- Last line: symbol, timeframe, number of bars, the share of up bars and the continuation after a run of InpReportLength bars, up and down. It is meant to be copied into a comment.

Fig. 1. A simulated H1 series in which runs tend to continue, so the table has something to show. The histogram shows the run length of the last 90 bars (white up runs, gray down runs, dotted line at zero). The panel shows the table for all 2,999 closed bars: cases, continuation rate, average move of the next bar in ATR, and the mark for rows that differ from the baseline. The numbers come from simulated data and say nothing about any real market. Black and white screenshot, per the Code Base image guidelines.
External variables (inputs)
| Input | Default | Purpose |
|---|---|---|
| InpMaxLen | 7 | Longest run that has its own row (2-12). Longer runs are counted in the last row. |
| InpDojiPoints | 0 | A bar whose body is at most this many points is a doji (0-100). Raise it on symbols with many tiny bodies. |
| InpAtrPeriod | 14 | Period of the Wilder ATR that measures the next bar (2-100). The first bars of the analysis build the ATR and are not used for the "next" figure. |
| InpHistoryBars | 20000 | Newest bars analysed when the indicator loads or is fully recalculated. 0 analyses all bars. New bars are added as they arrive. |
| InpMinCases | 30 | Fewest cases before a row shows percentages (5-1000). It also limits the * mark. |
| InpZFlag | 2.0 | A row gets the * mark when its z-score is at least this large in size (1-5). |
| InpReportLength | 3 | Run length used in the last line of the panel (1-12). It is kept at or below InpMaxLen . |
| InpShowPanel | true | Show the statistics panel. |
| InpTextColor | clrSilver | Panel text color. |
| InpFontSize | 8 | Panel font size (Consolas). The line height follows the size. |
| InpPanelX , InpPanelY | 8, 14 | Panel offset from the upper left corner of the chart, in pixels. |
Recommended use
- Symbols and timeframes. Any symbol and any timeframe. The statistics describe one symbol on one timeframe, so the table of H1 is not the table of M15. Run it on the symbols you trade and compare the tables.
- Number of cases. The long rows are rare. With a few thousand bars the rows for runs of six or more bars have a few dozen cases at best, and their percentages jump around. Look at the "n" columns first. Load enough history through InpHistoryBars , and treat rows under 100 cases with care.
- Read the * marks with care. A table has up to 24 rows. At a z-score of 2 about one row in twenty is marked by chance, so one or two marks in an otherwise ordinary table mean nothing. Rows that are next to each other are also related, because the same run passes through them. A row of a few cases is not evidence, and the z-score assumes that cases are independent, which they are not when volatility comes in waves. Many marks in a row with the same sign are more interesting than a single one.
- Look for consistency. Change InpHistoryBars to see the older and the newer part of the history separately. A pattern that exists in one half and not in the other is not a pattern.
- An edge is not a profit. A cont% of 52% with a "next" of +0.02 ATR is smaller than the spread of most symbols. The indicator does not include spread, swap or slippage.
- How the color is decided. The color follows the body of the bar (open to close), not the close against the previous close. On symbols that open with a gap, a bar can be colored down even if it closed above the previous close. A run is counted over consecutive bars of the chart, so it continues over a weekend or a session break.
- The data is the broker's data. The open and close of a bar depend on the broker's feed and its time zone. The same symbol can give a slightly different table on another broker.
- It describes the past. The table tells you what was usual in the loaded history. It does not predict the next bar.
- Performance and memory. The counts are kept in tables of a few dozen numbers, and the indicator has six buffers. Later ticks only process the newest bar and update the panel.
- Chart objects. The only objects are the panel lines in the main chart window, named with the prefix CS_ . They are removed in OnDeinit() .
- Account types. The indicator does not trade and does not depend on netting or hedging accounts.
- Your turn: post your numbers. Attach the indicator to a chart you trade, copy the last line of the panel and post it in the comments with the name of your broker. This is only the format, and its numbers are made up: EURUSD H1 | 20000 bars | up bars 50.1% | after 3 up: cont 49.4% (n=1450) | after 3 down: cont 50.2% (n=1432) . Which symbols have runs that continue, and which ones turn? Does a run of three behave differently from a run of six on your symbol?
Complement community code with advanced automation from RobotFX.
80076