Code: Select all
//@version=5
strategy("Series Guardian Strategy", overlay=true, calc_on_every_tick=true, initial_capital=10000)
// --- Input Parameters ---
grp_goals = "Series Goals & Risk"
inpSeriesTarget = input.float(12000.0, title="Series Equity Target", group=grp_goals)
inpTrailDrawdown = input.float(2500.0, title="Trailing Drawdown Limit", group=grp_goals)
inpDailyDrawdown = input.float(1000.0, title="Daily Hard Limit", group=grp_goals)
// --- State Variables ---
var float highWaterMark = strategy.equity
var float startOfDayEquity = strategy.equity
var int currentDay = dayofyear(time)
var bool isHalted = false
var string haltReason = ""
// --- Midnight Rollover Check ---
if dayofyear(time) != currentDay
currentDay := dayofyear(time)
startOfDayEquity := strategy.equity
// --- Update High-Water Mark ---
if strategy.equity > highWaterMark
highWaterMark := strategy.equity
// --- Calculate Dynamic Floors ---
dynamicTrailingFloor = highWaterMark - inpTrailDrawdown
dynamicDailyFloor = startOfDayEquity - inpDailyDrawdown
activeFloor = math.max(dynamicTrailingFloor, dynamicDailyFloor)
// --- Guardian Logic (Target & Breach Detection) ---
if not isHalted
// 1. Target Detection
if inpSeriesTarget > 0 and strategy.equity >= inpSeriesTarget
strategy.close_all(comment="SERIES COMPLETE")
isHalted := true
haltReason := "Target Reached: " + str.tostring(strategy.equity, "#.##")
alert("SERIES COMPLETE: " + haltReason, alert.freq_all)
// 2. Breach Detection
else if strategy.equity <= activeFloor and strategy.opentrades > 0
strategy.close_all(comment="FATAL BREACH")
isHalted := true
haltReason := "Floor Breached. Eq: " + str.tostring(strategy.equity, "#.##") + " Flr: " + str.tostring(activeFloor, "#.##")
alert("FATAL ALERT: " + haltReason, alert.freq_all)
// --- Hypothetical Entry Logic (Only execute if series is active) ---
// Replace this with your actual M15/H4 entry conditions
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if longCondition and not isHalted
strategy.entry("Long", strategy.long)
// --- Visual Dashboard ---
var table dashboard = table.new(position.top_right, 2, 5, border_width=1)
if barstate.islast
table.cell(dashboard, 0, 0, "Current Equity:", text_color=color.white, bgcolor=color.gray)
table.cell(dashboard, 1, 0, str.tostring(strategy.equity, "#.##"), text_color=color.white, bgcolor=color.gray)
table.cell(dashboard, 0, 1, "Series Target:", text_color=color.white, bgcolor=color.green)
table.cell(dashboard, 1, 1, str.tostring(inpSeriesTarget, "#.##"), text_color=color.white, bgcolor=color.green)
table.cell(dashboard, 0, 2, "Active Floor:", text_color=color.white, bgcolor=color.red)
table.cell(dashboard, 1, 2, str.tostring(activeFloor, "#.##"), text_color=color.white, bgcolor=color.red)
table.cell(dashboard, 0, 3, "Drawdown Distance:", text_color=color.white, bgcolor=color.orange)
table.cell(dashboard, 1, 3, str.tostring(strategy.equity - activeFloor, "#.##"), text_color=color.white, bgcolor=color.orange)
if isHalted
table.cell(dashboard, 0, 4, "STATUS:", text_color=color.white, bgcolor=color.black)
table.cell(dashboard, 1, 4, "HALTED", text_color=color.red, bgcolor=color.black)