Stochastic RSI — Pine Script v5 strategy
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
| Parameter | Value | Role |
|---|---|---|
Strategy declaration | strategy("Stoch RSI", overlay=true) | Sets the strategy title shown in the tester and draws orders on the price chart. |
RSI length | 14 | Lookback of ta.rsi applied to close, producing the series fed to the stochastic. |
Stochastic length | 14 | Lookback passed to ta.stoch on the RSI series before smoothing. |
%K smoothing | 3 | ta.sma of the raw stochastic value, giving the %K line. |
%D smoothing | 3 | ta.sma of %K, giving the %D signal line. |
Entry threshold | 20 | The long entry also requires k < 20 at the crossover bar. |
Exit threshold | 80 | The exit also requires k > 80 at the crossunder bar. |
Order direction | strategy.long | Only long entries are coded; strategy.close("Long") handles the exit. |
Order size | not specified | strategy.entry is called without qty, so the tester default size applies. |
Known limits of this rule
- In a range the %K/%D pair crosses repeatedly, so the same thresholds can produce entries that are reversed shortly after.
- In a strong trend the oscillator can stay pinned beyond the 20 or 80 level, which delays the entry or the exit signal.
- The script sets no stop loss, take profit, commission or slippage, so its tester results ignore trading costs and risk control.
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.
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
More TradingView paths
Educational code on past data. No investment advice; no result is promised.