Code: Select all
//@version=5
strategy("Pro Daily Risk Manager [HUD]", overlay=true, calc_on_every_tick=true, initial_capital=10000)
// ==============================================================================
// 1. PARAMETERS
// ==============================================================================
maxLossPct = input.float(3.0, title="Max Daily Loss (%)", minval=0.1, step=0.1, group="Risk Limits")
showHud = input.bool(true, title="Show On-Chart HUD", group="User Interface")
// ==============================================================================
// 2. THE KILL SWITCH (NATIVE PINE SCRIPT ENGINE)
// ==============================================================================
// This single line automatically handles everything. If the script's equity drops
// by the specified percentage from the start of the day, it instantly liquidates
// all open positions, cancels pending orders, and locks out new entries until tomorrow.
strategy.risk.max_intraday_loss(maxLossPct, strategy.percent_of_equity)
// ==============================================================================
// 3. TRACKING LOGIC (FOR THE HUD AND ALERTS)
// ==============================================================================
var float startOfDayEquity = na
var bool isLimitHit = false
// Detect when a new daily session begins
isNewDay = ta.change(time("D")) != 0
// Current Equity = Starting Capital + Realized PnL + Floating PnL
currentEquity = strategy.initial_capital + strategy.netprofit + strategy.openprofit
// Reset tracking variables at the start of each day
if isNewDay
startOfDayEquity := currentEquity
isLimitHit := false
// Calculate the day's performance
dailyPnL = currentEquity - startOfDayEquity
dailyPnLPct = na(startOfDayEquity) or startOfDayEquity == 0 ? 0 : (dailyPnL / startOfDayEquity) * 100
if dailyPnLPct <= -maxLossPct
isLimitHit := true
// ==============================================================================
// 4. ALERTS
// ==============================================================================
// Send a webhook/email/app notification the exact moment the limit is breached
if isLimitHit and not isLimitHit[1]
alert("DAILY LOSS CAP REACHED. All strategy positions closed and locked for the day.", alert.freq_once_per_bar)
// ==============================================================================
// 5. PRO ON-CHART HUD (DASHBOARD)
// ==============================================================================
var table riskTable = table.new(position.top_right, 2, 4, border_width=1, border_color=color.gray)
if showHud and barstate.islast
// Start Balance Row
table.cell(riskTable, 0, 0, "Start Balance:", text_color=color.white, bgcolor=color.new(color.black, 20))
table.cell(riskTable, 1, 0, "$" + str.tostring(startOfDayEquity, "#.##"), text_color=color.white, bgcolor=color.new(color.black, 20))
// Daily PnL Row
table.cell(riskTable, 0, 1, "Daily PnL:", text_color=color.white, bgcolor=color.new(color.black, 20))
table.cell(riskTable, 1, 1, "$" + str.tostring(dailyPnL, "#.##"), text_color=dailyPnL < 0 ? color.red : color.green, bgcolor=color.new(color.black, 20))
// Drawdown Row
table.cell(riskTable, 0, 2, "Drawdown:", text_color=color.white, bgcolor=color.new(color.black, 20))
table.cell(riskTable, 1, 2, str.tostring(dailyPnLPct, "#.##") + "% / -" + str.tostring(maxLossPct) + "%", text_color=dailyPnLPct <= -maxLossPct ? color.red : color.green, bgcolor=color.new(color.black, 20))
// Status Row
table.cell(riskTable, 0, 3, "Status:", text_color=color.white, bgcolor=color.new(color.black, 20))
table.cell(riskTable, 1, 3, isLimitHit ? "LOCKED" : "ACTIVE", text_color=isLimitHit ? color.red : color.green, bgcolor=color.new(color.black, 20))
// ==============================================================================
// 6. YOUR STRATEGY ENTRIES GO HERE
// ==============================================================================
// Example: basic moving average crossover to show how the script executes trades.
// Replace this with your actual edge/scalping logic.
// fastMA = ta.sma(close, 9)
// slowMA = ta.sma(close, 21)
//
// if ta.crossover(fastMA, slowMA)
// strategy.entry("Long", strategy.long)
//
// if ta.crossunder(fastMA, slowMA)
// strategy.close("Long")