Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (4)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOctRSI$100,000$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference.
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

Verified

Same 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⋯

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
Length14intrsiValue highestRSI lowestRSI
SourceclosesourcersiValue
K3intunused
D3intd

What it calculates

  • RSI (rsi, ta.rsi in v5)Source (close), Length (14)rsiValue · line 10
  • Highest (highest, ta.highest in v5)rsiValue, Length (14)highestRSI · line 11
  • Lowest (lowest, ta.lowest in v5)rsiValue, Length (14)lowestRSI · line 12
  • SMA (sma, ta.sma in v5)k, D (3)d · line 14
  • SMA (sma, ta.sma in v5)close, 9maShort · line 16
  • SMA (sma, ta.sma in v5)close, 21maLong · line 17
  • VWAP (vwap, ta.vwap in 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.

closecloseprice datalengthLengthsetting · 14smoothDDsetting · 3hlc3hlc3price datahlc3 · volumehlc3 · volumeprice datarsiValueRSI→ rsiValuehighestRSIHighest→ highestRSIlowestRSILowest→ lowestRSIdSMA→ dmaShortSMA→ maShortmaLongSMA→ maLongvwapValueVWAP→ vwapValuekkvaluespreadspreadvaluelongStopLoss, longTakeProfit, shortStopLoss, shortTakeProfitlongStopLoss · longTake…4 valueslongConditionlongConditionconditionshortConditionshortConditionconditionentry:LongOpen Longstrategy.entryentry:ShortOpen Shortstrategy.entryexit:Exit Longexit Exit Longstrategy.exitexit:Exit Shortexit Exit Shortstrategy.exit

Order rules

OrderFires whenBy defaultLine
Open Long long(longCondition)On27
Open Short short(shortCondition)On29
Exit Exit Long stop and limitevery barOn33
Exit Exit Short stop and limitevery barOn36

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.

1/1trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D−0.0%+102.5%333%0.640.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.0%+5.5%540%0.850.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+0.0%+9.8%580%4.860.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.0%+8.4%743%0.910.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.0%+16.7%2100%—0.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%1060%1.880.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.0%+148.3%333%0.520.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%475%3.750.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 barsTradesWonP&L
Rising market10%−$0.20
Sideways market10%−$0.19
Falling market1100%$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.

0%1%
All 3 trades
SideEntryExitBarsP&L
long11 Mar 2024 68,955.9011 Mar 2024 68,955.700−$0.20
long4 Jun 2024 68,809.904 Jun 2024 68,809.700−$0.19
short20 Feb 2026 67,003.7020 Feb 2026 67,003.500$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.

ScriptSharesTypeNetTradesMax drawdown
Stochastic RSI Strategy (with SMA and VWAP Filters) this page—v4 strategy−0.0%30.0%
Stochastic RSI by Zeal Terminalrsi highest lowest smav5 indicatorindicator
sma RSI & sudden buy and sell Strategy v1 by samwillingtonrsi highest lowest smav5 strategy+7.6%153.7%
PlanB Quant Investing 101 v2 by fillipponersi highest lowestv4 strategyNo trades on this market
Bull Trend Filtered StochRSI (BTFS) by SnarkyPuppyhighest rsi smav5 strategy+36.9%2121.5%
ChBrkOutStrategySMA by anshuanshu333sma highest lowestv4 strategy−4.5%4220.9%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 10 msYes 1,000 bars in 33 msNot measured
PineTSNo 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)