Bull Trend Filtered StochRSI strategy in Pine Script
Buys while the Stochastic RSI is above a lower band and the close is above the highest EMA of the highs.
Stochastic RSI · EMA · long only · by SnarkyPuppy · Mozilla Public License 2.0
What is its backtest worth?
A parameter sweep finds the best setting of the Bull Trend Filtered StochRSI strategy. PineTester tells you if it holds up: whether the best result was only picked after the fact, what it makes with fees, what you could have known at the time (a walk-forward on data the pick did not see) and how deep it fell along the way. On the free start, your first own backtest gets the full evaluation; its trade list and result file come with the full report or a plan.
Creates a draft on Binance BTC/USDT 4h over about six and a half years with 0.1% commission per order and 2 ticks slippage; you start it yourself. A backtest uses credits; new accounts receive free credits.
The rules
- Enters long when the smoothed Stochastic RSI %K is above the lower band and the close is above the highest value of an EMA of the highs over the lookback.
- Closes when %K crosses below the lower band.
- Invests the whole account in each trade (100% of equity, set in the code).
What to watch for
- The trend filter keeps it out of falling markets but also buys late in rises; compare with buy & hold.
- Five varied inputs give many similar settings – the typical setting tells more than the top one.
How PineTester judges a backtest, with its sources: how we judge results.
The Pine Script code
The author's code with its licence notice (Mozilla Public License 2.0), with one change marked in the code: the same test conditions for every library strategy in its strategy() line – the whole account per trade (split over the entries where it adds to a position), 0.1% commission per order and 2 ticks slippage; the trading rules are unchanged. Pine Script v5; PineTester converts it to v6 automatically, so results can differ slightly from TradingView.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SnarkyPuppy
// Modified by PineTester (2026-11-14): the same test conditions for every library strategy in strategy(): 100% of equity per entry,
// 0.1% commission per order, 2 ticks slippage, initial capital 10,000. The trading rules are unchanged.
//@version=5
strategy("Filtered Stoch", overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
/////high filter...upper donchian channel of ema... or highest ema for a given amount of candles
filter_ema_len= input(6)
ema_donchian_len = input(30)
ema=ta.highest(ta.ema(high,filter_ema_len),ema_donchian_len)
////////////////////////basic stoch rsi with max val 100 and min val 0.. entry above lowerband preset 35
smoothK = input.int(7, "K", minval=1)
smoothD = input.int(4, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
src = input(close, title="RSI Source")
rsi1 = ta.rsi(src, lengthRSI)
k = math.min(100, math.max(0,ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)))
d = math.max(math.min(ta.sma(k, smoothD),80),35)
plot(k, "K", color=#2962FF)
plot(d, "D", color=#FF6D00)
h0 = hline(80, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
lowerband=input(35)
h11 = hline(lowerband, "Lower Band", color=#787B86)
fill(h0, h11, color=color.rgb(33, 150, 243, 90), title="Background")
ematrend= close>ema ? 1 : 0
bgcolor(ematrend==1?color.rgb(76, 175, 79, 80):na)
longCondition = k>lowerband and ematrend==1
if (longCondition)
strategy.entry("Up", strategy.long)
shortCondition = ta.crossunder(k,lowerband)
if (shortCondition)
strategy.close("Up", shortCondition)
Related strategies
- RSI + EMA – Buys an oversold RSI while a faster moving average is above a slower one.
- Tendency EMA + RSI – Buys a crossover of two short EMAs above a long EMA and takes profit after a set number of bars.
- Omar MMR – A MACD crossover in an uptrend of three EMAs, with a fixed take profit and stop loss.
- RSI Mean Reversion – Buys a dip of the RSI in an uptrend and sells a spike in a downtrend, with a fixed take profit and stop.
- Double Inside Bar & Trend – After two inside bars, a stop order on the side of the Hull average trend enters the breakout.
Open-source script by SnarkyPuppy, shown under the Mozilla Public License 2.0. Not a recommendation; neither the author nor TradingView endorses PineTester. Backtests are hypothetical results on past data, not a forecast.