Indicator · Pine Script v4 · On the chart

Mean Reversion Study

by lth-elm

Mean Reversion Study is an indicator that identifies trade setups combining exponential moving averages, RSI extremes, and engulfing candlestick patterns.

View on GitHub

GitHub stars285on the repository
Pine versionv4older, still runs
Lines of code67
Settings6inputs
LicenceNone statedfile: MPL-2.0
Last update3 years ago16 Mar 2023

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

67 lines of Pine Script v4 from pinestrategies/Quick-Mean-Reversion-Study.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Quick-Mean-Reversion-Study.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lth_elm

//@version=4
study(title="Mean reversion study", overlay=true)


// ----- INPUTS -----


// EMA
ema_len = input(200, minval=1, title="EMA Length")
ema = ema(close, ema_len)
plot(ema, title="EMA", color=color.blue)

// RSI
rsi_len = input(14, minval=1, title="RSI Length")
r = rsi(close, rsi_len)

// ATR
tp_sl_mul = input(false,  "──── TP/SL multiplicator")
atr_bars = input( title="ATR Bars", defval=14, minval=1)
stop_mult = input(title=" ▪ Stoploss Multiplicator", type=input.float, defval=3)
tp_mult = input(title=" ▪ Takeprofit Multiplicator", type=input.float, defval=5)
atr = atr(atr_bars)


// ----- CONDITIONS -----


bullishEng = open <= close[1] and close > open[1] and open[1] > close[1] and close < ema and r[1] < 30
bearishEng = open >= close[1] and close < open[1] and open[1] < close[1] and close > ema and r[1] > 70

plotshape(bullishEng, title="Crossover", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.tiny)
plotshape(bearishEng, title="Crossunder", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.tiny)


// ----- MANAGE TP/SL -----


// For long
long_stop_level = close - atr * stop_mult
long_profit_level = close + atr * tp_mult

// For short
short_stop_level = close + atr * stop_mult
short_profit_level = close - atr * tp_mult

// Plot TP/SL

plot(long_stop_level, color=color.red, style=plot.style_linebr, linewidth=1, title="Long SL", display=display.none)
plot(long_profit_level, color=color.green, style=plot.style_linebr, linewidth=1, title="Long TP", display=display.none)
plot(short_stop_level, color=color.red, style=plot.style_linebr, linewidth=1, title="Short SL", display=display.none)
plot(short_profit_level, color=color.green, style=plot.style_linebr, linewidth=1, title="Short TP", display=display.none)

plotshape(bullishEng ? long_stop_level : na, style=shape.xcross, color=color.red, location=location.absolute, size=size.tiny, offset=1)
plotshape(bullishEng ? long_profit_level : na, style=shape.circle, color=color.green, location=location.absolute, size=size.tiny, offset=1)

plotshape(bearishEng ? short_stop_level : na, style=shape.xcross, color=color.red, location=location.absolute, size=size.tiny, offset=1)
plotshape(bearishEng ? short_profit_level : na, style=shape.circle, color=color.green, location=location.absolute, size=size.tiny, offset=1)


// ----- ALERTS -----


alertcondition(bullishEng, "MeanRev Long Alert",  '{"ticker": {{ticker}}, "exchange": {{exchange}}, "action": "buy", "type": "limit", "price": {{close}}, "message": "entry", "long SL": {{plot("Long SL")}}, "long TP": {{plot("Long TP")}}, "passphrase": "abcdefg", "subaccount": "Testing"}')
alertcondition(bearishEng, "MeanRev Short Alert", '{"ticker": {{ticker}}, "exchange": {{exchange}}, "action": "sell", "type": "limit", "price": {{close}}, "message": "entry", "short SL": {{plot("Short SL")}}, "short TP": {{plot("Short TP")}}, "passphrase": "abcdefg", "subaccount": "Testing"}')

Overview

Mean Reversion Study highlights bullish setups when a bullish engulfing candle forms below the EMA while RSI is oversold, and bearish setups when a bearish engulfing pattern appears above the EMA with RSI overbought. The indicator calculates ATR-based stop loss and take profit levels and formats them into JSON alert messages for external webhook bots.

Details

Published by

lth-elm on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Mean reversion, Candlestick patterns, Oscillators & momentum, 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.

Topics

tradingviewdiscord-botwebhookftxbinancebybitapi

Settings

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

SettingTypeDefaultVariable
EMA Lengthint200ema_len
RSI Lengthint14rsi_len
──── TP/SL multiplicatorboolfalsetp_sl_mul
ATR Barsint14atr_bars
 ▪ Stoploss Multiplicatorfloat3stop_mult
 ▪ Takeprofit Multiplicatorfloat5tp_mult
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

This indicator plots trade entry and exit signals on the price chart by combining Bollinger Bands with optional filter conditions. It calculates standard Bollinger Bands along with RSI, Aroon, and moving average filters. Reversal signals trigger when price bre

Indicator v4 Volatility & bandsOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner IND [Alorse]
55 stars

This indicator overlays on the price chart and provides trend and momentum analysis. It calculates and plots three exponential moving averages with fixed periods of 20, 50, and 200 using yellow, orange, and blue colors respectively. The script also computes th

Indicator v6 TrendOscillators & momentum
Updated 6 months ago · Apache-2.0 GitHub repository of Trend & Momentum Analysis
948 stars

The Adaptive RSI Moving Average (ARSIMA) is an overlay indicator that dynamically adjusts its smoothing factor based on the Relative Strength Index (RSI). When the RSI moves toward extreme overbought or oversold conditions, the smoothing factor increases to ma

Indicator v4 Moving averagesOscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive RSI Moving Average

This overlay indicator plots a Supertrend from an ATR length and factor, four exponential moving averages, and a selectable VWAP or TWAP taken from a chosen timeframe on the price chart. It also shows MACD and stochastic with adjustable lengths, smoothing, ver

Indicator v5 Oscillators & momentumMoving averages
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup: Vijay Thakare Option Buying Scalping Setup
318 stars

This indicator plots three moving averages on the price chart alongside colored fills between the lines to visualize trend structure. Internally, it calculates a Stochastic RSI oscillator to detect oversold reversals. When the Stochastic %K line rises above 10

Indicator v5 Moving averagesOscillators & momentum
Updated 2 months ago · MIT GitHub repository of TVT 3X Scale In Out

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