← Free code

Free Pine Script code

3-Bar Reversal Signals

The Pine Script indicator from our Pine Script → MQL5 conversion video. Copy it into TradingView and try it yourself.

3-bar-reversal-signals.pine

Pine Script v6

//@version=6
indicator("3-Bar Reversal Signals", overlay=true)

// Settings
barsRequired = input.int(3, "Consecutive Bars", minval=2, maxval=10)
requireHigherCloses = input.bool(true, "Require Higher Closes")
showSignals = input.bool(true, "Show Arrows")

// Candle direction
bullish = close > open
bearish = close < open

// Count consecutive bullish and bearish candles
var int bullishCount = 0
var int bearishCount = 0

bullishCount := bullish ? bullishCount[1] + 1 : 0
bearishCount := bearish ? bearishCount[1] + 1 : 0

// Optional momentum filter
higherCloses = ta.rising(close, barsRequired)
lowerCloses = ta.falling(close, barsRequired)

// Signal conditions
bearishSignal = bullishCount == barsRequired and (not requireHigherCloses or higherCloses)
bullishSignal = bearishCount == barsRequired and (not requireHigherCloses or lowerCloses)

// Plot signals only on confirmed candles
plotshape(showSignals and bearishSignal and barstate.isconfirmed,
     title="Bearish Reversal",
     style=shape.arrowdown,
     location=location.abovebar,
     color=color.red,
     size=size.small)

plotshape(showSignals and bullishSignal and barstate.isconfirmed,
     title="Bullish Reversal",
     style=shape.arrowup,
     location=location.belowbar,
     color=color.lime,
     size=size.small)

// Alerts
alertcondition(bearishSignal and barstate.isconfirmed,
     title="Bearish Reversal",
     message="Consecutive bullish candles: bearish signal")

alertcondition(bullishSignal and barstate.isconfirmed,
     title="Bullish Reversal",
     message="Consecutive bearish candles: bullish signal")

Add it to TradingView

  1. 1Open any chart on TradingView, then open the Pine Editor.
  2. 2Select everything in the editor and replace it with the copied code.
  3. 3Click Save, then Add to chart.
  4. 4To change the settings, hover over the indicator's name on the chart and click the gear icon.

What it does#

The indicator counts candles that close in the same direction. When a run reaches the length you set (3 by default), it marks a possible reversal:

  • Red arrow above the bar after 3 bullish (green) candles in a row. The move up may be running out.
  • Green arrow below the bar after 3 bearish (red) candles in a row. The move down may be running out.

Arrows only appear once a candle has closed, so they don't flicker on and off while a bar is still forming.

It's a deliberately simple pattern, made for learning and conversion practice. It isn't a finished strategy, so backtest it before you trade it.

Settings#

SettingDefaultWhat it does
Consecutive Bars3How many same-colour candles in a row trigger a signal (2 to 10).
Require Higher ClosesOnA momentum filter. The last candle of the run has to close above each of the closes before it (below them, for a run of red candles). Turn it off to get a signal on every run.
Show ArrowsOnShows or hides the arrows. Alerts still work when the arrows are hidden.

Alerts#

The script has two alert conditions, Bullish Reversal and Bearish Reversal. In TradingView, click Alert, choose 3-Bar Reversal Signals as the condition, then pick the signal you want to be notified about.

From Pine Script to MQL5#

In the video, we convert this indicator to MQL5 with an AI tool, so it runs in MetaTrader 5. It's a good first conversion: the logic is short, but it touches the parts that differ most between the two languages: bar history, drawing arrows on the chart, and alerts.

New to MQL5 indicators? Start with Coding Custom Indicators in MQL5.

Watch the video

Watch on YouTube ↗

More on this topic

Free to use for learning. Not financial advice: test any indicator on a demo account or in a backtest before you trade with it. More on our YouTube channel.