//@version=5
strategy("Supertrend Strategy with Bollinger Bands, Stop Loss, and Target Profit",
overlay=true, default_qty_type=strategy.fixed, default_qty_value=1)
// DISCLAIMER:
// This Pine Script code is provided for educational and informational purposes
only and should not be considered as financial or investment advice.
// Trading financial instruments, including stocks, futures, and cryptocurrencies,
carries a high level of risk and may not be suitable for all investors.
// Before using this strategy or any other trading strategy, you should carefully
consider your financial situation, investment objectives, and risk tolerance.
// Past performance is not indicative of future results. The developer of this
script is not responsible for any losses or damages that may result from the use of
this script.
// You should perform your own research and seek advice from a licensed financial
advisor before making any trading decisions.
// Use this script at your own risk.
// Supertrend Settings
atrPeriod = input(10, "ATR Length")
factor = input.float(3.0, "Factor", step = 0.01)
stopLossPercent = input.float(1.0, title="Stop Loss (%)", step=0.1) / 100
targetProfitPercent = input.float(1.0, title="Target Profit (%)", step=0.1) / 100
[supertrendValue, direction] = ta.supertrend(factor, atrPeriod)
// Bollinger Bands Settings
bbLength = input(20, title="Bollinger Bands Length")
bbMult = input.float(2.0, title="Bollinger Bands Multiplier", step=0.1)
basis = ta.sma(close, bbLength)
dev = bbMult * ta.stdev(close, bbLength)
upperBand = basis + dev
lowerBand = basis - dev
// Calculate Stop Loss and Target Profit
longStopLoss = strategy.position_avg_price * (1 - stopLossPercent)
longTargetProfit = strategy.position_avg_price * (1 + targetProfitPercent)
shortStopLoss = strategy.position_avg_price * (1 + stopLossPercent)
shortTargetProfit = strategy.position_avg_price * (1 - targetProfitPercent)
// Entry Conditions with Supertrend and Bollinger Bands
if ta.change(direction) < 0 and close < upperBand
strategy.entry("Long", strategy.long, qty=1)
if ta.change(direction) > 0 and close > lowerBand
strategy.entry("Short", strategy.short, qty=1)
// Exit Conditions
if (strategy.position_size > 0) // Long position
strategy.exit("Take Profit/Stop Loss", "Long", stop=longStopLoss,
limit=longTargetProfit)
if (strategy.position_size < 0) // Short position
strategy.exit("Take Profit/Stop Loss", "Short", stop=shortStopLoss,
limit=shortTargetProfit)
// Plotting
plot(direction > 0 ? na : supertrendValue, title="Supertrend Up",
color=color.green, linewidth=2, style=plot.style_line)
plot(direction < 0 ? na : supertrendValue, title="Supertrend Down",
color=color.red, linewidth=2, style=plot.style_line)
plot(upperBand, title="Upper Bollinger Band", color=color.blue, linewidth=1)
plot(lowerBand, title="Lower Bollinger Band", color=color.blue, linewidth=1)
plot(basis, title="Bollinger Bands Basis", color=color.gray, linewidth=1)