Pine Script v4 · Strategy
Stochastic RSI + WMA + SMA strat
A Pine Script v4 strategy built on RSI (close, 14), SMA or Stoch (3) and SMA (k, 3). 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 commits 100% of equity and pays 0.04 commission. 14 settings; defaults include Swing Lo/Hi Lookback 20, SL Expander 10 and TP Expander 30.
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 + WMA + SMA strat repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Stochastic RSI + WMA + SMA strat do on BTCUSDT 1D with default settings?
- −11.93%
- From 26 Jan 2024 to 21 Oct 2026, over 4 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Stochastic RSI + WMA + SMA strat 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 + WMA + SMA strat use a stop loss?
- Yes
- strategy.exit sets a stop on line 93, 94.
- Does Stochastic RSI + WMA + SMA strat run in Zeal Terminal?
- Yes
- It compiles in 44 ms and runs 1,000 bars in 97 ms, in the browser.
BTCUSDT · 1D · default settings
- HLine
- HLine
- Plot
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
Needs BINANCE:BTCUSDT 60 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Values that depend on it are empty on this chart.
- Net profit
- −11.93%
- −$1,193.01
- Buy and hold
- +102.5%
- same period
- Closed trades
- 4
- 1 won · 3 lost
- Profitable
- 25.0%
- of closed trades
- Profit factor
- 0.62
- gross profit ÷ gross loss
- Max drawdown
- 37.88%
- −$3,788.28
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A short position was still open at the last bar. 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, 5) ignores the settings line 41
Line 41 hard-codes 5 where line 34 uses “Smooth K”. Changing those settings moves one band but not this one.
sma(close, 5)sma(close, smoothK) -
Likely bug
sma(close, 5) ignores the settings line 43
Line 43 hard-codes 5 where line 34 uses “Smooth K”. Changing those settings moves one band but not this one.
sma(close, 5)sma(close, smoothK) -
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
4 settings have no label
In the settings dialog smoothK (line 29), smoothD (line 30), lengthRSI (line 31), lengthStoch (line 32) show an empty name. The labels on this page come from the variable names.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses long =, which v5 renamed line 88
long = on line 88, 90. v5 writes it as direction = strategy.long or strategy.short.
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, sma → ta.sma, stoch → ta.stoch, security → request.security, wma → ta.wma, crossover → ta.crossover, crossunder → ta.crossunder, lowest → ta.lowest, highest → ta.highest, valuewhen → ta.valuewhen, atr → ta.atr.
v4 → v5
VerifiedSame 4 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 15: //@version=4 → //@version=5
- Line 33: rsi → ta.rsi
- Line 34: sma → ta.sma
- Line 34: stoch → ta.stoch
- Line 35: sma → ta.sma
- Line 40: security → request.security
25 more edits
- Line 40: wma → ta.wma
- Line 41: security → request.security
- Line 41: sma → ta.sma
- Line 42: wma → ta.wma
- Line 43: sma → ta.sma
- Line 45: crossover → ta.crossover
- Line 46: crossunder → ta.crossunder
- Line 50: lowest → ta.lowest
- Line 51: highest → ta.highest
- Line 53: valuewhen → ta.valuewhen
- Line 53: valuewhen → ta.valuewhen
- Line 53: atr → ta.atr
- Line 54: valuewhen → ta.valuewhen
- Line 54: valuewhen → ta.valuewhen
- Line 54: atr → ta.atr
- Line 55: valuewhen → ta.valuewhen
- Line 55: valuewhen → ta.valuewhen
- Line 55: atr → ta.atr
- Line 56: valuewhen → ta.valuewhen
- Line 56: valuewhen → ta.valuewhen
- Line 56: atr → ta.atr
- Line 60: valuewhen → ta.valuewhen
- Line 72: valuewhen → ta.valuewhen
- Line 88: long = not i_reverse?true:false → direction = not i_reverse?strategy.long:strategy.short
- Line 90: long = not i_reverse?false:true → direction = not i_reverse?strategy.short:strategy.long
⋯// in the overbought region of the stochastic. Viceversa for longs"-//@version=4+//@version=5strategy("Stoch + WMA + SMA strat", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)⋯lengthStoch = input(14, minval=1)-rsi1 = rsi(src4, lengthRSI)+rsi1 = ta.rsi(src4, lengthRSI)-k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)+k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)-d = sma(k, smoothD)+d = ta.sma(k, smoothD)h0 = hline(80, linestyle=hline.style_dotted)⋯wmalen=input(defval=144, title="WMA Length")-WMA = security(syminfo.tickerid, "60", wma(close, wmalen))+WMA = request.security(syminfo.tickerid, "60", ta.wma(close, wmalen))-SMA = security(syminfo.tickerid, "60", sma(close, 5))+SMA = request.security(syminfo.tickerid, "60", ta.sma(close, 5))-minWMA = wma(close, wmalen)+minWMA = ta.wma(close, wmalen)-minSMA = sma(close, 5)+minSMA = ta.sma(close, 5)//Entry Logic-stobuy = crossover(k, d) and k < stochOS+stobuy = ta.crossover(k, d) and k < stochOS-stosell = crossunder(k, d) and k > stochOB+stosell = ta.crossunder(k, d) and k > stochOBmabuy = minSMA > minWMA⋯//SL & TP Calculations-SwingLow=lowest(i_SwingLookback)+SwingLow=ta.lowest(i_SwingLookback)-SwingHigh=highest(i_SwingLookback)+SwingHigh=ta.highest(i_SwingLookback)bought=strategy.position_size != strategy.position_size[1]-LSL=valuewhen(bought, SwingLow, 0)-((valuewhen(bought, atr(14), 0)/5)*i_SLExpander)+LSL=ta.valuewhen(bought, SwingLow, 0)-((ta.valuewhen(bought, ta.atr(14), 0)/5)*i_SLExpander)-SSL=valuewhen(bought, SwingHigh, 0)+((valuewhen(bought, atr(14), 0)/5)*i_SLExpander)+SSL=ta.valuewhen(bought, SwingHigh, 0)+((ta.valuewhen(bought, ta.atr(14), 0)/5)*i_SLExpander)-lTP=(strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0)))+((valuewhen(bought, atr(14), 0)/5)*i_TPExpander))+lTP=(strategy.position_avg_price + (strategy.position_avg_price-(ta.valuewhen(bought, SwingLow, 0)))+((ta.valuewhen(bought, ta.atr(14), 0)/5)*i_TPExpander))-sTP=(strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price))-((valuewhen(bought, atr(14), 0)/5)*i_TPExpander)+sTP=(strategy.position_avg_price - (ta.valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price))-((ta.valuewhen(bought, ta.atr(14), 0)/5)*i_TPExpander)islong=strategy.position_size > 0⋯//TrailingStop-dif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0))+dif=(ta.valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0)) -strategy.position_avg_price⋯var Ststop = float(na)-Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0 +Sdif=strategy.position_avg_price-(ta.valuewhen(strategy.position_size<0 and strategy.position_size[1]>=0, low,0))⋯if stobuy and mabuy and daymabuy- strategy.entry("long", long=not i_reverse?true:false)+ strategy.entry("long", direction = not i_reverse?strategy.long:strategy.short)if stosell and not mabuy and not daymabuy- strategy.entry("short", long=not i_reverse?false:true)+ strategy.entry("short", direction = not i_reverse?strategy.short:strategy.long)//Exit⋯Same 4 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 18: input() → input.bool()
- Line 19: input() → input.int()
- Line 20: input() → input.int()
- Line 21: input() → input.int()
- Line 22: input() → input.bool()
- Line 23: input() → input.bool()
8 more edits
- Line 25: input() → input.source()
- Line 26: input() → input.int()
- Line 27: input() → input.int()
- Line 29: input() → input.int()
- Line 30: input() → input.int()
- Line 31: input() → input.int()
- Line 32: input() → input.int()
- Line 39: input() → input.int()
⋯//SL & TP Inputs-i_SL=input(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")+i_SL=input.bool(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")-i_SwingLookback=input(20, title="Swing Lo/Hi Lookback")+i_SwingLookback=input.int(20, title="Swing Lo/Hi Lookback")-i_SLExpander=input(defval=10, step=1, title="SL Expander")+i_SLExpander=input.int(defval=10, step=1, title="SL Expander")-i_TPExpander=input(defval=30, step=1, title="TP Expander")+i_TPExpander=input.int(defval=30, step=1, title="TP Expander")-i_reverse=input(false, title="Reverse Trades")+i_reverse=input.bool(false, title="Reverse Trades")-i_TStop =input(false, title="Use Trailing Stop")+i_TStop =input.bool(false, title="Use Trailing Stop")//Strategy Inputs-src4 = input(close, title="RSI Source")+src4 = input.source(close, title="RSI Source")-stochOS=input(defval=20, step=5, title="Stochastics Oversold Level")+stochOS=input.int(defval=20, step=5, title="Stochastics Oversold Level")-stochOB=input(defval=80, step=5, title="Stochastics Overbought Level")+stochOB=input.int(defval=80, step=5, title="Stochastics Overbought Level")//Stoch rsi Calculations-smoothK = input(3, minval=1)+smoothK = input.int(3, minval=1)-smoothD = input(3, minval=1)+smoothD = input.int(3, minval=1)-lengthRSI = input(14, minval=1)+lengthRSI = input.int(14, minval=1)-lengthStoch = input(14, minval=1)+lengthStoch = input.int(14, minval=1)rsi1 = ta.rsi(src4, lengthRSI)⋯//MA-wmalen=input(defval=144, title="WMA Length")+wmalen=input.int(defval=144, title="WMA Length")WMA = request.security(syminfo.tickerid, "60", ta.wma(close, wmalen))⋯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: 4 before, 22 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.
⋯// in the overbought region of the stochastic. Viceversa for longs"-//@version=5+//@version=6strategy("Stoch + WMA + SMA strat", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)⋯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 |
|---|---|---|---|
| Use Swing Lo/Hi Stop Loss & Take Profit | true | bool | exit longexit exit shortexit line 92 plot |
| Swing Lo/Hi Lookback | 20 | int | SwingLow SwingHigh |
| SL Expander | 10 | int · step 1 | LSL SSL |
| TP Expander | 30 | int · step 1 | lTP sTP |
| Reverse Trades | false | bool | entry long entry short |
| Use Trailing Stop | false | bool | line 83 |
| RSI Source | close | source | rsi1 |
| Stochastics Oversold Level | 20 | int · step 5 | stobuy |
| Stochastics Overbought Level | 80 | int · step 5 | stosell |
| Smooth K (no label in the script) | 3 | int · min 1 | k |
| Smooth D (no label in the script) | 3 | int · min 1 | d |
| Length RSI (no label in the script) | 14 | int · min 1 | rsi1 |
| Length Stoch (no label in the script) | 14 | int · min 1 | k |
| WMA Length | 144 | int | WMA minWMA |
What it calculates
- RSI (
rsi,ta.rsiin v5)RSI Source (close), Length RSI (14)rsi1 · line 33 - SMA / Stoch (
sma,ta.smain v5)stoch(rsi1, rsi1, rsi1, lengthStoch), Smooth K (3)k · line 34 - SMA (
sma,ta.smain v5)k, Smooth D (3)d · line 35 - WMA (
wma,ta.wmain v5)close, WMA Length (144)WMA · line 40 - SMA (
sma,ta.smain v5)close, 5SMA · line 41 - WMA (
wma,ta.wmain v5)close, WMA Length (144)minWMA · line 42 - SMA (
sma,ta.smain v5)close, 5minSMA · line 43 - Lowest (
lowest,ta.lowestin v5)Swing Lo/Hi Lookback (20)SwingLow · line 50 - Highest (
highest,ta.highestin v5)Swing Lo/Hi Lookback (20)SwingHigh · line 51 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingLow, 0LSL · line 53 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingHigh, 0SSL · line 54 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingLow, 0lTP · line 55 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingHigh, 0sTP · line 56
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 not i_reverse?true:false | stobuy and mabuy and daymabuy | On | 88 |
| Open short not i_reverse?false:true | stosell and not mabuy and not daymabuy | On | 90 |
| Exit longexit stop and limit | i_SL | On “Use Swing Lo/Hi Stop Loss & Take Profit” | 93 |
| Exit shortexit stop and limit | i_SL | On “Use Swing Lo/Hi Stop Loss & Take Profit” | 94 |
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.
- ClearHigher-timeframe data2 security call(s), none with lookahead on.
- 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
Not run on other markets: the script requests BINANCE:BTCUSDT 60, and that data is not available here (a lower timeframe than the chart; the engine does not evaluate intrabar data). Its results would rest on empty values.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 2 | 0% | −$2,408.82 |
| Sideways market | 2 | 50% | $1,219.33 |
| Falling market | 0 | — | $0.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
31.5%
The 95th percentile of the worst drawdown when the same 4 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 17.4% to 31.5%; the tick is what actually happened, 31.5%. Order luck alone moves it this much.
All 4 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 15 Feb 2024 51,795.20 | 27 Feb 2024 55,957.00 | 12 | −$811.52 |
| short | 8 May 2024 62,312.10 | 29 Oct 2024 73,094.40 | 174 | −$1,597.30 |
| short | 28 Mar 2025 87,232.00 | 25 Apr 2025 95,671.60 | 28 | −$740.51 |
| short | 29 Oct 2025 112,898.00 | 31 Jan 2026 80,510.20 | 94 | $1,959.84 |
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 + WMA + SMA strat this page | — | v4 strategy | −11.9% | 4 | 37.9% |
| Correlation Strategy by tweakerID | lowest highest valuewhen atr rsi sma | v4 strategy | does not run in Zeal Terminal | ||
| Delayed RSI Strategy by tweakerID | lowest highest valuewhen atr rsi | v4 strategy | −40.4% | 7 | 42.2% |
| CCI Scalping Strategy by tweakerID | lowest highest valuewhen atr wma | v4 strategy | −66.2% | 41 | 73.0% |
| Money Flow Index 5 min Strategy by tweakerID | lowest highest valuewhen atr sma | v4 strategy | −6.8% | 18 | 28.4% |
| Double Bollinger Strategy by tweakerID | lowest highest valuewhen atr rsi | v4 strategy | −29.6% | 61 | 50.1% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 44 ms | With a workaround 1,000 bars in 97 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = not i_reverse?true:false, long = not i_reverse?false:true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 88, 90) so the engine trades the way the script says. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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/// © bufirolas// Works well with a wide stop with 20 bars lookback// for the SL level and a 2:1 reward ratio Take Profit .// These parameters can be modified in the Inputs section of the strategy panel.// "an entry signal it's a cross down or up on// the stochastics. if you're in a downtrend// on the hourly time frame you// must also be in a downtrend on the five// minute so the five period has to be below the 144// as long as the five period is still trading below// the 144 period on both the hourly and the five minutes// we are looking for these short signals crosses down// in the overbought region of the stochastic. Viceversa for longs"//@version=4strategy("Stoch + WMA + SMA strat", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)//SL & TP Inputsi_SL=input(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")i_SwingLookback=input(20, title="Swing Lo/Hi Lookback")i_SLExpander=input(defval=10, step=1, title="SL Expander")i_TPExpander=input(defval=30, step=1, title="TP Expander")i_reverse=input(false, title="Reverse Trades")i_TStop =input(false, title="Use Trailing Stop")//Strategy Inputssrc4 = input(close, title="RSI Source")stochOS=input(defval=20, step=5, title="Stochastics Oversold Level")stochOB=input(defval=80, step=5, title="Stochastics Overbought Level")//Stoch rsi CalculationssmoothK = input(3, minval=1)smoothD = input(3, minval=1)lengthRSI = input(14, minval=1)lengthStoch = input(14, minval=1)rsi1 = rsi(src4, lengthRSI)k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)d = sma(k, smoothD)h0 = hline(80, linestyle=hline.style_dotted)h1 = hline(20, linestyle=hline.style_dotted)//MAwmalen=input(defval=144, title="WMA Length")WMA = security(syminfo.tickerid, "60", wma(close, wmalen))SMA = security(syminfo.tickerid, "60", sma(close, 5))minWMA = wma(close, wmalen)minSMA = sma(close, 5)//Entry Logicstobuy = crossover(k, d) and k < stochOSstosell = crossunder(k, d) and k > stochOBmabuy = minSMA > minWMAdaymabuy = SMA > WMA//SL & TP CalculationsSwingLow=lowest(i_SwingLookback)SwingHigh=highest(i_SwingLookback)bought=strategy.position_size != strategy.position_size[1]LSL=valuewhen(bought, SwingLow, 0)-((valuewhen(bought, atr(14), 0)/5)*i_SLExpander)SSL=valuewhen(bought, SwingHigh, 0)+((valuewhen(bought, atr(14), 0)/5)*i_SLExpander)lTP=(strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0)))+((valuewhen(bought, atr(14), 0)/5)*i_TPExpander))sTP=(strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price))-((valuewhen(bought, atr(14), 0)/5)*i_TPExpander)islong=strategy.position_size > 0isshort=strategy.position_size < 0//TrailingStopdif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0)) -strategy.position_avg_pricetrailOffset = strategy.position_avg_price - LSLvar tstop = float(na)if strategy.position_size > 0 tstop := high- trailOffset - dif if tstop<tstop[1] tstop:=tstop[1]else tstop := naStrailOffset = SSL - strategy.position_avg_pricevar Ststop = float(na)Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0 and strategy.position_size[1]>=0, low,0))if strategy.position_size < 0 Ststop := low+ StrailOffset + Sdif if Ststop>Ststop[1] Ststop:=Ststop[1]else Ststop := na //Stop SelectorSL= islong ? LSL : isshort ? SSL : naif i_TStop SL:= islong ? tstop : isshort ? Ststop : naTP= islong ? lTP : isshort ? sTP : na//Entriesif stobuy and mabuy and daymabuy strategy.entry("long", long=not i_reverse?true:false)if stosell and not mabuy and not daymabuy strategy.entry("short", long=not i_reverse?false:true)//Exitif i_SL strategy.exit("longexit", "long", stop=SL, limit=TP) strategy.exit("shortexit", "short", stop=SL, limit=TP)//Plotsplot(i_SL ? SL : na, color=color.red, style=plot.style_cross)plot(i_SL ? TP : na, color=color.green, style=plot.style_cross)plot(minWMA)plot(minSMA, color=color.green)