//@version=5
strategy("Bollinger Bands Breakout Strategy with Risk Management", overlay=true, margin_long=100, margin_short=100)
// User Inputs
// Bollinger Bands parameters
bb_length = input.int(title="BB Length", defval=20, minval=1, tooltip="Length for Bollinger Bands")
bb_std_dev = input.float(title="BB Standard Deviation", defval=2.0, minval=0.1, tooltip="Standard Deviation for Bollinger Bands")
// Risk management parameters
risk_per_trade_percent = input.float(title="Risk per Trade (%)", defval=1.0, minval=0.1, maxval=100, tooltip="Percentage of account equity to risk per trade")
stop_loss_percent = input.float(title="Stop Loss (%)", defval=0.5, minval=0.1, maxval=100, tooltip="Stop loss distance in percentage")
take_profit_percent = input.float(title="Take Profit (%)", defval=1.0, minval=0.1, maxval=100, tooltip="Take profit distance in percentage")
// Calculate Bollinger Bands
basis = ta.sma(close, bb_length)
deviation = bb_std_dev * ta.stdev(close, bb_length)
upper_band = basis + deviation
lower_band = basis - deviation
// Plot Bollinger Bands
plot(basis, color=color.blue, linewidth=1, title="Basis")
plot(upper_band, color=color.green, linewidth=1, title="Upper Band")
plot(lower_band, color=color.red, linewidth=1, title="Lower Band")
// Risk Management Calculations
account_equity = strategy.equity
risk_amount = account_equity * (risk_per_trade_percent / 100)
// Calculate position size based on risk and stop loss distance
stop_loss_long_price = close * (1 - stop_loss_percent / 100)
stop_loss_short_price = close * (1 + stop_loss_percent / 100)
qty_long = risk_amount / (close - stop_loss_long_price)
qty_short = risk_amount / (stop_loss_short_price - close)
// Entry Conditions
enter_long = ta.crossover(close, upper_band)
enter_short = ta.crossunder(close, lower_band)
// Exit Conditions
exit_long = strategy.position_size > 0 and (close <= stop_loss_long_price or close >= close * (1 + take_profit_percent / 100))
exit_short = strategy.position_size < 0 and (close >= stop_loss_short_price or close <= close * (1 - take_profit_percent / 100))
// Execute Trades
if enter_long
strategy.entry("Long Entry", strategy.long, qty=qty_long, comment="Long Entry")
if enter_short
strategy.entry("Short Entry", strategy.short, qty=qty_short, comment="Short Entry")
if exit_long
strategy.close("Long Entry", comment="Exit Long")
if exit_short
strategy.close("Short Entry", comment="Exit Short")
// Plotting Stop Loss and Take Profit Levels
plot(strategy.position_size > 0 ? stop_loss_long_price : na, style=plot.style_circles, color=color.red, title="Long Stop Loss")
plot(strategy.position_size < 0 ? stop_loss_short_price : na, style=plot.style_circles, color=color.red, title="Short Stop Loss")
plot(strategy.position_size > 0 ? close * (1 + take_profit_percent / 100) : na, style=plot.style_circles, color=color.green, title="Long Take Profit")
plot(strategy.position_size < 0 ? close * (1 - take_profit_percent / 100) : na, style=plot.style_circles, color=color.green, title="Short Take Profit")