Skip to main content

Stochastic RSI — Pine Script v5 strategy

REVERSALStochasticRSIPine Script v5

This script applies the stochastic oscillator to a 14-period RSI instead of price. It opens a long trade on a %K/%D crossover below 20 and closes it on the opposite cross above 80.

Full code (free to copy)

//@version=5
strategy("Stoch RSI", overlay=true)
rsi1 = ta.rsi(close, 14)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, 14), 3)
d = ta.sma(k, 3)
if (ta.crossover(k, d) and k < 20)
    strategy.entry("Long", strategy.long)
if (ta.crossunder(k, d) and k > 80)
    strategy.close("Long")

How the script works

The first line declares v5 and calls strategy with overlay=true, plotting orders on the price chart. rsi1 = ta.rsi(close, 14) computes a 14-bar RSI on close. That same series is passed three times into ta.stoch with length 14, and the output is smoothed by a 3-period SMA to form %K. d = ta.sma(k, 3) smooths %K again to build %D.

Entry is a single if block. ta.crossover(k, d) is true only on the bar where %K moves from at or below %D to above it, and the block also requires k < 20. When both conditions hold, strategy.entry("Long", strategy.long) sends a long market order. No pyramiding limit is coded and there is no short branch.

Exit is a second if block. ta.crossunder(k, d) fires on the bar where %K drops from at or above %D to below it, and the block requires k > 80. strategy.close("Long") then closes the open long position. The script sets no stop loss, take profit, commission or slippage, so a trade stays open until that cross appears.

Parameters in the code

ParameterValueRole
Strategy declarationstrategy("Stoch RSI", overlay=true)Sets the strategy title shown in the tester and draws orders on the price chart.
RSI length14Lookback of ta.rsi applied to close, producing the series fed to the stochastic.
Stochastic length14Lookback passed to ta.stoch on the RSI series before smoothing.
%K smoothing3ta.sma of the raw stochastic value, giving the %K line.
%D smoothing3ta.sma of %K, giving the %D signal line.
Entry threshold20The long entry also requires k < 20 at the crossover bar.
Exit threshold80The exit also requires k > 80 at the crossunder bar.
Order directionstrategy.longOnly long entries are coded; strategy.close("Long") handles the exit.
Order sizenot specifiedstrategy.entry is called without qty, so the tester default size applies.

Known limits of this rule

Before you trust it: test it on data it never saw

A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: holds, mirage or inconclusive.

Get the free Lab →

FAQ

Does it work on crypto?

The code only uses close and ta.rsi, so it can be applied to any symbol available on your chart. Behaviour differs by market because the thresholds are fixed. You can replay it on a period you have never seen in Strategy Arena Lab, which is free.

How do I add a stop loss?

Add a loss argument to strategy.entry, or call strategy.exit with a stop price inside the exit block. The shipped code closes only on the %K/%D crossunder above 80.

Other free Pine Script v5 strategies

RSI Mean ReversionEMA Crossover 9/21EMA Ribbon 8/21/55Bollinger Band BounceMACD MomentumSuperTrendATR BreakoutVWAP BounceRSI + EMA Combo
TradingView Starter
Choose the account path. Prove the strategy separately.
Your first chart and price alert do not require the Lab. Strategy Arena proof remains required before trusting or exporting a strategy.
Partner attribution is for first-time users and web purchases only. Strategy Arena may earn a commission on eligible purchases; starting with a free account needs no payment.
I have never had an account · Start free I already have an account Check a strategy
More TradingView paths
Evidence-gated Pine review

Educational code on past data. No investment advice; no result is promised.