Bollinger Bands and RSI Short Selling (by Coinrule)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Coinrule
// Works best on 30m, 45m timeframe

//@version=5
strategy("Bollinger Bands and RSI Short Selling",
         overlay=true,
         initial_capital = 1000,
         default_qty_value = 30,
         default_qty_type = strategy.percent_of_equity,
         commission_type=strategy.commission.percent,
         commission_value=0.1)

//Backtest period
timePeriod = time >= timestamp(syminfo.timezone, 2021, 12, 1, 0, 0)
notInTrade = strategy.position_size <= 0

//Bollinger Bands Indicator
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(basis, "Basis", color=#FF6D00, offset = offset)
p1 = plot(upper, "Upper", color=#2962FF, offset = offset)
p2 = plot(lower, "Lower", color=#2962FF, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))


// RSI inputs and calculations
lengthRSI = 14
RSI = ta.rsi(close, lengthRSI)
oversold= input(30)


//Stop Loss and Take Profit for Shorting
Stop_loss= ((input (1))/100)
Take_profit= ((input (7)/100))

shortStopPrice  = strategy.position_avg_price * (1 + Stop_loss)
shortTakeProfit = strategy.position_avg_price * (1 - Take_profit)

//Entry and Exit
strategy.entry(id="short", direction=strategy.short, when=ta.crossover(close, upper) and RSI < 70 and timePeriod and notInTrade)

if (ta.crossover(upper, close) and RSI > 70 and timePeriod)
    strategy.exit(id='close', stop = shortTakeProfit, limit = shortStopPrice)

    
