Pine Script v4 · Strategy
Stochastic RSI Strategy (with SMA and VWAP Filters)
A Pine Script v4 strategy built on RSI (close, 14), Highest (rsiValue, 14) and Lowest (rsiValue, 14). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 4 settings; defaults include Length 14, Source close and K 3.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Stochastic RSI Strategy (with SMA and VWAP Filters) repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 1 of 1 trades.
- How did Stochastic RSI Strategy (with SMA and VWAP Filters) do on BTCUSDT 1D with default settings?
- −0.00%
- From 26 Jan 2024 to 21 Oct 2026, over 3 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Stochastic RSI Strategy (with SMA and VWAP Filters) be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does Stochastic RSI Strategy (with SMA and VWAP Filters) use a stop loss?
- Yes
- strategy.exit sets a stop on line 33, 36.
- Does Stochastic RSI Strategy (with SMA and VWAP Filters) run in Zeal Terminal?
- Yes
- It compiles in 10 ms and runs 1,000 bars in 33 ms, in the browser.
BTCUSDT · 1D · default settings
- Highest
- Lowest
- SMA
- SMA
- SMA
- VWAP
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −0.00%
- −$0.14
- Buy and hold
- +102.5%
- same period
- Closed trades
- 3
- 1 won · 2 lost
- Profitable
- 33.3%
- of closed trades
- Profit factor
- 0.64
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- −$0.39
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Likely bug
sma(close, 9) ignores the settings line 16
Line 16 hard-codes 9 where line 14 uses “D”. Changing those settings moves one band but not this one.
sma(close, 9)sma(close, smoothD) -
Likely bug
sma(close, 21) ignores the settings line 17
Line 17 hard-codes 21 where line 14 uses “D”. Changing those settings moves one band but not this one.
sma(close, 21)sma(close, smoothD) -
Check
1 setting does nothing
“K” is declared but never read.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames rsi → ta.rsi, highest → ta.highest, lowest → ta.lowest, sma → ta.sma, vwap → ta.vwap, crossover → ta.crossover, crossunder → ta.crossunder.
v4 → v5
VerifiedSame 3 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 10: rsi → ta.rsi
- Line 11: highest → ta.highest
- Line 12: lowest → ta.lowest
- Line 14: sma → ta.sma
- Line 16: sma → ta.sma
4 more edits
- Line 17: sma → ta.sma
- Line 21: vwap → ta.vwap
- Line 23: crossover → ta.crossover
- Line 24: crossunder → ta.crossunder
⋯// © thedoggwalker-//@version=4+//@version=5strategy("Stochastic RSI Strategy", overlay=true)⋯smoothD = input(3, title="D")-rsiValue = rsi(src, length)+rsiValue = ta.rsi(src, length)-highestRSI = highest(rsiValue, length)+highestRSI = ta.highest(rsiValue, length)-lowestRSI = lowest(rsiValue, length)+lowestRSI = ta.lowest(rsiValue, length)k = (rsiValue - lowestRSI) / (highestRSI - lowestRSI) * 100-d = sma(k, smoothD)+d = ta.sma(k, smoothD)// Moving averages-maShort = sma(close, 9)+maShort = ta.sma(close, 9)-maLong = sma(close, 21)+maLong = ta.sma(close, 21)// Spread between moving averages⋯// VWAP-vwapValue = vwap(hlc3)+vwapValue = ta.vwap(hlc3)// Entry conditions-longCondition = crossover(k, 30) and spread > 0 and close < vwapValue+longCondition = ta.crossover(k, 30) and spread > 0 and close < vwapValue-shortCondition = crossunder(k, 70) and spread < 0 and close > vwapValue+shortCondition = ta.crossunder(k, 70) and spread < 0 and close > vwapValue// Entry orders⋯Same 3 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 7: input() → input.source()
- Line 8: input() → input.int()
- Line 9: input() → input.int()
⋯// Stochastic RSI-length = input(14, title="Length")+length = input.int(14, title="Length")-src = input(close, title="Source")+src = input.source(close, title="Source")-smoothK = input(3, title="K")+smoothK = input.int(3, title="K")-smoothD = input(3, title="D")+smoothD = input.int(3, title="D")rsiValue = ta.rsi(src, length)⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 3 before, 3 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © thedoggwalker-//@version=5+//@version=6strategy("Stochastic RSI Strategy", overlay=true)⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Length | 14 | int | rsiValue highestRSI lowestRSI |
| Source | close | source | rsiValue |
| K | 3 | int | unused |
| D | 3 | int | d |
What it calculates
- RSI (
rsi,ta.rsiin v5)Source (close), Length (14)rsiValue · line 10 - Highest (
highest,ta.highestin v5)rsiValue, Length (14)highestRSI · line 11 - Lowest (
lowest,ta.lowestin v5)rsiValue, Length (14)lowestRSI · line 12 - SMA (
sma,ta.smain v5)k, D (3)d · line 14 - SMA (
sma,ta.smain v5)close, 9maShort · line 16 - SMA (
sma,ta.smain v5)close, 21maLong · line 17 - VWAP (
vwap,ta.vwapin v5)hlc3vwapValue · line 21
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | (longCondition) | On | 27 |
| Open Short short | (shortCondition) | On | 29 |
| Exit Exit Long stop and limit | every bar | On | 33 |
| Exit Exit Short stop and limit | every bar | On | 36 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −0.0% | +102.5% | 3 | 33% | 0.64 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.0% | +5.5% | 5 | 40% | 0.85 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.0% | +9.8% | 5 | 80% | 4.86 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.0% | +8.4% | 7 | 43% | 0.91 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 2 | 100% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 10 | 60% | 1.88 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 3 | 33% | 0.52 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 4 | 75% | 3.75 | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 1 | 0% | −$0.20 |
| Sideways market | 1 | 0% | −$0.19 |
| Falling market | 1 | 100% | $0.25 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
0.0%
The 95th percentile of the worst drawdown when the same 3 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.0% to 0.0%; the tick is what actually happened, 0.0%. Order luck alone moves it this much.
All 3 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 11 Mar 2024 68,955.90 | 11 Mar 2024 68,955.70 | 0 | −$0.20 |
| long | 4 Jun 2024 68,809.90 | 4 Jun 2024 68,809.70 | 0 | −$0.19 |
| short | 20 Feb 2026 67,003.70 | 20 Feb 2026 67,003.50 | 0 | $0.25 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Stochastic RSI Strategy (with SMA and VWAP Filters) this page | — | v4 strategy | −0.0% | 3 | 0.0% |
| Stochastic RSI by Zeal Terminal | rsi highest lowest sma | v5 indicator | indicator | ||
| sma RSI & sudden buy and sell Strategy v1 by samwillington | rsi highest lowest sma | v5 strategy | +7.6% | 15 | 3.7% |
| PlanB Quant Investing 101 v2 by fillippone | rsi highest lowest | v4 strategy | No trades on this market | ||
| Bull Trend Filtered StochRSI (BTFS) by SnarkyPuppy | highest rsi sma | v5 strategy | +36.9% | 21 | 21.5% |
| ChBrkOutStrategySMA by anshuanshu333 | sma highest lowest | v4 strategy | −4.5% | 42 | 20.9% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | Yes 1,000 bars in 33 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © thedoggwalker//@version=4strategy("Stochastic RSI Strategy", overlay=true)// Stochastic RSIlength = input(14, title="Length")src = input(close, title="Source")smoothK = input(3, title="K")smoothD = input(3, title="D")rsiValue = rsi(src, length)highestRSI = highest(rsiValue, length)lowestRSI = lowest(rsiValue, length)k = (rsiValue - lowestRSI) / (highestRSI - lowestRSI) * 100d = sma(k, smoothD)// Moving averagesmaShort = sma(close, 9)maLong = sma(close, 21)// Spread between moving averagesspread = maShort - maLong// VWAPvwapValue = vwap(hlc3)// Entry conditionslongCondition = crossover(k, 30) and spread > 0 and close < vwapValueshortCondition = crossunder(k, 70) and spread < 0 and close > vwapValue// Entry ordersif (longCondition) strategy.entry("Long", strategy.long)if (shortCondition) strategy.entry("Short", strategy.short)// Exit orderslongStopLoss = close - 20 * syminfo.minticklongTakeProfit = close + 25 * syminfo.mintickstrategy.exit("Exit Long", "Long", stop=longStopLoss, limit=longTakeProfit)shortStopLoss = close + 20 * syminfo.mintickshortTakeProfit = close - 25 * syminfo.mintickstrategy.exit("Exit Short", "Short", stop=shortStopLoss, limit=shortTakeProfit)