Strategy · Pine Script v4 · On the chart

RSI Mean Reversion Strategy 2021

by Web3Degenerate· code credited to ZenAndTheArtOfTrading / www.pinescriptmastery.com

This strategy enters long positions when the Relative Strength Index drops into oversold territory on a confirmed bar.

View on GitHub

GitHub stars2on the repository
Pine versionv4older, still runs
Lines of code66
Settings6inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago13 Sep 2024

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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

66 lines of Pine Script v4 from advanced-pine-script/16.Bonus-V4-Content/9.3commas_Binance_TradingView_Alerts.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

9.3commas_Binance_TradingView_Alerts.pine
// This demonstrates how you can connect your TradingView strategies & indicators to the 3commas API for auto-trading Crypto with your scripts.

// This particular script is just an example - it's not intended to be used in practice, but maybe it can inspire some ideas for your own trading given its simplicity and the fact that it is decently profitable.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading / www.pinescriptmastery.com
// Last Updated: 17th February 2021
// @version=4
strategy("RSI Mean Reversion Strategy 2021", 
 shorttitle="pinescriptmastery.com", 
 overlay=true, 
 initial_capital=10000, 
 default_qty_value=10000)

// Get user input
rsiLen = input(title="RSI Length", type=input.integer, defval=14)
rsiOS = input(title="RSI OS", type=input.float, defval=30.0)
rsiSell = input(title="RSI Sell", type=input.float, defval=50.0)
slType = input(title="Stop Loss ATR / %", type=input.bool, defval=true)
slMulti = input(title="SL Multiplier", type=input.float, defval=1.0, step=0.1)
minimumProfitPercent = input(title="Minimum Profit %", type=input.float, defval=0.00)

// Get ATR & RSI Value
atr = atr(14)
rsi = rsi(close, rsiLen)

// Determine percentage of open profit
var entry = 0.0
distanceProfit = high - entry
distanceProfitPercent = distanceProfit / entry

// Determine if we have a long entry signal OR a sell position signal
profitSignal = minimumProfitPercent == 0.0 or distanceProfitPercent >= minimumProfitPercent
longSignal = rsi <= rsiOS and barstate.isconfirmed and strategy.position_size == 0 and not na(atr)
exitSignal = profitSignal and barstate.isconfirmed and strategy.position_size != 0 and rsi >= rsiSell

// Get Stop Loss (in pips AND percentage distance)
longStop = lowest(low, 4) - (atr * slMulti)
longStopPercent = close - (close * slMulti)
    
// Save long stop & target prices (used for drawing data to the chart & determining profit)
var longStopSaved = 0.0
var longTargetSaved = 0.0
enterLong = false
if longSignal
    longStopSaved := slType ? longStop : longStopPercent
    enterLong := true
    entry := close

// Enter trades when conditions are met
strategy.entry(id="Long", 
 long=strategy.long, 
 when=enterLong, 
 comment="{\n\"message_type\": \"bot\",\n\"bot_id\": 2695468,\n\"email_token\": \"de096c0b-e774-487f-a54a-677cac799710\",\n\"delay_seconds\": 0\n}")

// Place exit orders (only executed after trades are active)
strategy.exit(id="Long Exit", 
 from_entry="Long", 
 limit=exitSignal ? close : na, 
 stop=longStopSaved, 
 when=strategy.position_size > 0, 
 comment="{\n\"message_type\": \"bot\",\n\"bot_id\": 2695468,\n\"email_token\": \"de096c0b-e774-487f-a54a-677cac799710\",\n\"delay_seconds\": 0,\n\"action\": \"close_at_market_price\"\n}")

// Draw stops & entry price until position is exited
plot(strategy.position_size > 0 ? longStopSaved : na, color=color.red, style=plot.style_linebr)
plot(strategy.position_size > 0 ? entry : na, color=color.blue, style=plot.style_linebr)

Overview

RSI Mean Reversion Strategy 2021 exits when RSI rises back to a target sell level, provided an optional minimum profit percentage threshold is satisfied. At trade entry, a stop loss is set based on either an ATR-adjusted swing low or a percentage offset. Order exit and entry comments include structured JSON parameters designed for 3Commas bot integration, while active entry and stop loss lines plot on the price chart.

Details

Published by

Web3Degenerate on GitHub

Code credit

ZenAndTheArtOfTrading / www.pinescriptmastery.com, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Oscillators & momentum, Mean reversion

Markets

Crypto

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
RSI Lengthint14rsiLen
RSI OSfloat30.0rsiOS
RSI Sellfloat50.0rsiSell
Stop Loss ATR / %booltrueslType
SL Multiplierfloat1.0slMulti
Minimum Profit %float0.00minimumProfitPercent
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

330 stars

Alert BB + RSI [Alorse] is a multi-symbol mean-reversion strategy designed for cryptocurrency trading. It evaluates Bollinger Bands and the Relative Strength Index (RSI) across up to 20 user-defined ticker symbols simultaneously. The strategy enters long posit

Strategy v4 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of Alert BB + RSI [Alorse]
285 stars

Mean Reversion Strategy is an overlay strategy that identifies pullbacks using a long-term EMA, RSI, and candlestick patterns. It generates long signals when a bullish engulfing candle forms below the EMA while RSI is oversold, and short signals when a bearish

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

This strategy draws on the price chart and combines Stochastic RSI, the Money Flow Index, and an exponential moving average to open long or short trades. Longs use the lower oscillator thresholds and shorts use the upper ones, with direction limited to longs,

Strategy v5 Oscillators & momentumMoving averages Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3 Indicator Strategy (StochRSI, MFI & EMA) With Safety Orders
104 stars

This strategy combines Bollinger Bands with a short RSI to fade moves that leave the bands and then return. A long requires the prior bar below the lower band and oversold, then a close back inside the band while RSI is between 30 and 50. Shorts mirror that at

Strategy v5 Oscillators & momentumVolatility & bands
Updated 3 years ago · MIT GitHub repository of Bollinger Bands and RSI Strategy
53 stars

This strategy implements Larry Connors' 2-period RSI mean-reversion algorithm overlaid on the price chart. It filters trades using a long-term Simple Moving Average (SMA), entering long positions when the price trades above the SMA and the short-term RSI drops

Strategy v6 Oscillators & momentumMean reversion
Updated 2 months ago · MPL-2.0 GitHub repository of Larry Connors 2-Period RSI Strategy

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers