0bps

Strategy vault / Mean reversion

RSI Mean Reversion Trading Strategy

This strategy is based on the mean reversion characteristics of the RSI indicator. Overbought and oversold RSI tends to revert back, creating trading opportunities. The strategy identifies overbought/oversold states…

Author
ChaoZhang
Complexity
Intermediate · 66 lines
Parameters
8
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

This strategy is based on the mean reversion characteristics of the RSI indicator. Overbought and oversold RSI tends to revert back, creating trading opportunities. The strategy identifies overbought/oversold states using RSI to establish long/short positions in a systematic way.

Strategy Logic:

  1. Calculate RSI value and set overbought and oversold thresholds, typically 60 and 30.

  2. When RSI crosses down the overbought line, go short.

  3. When RSI crosses up the oversold line, go long.

  4. Long stop loss is entry price * (1 - stop loss %). Short stop loss is entry price * (1 + stop loss %).

  5. If price hits the stop loss, exit the position.

Advantages:

  1. Captures mean reversion opportunities during trend pullbacks using RSI.

  2. Breakout trading allows timely entry at trend reversals.

  3. Stop loss controls single trade loss amount.

Risks:

  1. RSI tends to give false signals. Confirm with other indicators.

  2. Stop loss being too tight causes excessive stops. Consider widening range.

  3. Poor timing of entries may lead to oversized positions.

In summary, the RSI mean reversion strategy trades RSI overextensions. It follows the trend with controlled loss on single trades. But RSI reliability is low. Investors should use it prudently with other confirming indicators, optimized stops, and expect modest long-term returns.

Parameters

NameDefaultWhat it controls
v_input_1timestamp(08 Mar 2021 00:00 -0600)Start Time
v_input_2timestamp(9 Mar 2021 23:59 -0600)Start Time
v_input_314lengthRSI
v_input_4_close0Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_50Strategy: Long/Short | Long Only | Short Only
v_input_65Stop Loss %
v_input_730oversold
v_input_860overbought

Source Pine Script

/*backtest
start: 2022-09-05 00:00:00
end: 2023-09-11 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © relevantLeader16058

//@version=4
strategy(shorttitle='RSI Bot Strategy',title='Quadency Mean Reversion Bot Strategy', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.08)

//Backtest dates
start = input(defval = timestamp("08 Mar 2021 00:00 -0600"), title = "Start Time", type = input.time)
finish = input(defval = timestamp("9 Mar 2021 23:59 -0600"), title = "Start Time", type = input.time)
window()  => true       // create function "within window of time"

// Complete Control over RSI inputs and source price calculations
lengthRSI = input(14, minval=1)
source = input(title="Source", type=input.source, defval=close)
strat = input(title="Strategy", defval="Long/Short", options=["Long Only", "Long/Short", "Short Only"])
strat_val = strat == "Long Only" ? 1 : strat == "Long/Short" ? 0 : -1
stoploss = input(5.00, "Stop Loss %")
oversold= input(30)
overbought= input(60)

// Standard RSI Calculation
RSI = rsi(close, lengthRSI)
stLossLong=(1-(stoploss*.01))
stLossShort=(1+(stoploss*.01))

//Long and Short Strategy Logic
GoLong = crossunder(RSI, oversold) and window()
GoShort = crossover(RSI, overbought) and window()

// Strategy Entry and Exit
if (GoLong)
    if strat_val > -1
        strategy.entry("LONG", strategy.long)
    if strat_val < 1
        strategy.close("SHORT")
    

if (GoShort)
    if strat_val > -1
        strategy.close("LONG")
    if strat_val < 1
        strategy.entry("SHORT", strategy.short)


LongStopLoss = barssince(GoLong)<barssince(GoShort) and crossunder(low, valuewhen(GoLong, close, 0)*stLossLong)

ShortStopLoss = barssince(GoLong)>barssince(GoShort) and crossover(high, valuewhen(GoShort, close, 0)*stLossShort)

if (ShortStopLoss)
    strategy.close("SHORT")
    
if (LongStopLoss)
    strategy.close("LONG")




Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-12. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/RSI-Mean-Reversion-Trading-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open