feat: Add composite risk management system with 1% account risk and 7% max stop loss

This commit is contained in:
Bobby (aider) 2025-02-08 09:09:02 -08:00
parent 485ba4418c
commit aa6af3248c

View File

@ -1,57 +1,68 @@
//@version=5 //@version=5
strategy(title="Three ATR EMA Strategy w/ Trailing", shorttitle="3ATR-EMA Trailing", overlay=true, currency=currency.USD, initial_capital=10000, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.1) strategy(title="3ATR EMA + Risk Mgmt", shorttitle="3ATR-RM", overlay=true, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)
// Inputs // Inputs
ema_length = input.int(21, "EMA Length", minval=1) ema_length = input.int(21, "EMA Length")
atr_multiplier = input.float(3.0, "ATR Multiplier", step=0.1) atr_multiplier = input.float(3.0, "ATR Multiplier")
show_bulls = input.bool(true, "Show Bullish Markers") risk_percent = input.float(1.0, "Risk %", step=0.1) // 1% risk per trade
trail_percent = input.float(2.0, "Trailing Stop %", step=0.1) max_stop_pct = input.float(7.0, "Max Stop %", step=0.1) // 7% max price stop
// Calculate EMA and ATR-based bands // Calculations
ema = ta.ema(close, ema_length) ema = ta.ema(close, ema_length)
upper_band = ema + (ta.atr(ema_length) * atr_multiplier) atr = ta.atr(ema_length)
lower_band = ema - (ta.atr(ema_length) * atr_multiplier) upper_band = ema + (atr * atr_multiplier)
lower_band = ema - (atr * atr_multiplier)
// Strategy Conditions // Entry Conditions
bullish_entry = (close < ema) and bullish_entry = (close < ema) and
(close[1] <= lower_band[1]) and (close[1] <= lower_band[1]) and
(close > close[1]) (close > close[1])
// Trailing Stop Logic // Risk Management Variables
var float stop_price = na var float entry_price = na
var float initial_stop = na
var float max_stop_price = na
var float trail_price = na var float trail_price = na
var bool trail_active = false var bool trail_active = false
// Risk-based Position Sizing
position_size() =>
equity = strategy.calc_equity(close)
risk_amount = equity * (risk_percent / 100)
price_risk = entry_price * (max_stop_pct / 100)
strategy.equity / close // Comment: Temporary placeholder for calculation
(risk_amount / price_risk) // Final position size calculation
// Trade Execution
if bullish_entry if bullish_entry
strategy.entry("Long", strategy.long) entry_price := close
trail_price := na // Reset on new entry initial_stop := close * (1 - max_stop_pct/100)
max_stop_price := initial_stop
trail_price := na
trail_active := false trail_active := false
strategy.entry("Long", strategy.long, qty=position_size())
// Stop Management
if strategy.position_size > 0 if strategy.position_size > 0
// Activate trailing when price hits upper band // Update trailing stop
if close >= upper_band trail_price := math.max(high, nz(trail_price, high))
trail_active := true trail_stop = trail_price * 0.98 // 2% trailing
// Update trailing price when active // Active stop is best of: initial 7% stop OR current trailing stop
if trail_active current_stop = math.max(max_stop_price, trail_stop)
trail_price := math.max(high, nz(trail_price, high))
// Calculate stop price (now using declared variable) strategy.exit("Stop Exit", "Long", stop=current_stop)
stop_price := trail_active ? trail_price * (1 - trail_percent/100) : na
// Submit exit order // Plot stops
if trail_active plot(current_stop, "Active Stop", color=color.red, style=plot.style_linebr)
strategy.exit("Trail Exit", "Long", stop=stop_price) plot(max_stop_price, "Max Stop", color=color.gray, style=plot.style_circles)
// Visual elements // Visualization
plot(ema, "EMA", color=color.new(#4A90E2, 0)) plot(ema, "EMA", color=color.new(#4A90E2, 0))
plot(upper_band, "Upper", color=color.new(#F5A623, 0)) plot(upper_band, "Upper", color=color.orange)
plot(lower_band, "Lower", color=color.new(#7ED321, 0)) plot(lower_band, "Lower", color=color.green)
// Plot trailing stop line plotshape(bullish_entry ? low : na,
plot(strategy.position_size > 0 and trail_active ? stop_price : na, "Trailing Stop", color=color.red, style=plot.style_linebr)
plotshape(show_bulls and bullish_entry ? low : na,
style=shape.triangleup, style=shape.triangleup,
location=location.belowbar, location=location.belowbar,
color=color.new(#00FF00, 0), color=color.new(#00FF00, 0),