RSI trend pullback
All plugins

Plugin libraryMean reversion

Plugin

RSI trend pullback

Buys short, sharp dips while price is above its 200 bar SMA and sells rallies below it, out when RSI(2) recovers or price crosses the 5 bar SMA.

Strategy templateNo networkDesktop onlyv1.1.0

About

A mean reversion template inside a trend filter, after the RSI(2) pullback idea Larry Connors and Cesar Alvarez published. The 200 bar SMA decides the direction: above it only longs, below it only shorts. A 2 bar RSI at or below 10 marks a short, sharp dip in an uptrend, and the long is entered at the next bar's open; above 90 marks a rally in a downtrend for a short. The trade ends when the RSI recovers above 70 (below 30 for a short) or the close crosses back over the 5 bar SMA, whichever comes first.

The published rules have no stop. This template adds two guards you can widen or switch off: a stop 3 ATR from the entry, which also sizes the position by risk, and an exit after 10 bars held. The published rules are written for daily bars of stocks and indices; on other markets and timeframes every length counts bars.

It exports to MetaTrader 5, cTrader and Rust as it runs here. It keeps its own record of the trade, the fill on the ask or the bid and the stop on the other side, so the expert advisor and the cBot follow the bid and ask of the quotes they see. TradingView's bars carry one price and no bid or ask, so there is no export to it.

Parameters

NameTypeDefaultRange or optionsDescription
trendLengthint200min 10, max 500Bars in the trend SMA: longs only above it, shorts only below it.
rsiLengthint2min 2, max 30Bars in the RSI. 2 in the published rules.
entryLevelfloat10min 1, max 49A long enters when the RSI is at or below this, a short when it is at or above 100 minus this.
exitLevelfloat70min 51, max 99A long exits when the RSI rises above this, a short when it falls below 100 minus this.
exitSmaint5min 0, max 50Bars in the exit SMA: a long exits on a close above it, a short on a close below it. 0 is off.
atrLengthint14min 2, max 100Bars in the ATR the stop is measured in.
stopAtrfloat3min 0.5, max 20The stop's distance from the entry, in ATR.
maxBarsint10min 0, max 200Close the position after it has been held this many bars, counting the bar it opened on. 0 is off.
riskPercentfloat1min 0, max 10Per cent of the balance lost if the stop is hit. 0 trades the run's lot size.
allowShortint1min 0, max 11 also sells rallies in a downtrend, 0 long only.

Use it in a strategy

python# The entry rule in your own strategy:
trend = ctx.ind("sma", length=200)
rsi = ctx.ind("rsi", length=2)
atr = ctx.ind("atr", length=14)
close = ctx.bars(1)[-1].close
if trend is not None and rsi is not None and atr is not None:
    if close > trend and rsi <= 10:
        ctx.signal("LONG", sl_pips=3 * atr / ctx.pip(), risk_percent=1.0)

To run the whole template, install it, switch it on and pick RSI trend pullback under Plugins in the strategy list. The rules were published for daily bars.

What it provides

rsi_trend_pullback/rsi_pullbackStrategyThe strategy, with its parameters: trend SMA, RSI length, entry and exit levels, exit SMA, ATR stop, maximum bars held, the risk per trade and the short side.

When values are known

Every decision is made on a bar's close from that bar and the ones before it, and fills at the next bar's open. The stop is checked from the bar after the entry.

Limits

  • Buying dips means buying into falling prices. In a trend that breaks, the next bars can fall further before the exit; the stop limits that, at the price of being hit on deep dips the published rules would have held through.
  • Trades are short and frequent, so costs matter: spread, commission and the gap between a close and the next open. A test without realistic costs says little.
  • The published research used daily bars of US stocks and indices. Results on other markets or timeframes are not covered by it.
  • The template keeps its own record from the bars, as it did before a strategy could read the engine's position, so it trades as it always has. Without bid and ask on the bars the engine adds a spread to the fill the record cannot see, so on a bar that only just reaches the stop the two can differ; the record then waits for its own exit before the next entry.
  • No result is promised.
OrionQuant