Indicator · Pine Script v6 · On the chart

Reversal Signals

by turnupdigital· code credited to AlgoAlpha

An indicator on the price chart that looks for candle reversal setups over a lookback of 12 candles and requires confirmation within a few further candles, optionally backed by volume above its 20 bar average.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code164
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago12 Aug 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

164 lines of Pine Script v6 from BuySellIndicators/BUYSELLSIGNALS/AlgoAlphaReversalSignal.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

AlgoAlphaReversalSignal.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoAlpha

//@version=6
indicator('Reversal Signals [AlgoAlpha]', 'AlgoAlpha - Reversal Signals', overlay = true)

// Inputs
lookbackPeriod = input.int(defval = 12, title = 'Candle Lookback', tooltip = 'Number of candles to look back for reversal detection', group = 'Reversal Detection Settings')
confirmationPeriod = input.int(defval = 3, title = 'Confirm Within', tooltip = 'Number of candles within which the reversal must be confirmed', group = 'Reversal Detection Settings')
enableVolumeConfirmation = input.bool(defval = true, title = 'Use Volume Confirmation', tooltip = 'Enable or disable volume confirmation for signals', group = 'Reversal Detection Settings')
trendMAPeriod = input.int(defval = 50, title = 'Trend MA Period', tooltip = 'Period for the moving average used in trend detection', group = 'Trend Settings')
trendMAType = input.string(defval = 'EMA', title = 'MA Type', options = ['SMA', 'EMA', 'WMA', 'VWMA'], tooltip = 'Type of moving average to use', group = 'Trend Settings')
maStepPeriod = input.int(defval = 33, title = 'MA Step Period', tooltip = 'Number of bars after which the stepped MA updates', group = 'Trend Settings')
bullColor = input.color(#00ffbb, title = 'Bullish Color', tooltip = 'Color used for bullish signals and trends', group = 'Appearance')
bearColor = input.color(#ff1100, title = 'Bearish Color', tooltip = 'Color used for bearish signals and trends', group = 'Appearance')
fill = input.bool(true, 'Trend Fill', tooltip = 'Enable/disable trend fills', group = 'Appearance')
steps = input.bool(true, 'MA Steps', tooltip = 'Enable/disable MA steps', group = 'Appearance')

// Variables
var int bullCandleScore = 0
var int bearCandleScore = 0

bullSignal = false
bearSignal = false

var bool bullReversalCandidate = false
var bool bearReversalCandidate = false

var float bullReversalLow = 0.0
var float bullReversalHigh = 0.0
var float bearReversalLow = 0.0
var float bearReversalHigh = 0.0

var bool bullSignalConfirmed = false
var bool bearSignalConfirmed = false

var int bullCandleCounter = 0
var int bearCandleCounter = 0

volumeIsHigh = volume > ta.sma(volume, 20)

// Calculate the moving average value based on selected type
ma_current = switch trendMAType
    'SMA' => ta.sma(close, trendMAPeriod)
    'EMA' => ta.ema(close, trendMAPeriod)
    'WMA' => ta.wma(close, trendMAPeriod)
    'VWMA' => ta.vwma(close, trendMAPeriod)

// Update the stepped moving average
var float steppedMAValue = na
var int maLastUpdateBar = 0
var int maDirection = 1

if na(steppedMAValue)
    steppedMAValue := ma_current
    maLastUpdateBar := bar_index
    maLastUpdateBar
else
    if maDirection == 1
        if close < steppedMAValue
            maDirection := -1
            steppedMAValue := ma_current
            maLastUpdateBar := bar_index
            maLastUpdateBar
        else if bar_index - maLastUpdateBar >= maStepPeriod
            newMAValue = ma_current
            steppedMAValue := math.max(steppedMAValue, newMAValue)
            maLastUpdateBar := bar_index
            maLastUpdateBar
    else
        if close > steppedMAValue
            maDirection := 1
            steppedMAValue := ma_current
            maLastUpdateBar := bar_index
            maLastUpdateBar
        else if bar_index - maLastUpdateBar >= maStepPeriod
            newMAValue = ma_current
            steppedMAValue := math.min(steppedMAValue, newMAValue)
            maLastUpdateBar := bar_index
            maLastUpdateBar

// Reset candle scores
bullCandleScore := 0
bearCandleScore := 0

// Loop through the lookback period
for i = 0 to lookbackPeriod - 1 by 1
    if close < low[i]
        bullCandleScore := bullCandleScore + 1
        bullCandleScore
    if close > high[i]
        bearCandleScore := bearCandleScore + 1
        bearCandleScore

// Bearish reversal detection
if bearCandleScore == lookbackPeriod - 1
    bearReversalCandidate := true
    bearReversalLow := low
    bearReversalHigh := high
    bearSignalConfirmed := false
    bearCandleCounter := 0
    bearCandleCounter

if bearReversalCandidate
    bearCandleCounter := bearCandleCounter + 1
    if close > bearReversalHigh
        bearReversalCandidate := false
        bearReversalCandidate

bearCondition = false

if bearReversalCandidate and close < bearReversalLow and not bearSignalConfirmed and bearCandleCounter <= confirmationPeriod + 1
    bearSignalConfirmed := true
    bearCondition := true
    bearCondition

if bearCondition
    if not enableVolumeConfirmation or volumeIsHigh
        bearSignal := true
        bearSignal

// Bullish reversal detection
if bullCandleScore == lookbackPeriod - 1
    bullReversalCandidate := true
    bullReversalLow := low
    bullReversalHigh := high
    bullSignalConfirmed := false
    bullCandleCounter := 0
    bullCandleCounter

if bullReversalCandidate
    bullCandleCounter := bullCandleCounter + 1
    if close < bullReversalLow
        bullReversalCandidate := false
        bullReversalCandidate

bullCondition = false

if bullReversalCandidate and close > bullReversalHigh and not bullSignalConfirmed and bullCandleCounter <= confirmationPeriod + 1
    bullSignalConfirmed := true
    bullCondition := true
    bullCondition

if bullCondition
    if not enableVolumeConfirmation or volumeIsHigh
        bullSignal := true
        bullSignal

// Plot signals
plotshape(bullSignal, title = 'Buy Signal', style = shape.labelup, location = location.belowbar, color = bullColor, size = size.small, text = '𝓡', textcolor = chart.bg_color)
plotshape(bearSignal, title = 'Sell Signal', style = shape.labeldown, location = location.abovebar, color = bearColor, size = size.small, text = '𝓡', textcolor = chart.bg_color)

// Plot moving averages
stma = plot(steppedMAValue, title = 'Stepped MA', color = close > steppedMAValue ? bullColor : bearColor, linewidth = 2, display = steps ? display.all : display.none)
cm = plot((open + close) / 2, title = 'Center Line', display = display.none)

fill(stma, cm, steppedMAValue, (open + close) / 2, steppedMAValue < close ? color.new(bullColor, 70) : na, steppedMAValue < close ? color.new(bullColor, 100) : na, display = fill ? display.all : display.none)
fill(stma, cm, (open + close) / 2, steppedMAValue, steppedMAValue > close ? color.new(bearColor, 100) : na, steppedMAValue > close ? color.new(bearColor, 70) : na, display = fill ? display.all : display.none)

// Alerts
alertcondition(bullSignal, 'Bullish Reversal', 'Bullish Reversal Signal Detected')
alertcondition(bearSignal, 'Bearish Reversal', 'Bearish Reversal Signal Detected')
alertcondition(ta.crossover(close, steppedMAValue), 'Bullish Trend', 'Bullish Trend Shift')
alertcondition(ta.crossunder(close, steppedMAValue), 'Bearish Trend', 'Bearish Trend Shift')

Overview

A trend moving average, selectable as SMA, EMA, WMA or VWMA, is drawn as a stepped line that only updates after a set number of bars or on a trend flip, with optional fill. Bullish and bearish colours are adjustable.

Details

Published by

turnupdigital on GitHub

Code credit

AlgoAlpha, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Candlestick patterns, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Candle Lookbackint12lookbackPeriod
Confirm Withinint3confirmationPeriod
Use Volume ConfirmationbooltrueenableVolumeConfirmation
Trend MA Periodint50trendMAPeriod
MA TypestringEMAtrendMAType
MA Step Periodint33maStepPeriod
Bullish Colorcolor#00ffbbbullColor
Bearish Colorcolor#ff1100bearColor
Trend Fillbooltruefill
MA Stepsbooltruesteps
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

285 stars

Mean Reversion Study is an indicator that identifies trade setups combining exponential moving averages, RSI extremes, and engulfing candlestick patterns. It highlights bullish setups when a bullish engulfing candle forms below the EMA while RSI is oversold, a

Indicator v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Study

Smoothed candles

by be-thomas
65 stars

This indicator draws smoothed candlesticks on the price chart. It applies a simple moving average of a user-set length, defaulting to nine, separately to open, high, low, and close, then plots those four values with plotcandle. Wick color is green when the raw

Indicator v5 Moving averagesCandlestick patterns
Updated 5 days ago · GPL-3.0 GitHub repository of Smoothed candles

Major Buy Setup

by dcaoyuan
55 stars

Major Buy Setup plots a 20-period and a 50-period simple moving average on the price chart. It marks a buy when the current bar is both a bullish engulfing candle and a bald bull candle, defined as a green candle whose upper shadow is no more than five percent

Indicator v5 Moving averagesCandlestick patterns Repaints
Updated 6 months ago · Apache-2.0 GitHub repository of Major Buy Setup

30 min Pivot

by ChartsLector
41 stars

30 min Pivot is an overlay indicator that plots an exponential moving average (EMA) calculated from a specified timeframe (default 30 minutes) and flags specific multi-bar candle setups. It checks the relative distance between price and the higher-timeframe EM

Indicator v5 Moving averagesCandlestick patterns
Updated 3 days ago · None stated GitHub repository of 30 min Pivot

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers