Bull Trend Filtered StochRSI (BTFS)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SnarkyPuppy

//@version=5
strategy("Filtered Stoch", overlay=false, default_qty_value = 100, default_qty_type=strategy.percent_of_equity)

/////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)
    
