47 lines
2.0 KiB
Plaintext
47 lines
2.0 KiB
Plaintext
//@version=5
|
|
strategy("Sunny Bands Strategy", shorttitle="SunnyB Strategy", overlay=true, margin_long=100, margin_short=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
|
|
|
|
// Strategy Inputs
|
|
length = input.int(50, "DMA Length", group="Strategy Parameters")
|
|
atr_multiplier = input.float(1.5, "ATR Multiplier", group="Strategy Parameters")
|
|
smooth_factor = input.int(2, "Smoothing Factor", group="Strategy Parameters")
|
|
stop_loss_mult = input.float(1.0, "Stop Loss (ATR)", group="Risk Management")
|
|
take_profit_mult = input.float(2.0, "Take Profit (ATR)", group="Risk Management")
|
|
trade_size_pct = input.float(100, "Position Size (% Equity)", step=10, group="Risk Management") / 100
|
|
|
|
// Include original indicator logic
|
|
ema1 = ta.ema(close, length)
|
|
dma = ta.ema(ema1, smooth_factor)
|
|
atr = ta.atr(length)
|
|
upper_band = dma + (atr * atr_multiplier)
|
|
lower_band = dma - (atr * atr_multiplier)
|
|
|
|
// Strategy Calculations
|
|
bullish_signal = ta.crossover(close, lower_band)
|
|
bearish_signal = ta.crossunder(close, upper_band)
|
|
price_at_entry = close
|
|
stop_loss_level = price_at_entry - (atr * stop_loss_mult)
|
|
take_profit_level = price_at_entry + (atr * take_profit_mult)
|
|
|
|
// Strategy Rules
|
|
if bullish_signal
|
|
strategy.entry("Long", strategy.long, qty=strategy.equity * trade_size_pct / close)
|
|
|
|
if strategy.position_size > 0
|
|
strategy.exit("Exit", "Long",
|
|
stop=stop_loss_level,
|
|
limit=take_profit_level)
|
|
|
|
if bearish_signal
|
|
strategy.close("Long")
|
|
|
|
// Visual Elements
|
|
plot(dma, "DMA", color=color.new(#009688, 0))
|
|
plot(upper_band, "Upper Band", color=color.new(#FF5252, 0))
|
|
plot(lower_band, "Lower Band", color=color.new(#4CAF50, 0))
|
|
fill(plot(upper_band), plot(lower_band), color.new(#673AB7, 90), "Band Area")
|
|
|
|
// Plot trading levels
|
|
plot(strategy.position_size > 0 ? stop_loss_level : na, "Stop Loss", color=color.red, style=plot.style_linebr)
|
|
plot(strategy.position_size > 0 ? take_profit_level : na, "Take Profit", color=color.green, style=plot.style_linebr)
|