Code: Select all
//@version=5
indicator("Execution State Machine: Tier-1 Liquidity Node", overlay=true)
// --- System Configuration ---
grp_event = "Event Vector"
eventHour = input.int(14, "Event Hour (0-23)", group=grp_event)
eventMinute = input.int(30, "Event Minute (0-59)", group=grp_event)
eventTz = input.string("America/New_York", "Timezone", group=grp_event)
grp_params = "State Parameters"
killWindow = input.int(10, "Pre-Event Kill Window (Min)", group=grp_params)
coolWindow = input.int(5, "Post-Event Cooldown (Min)", group=grp_params)
// --- Custom Data Structures ---
// Defining a state object to maintain clean architecture
type SystemState
int deltaMinutes
string phaseName
color phaseColor
bool isExecutionLocked
// --- State Computation Engine ---
method evaluateState(int targetHour, int targetMin, int pre, int post) =>
int currTotal = (hour(time, eventTz) * 60) + minute(time, eventTz)
int evTotal = (targetHour * 60) + targetMin
int delta = evTotal - currTotal
if delta < -720 // Handle midnight crossover
delta := delta + 1440
else if delta > 720
delta := delta - 1440
SystemState state = SystemState.new(delta, "NEUTRAL", color.new(color.gray, 80), false)
if delta > 0 and delta <= (pre + 15) and delta > pre
state.phaseName := "WARNING (T-" + str.tostring(delta) + ")"
state.phaseColor := color.new(color.orange, 85)
else if delta > 0 and delta <= pre
state.phaseName := "RESTRICTED (PRE-PRINT)"
state.phaseColor := color.new(color.maroon, 85)
state.isExecutionLocked := true
else if delta <= 0 and delta >= -post
state.phaseName := "RESTRICTED (COOLDOWN)"
state.phaseColor := color.new(color.maroon, 85)
state.isExecutionLocked := true
state
// --- Execution ---
SystemState currentState = evaluateState(eventHour, eventMinute, killWindow, coolWindow)
// Visual enforcement of the state
bgcolor(currentState.phaseColor, title="State Enforcement Background")
// --- API Contract (JSON Webhook) ---
// Formulates a robust payload for consumption by a backend (e.g., C# / REST API)
if currentState.isExecutionLocked and not currentState.isExecutionLocked[1]
string jsonPayload = '{"action": "LOCK_DOWN", ' +
'"asset": "' + syminfo.ticker + '", ' +
'"timestamp": ' + str.tostring(timenow) + ', ' +
'"commands": ["CLOSE_ALL_MARKET", "CANCEL_ALL_PENDING"]}'
alert(jsonPayload, alert.freq_once_per_bar)
if not currentState.isExecutionLocked and currentState.isExecutionLocked[1]
string jsonPayload = '{"action": "UNLOCK", ' +
'"asset": "' + syminfo.ticker + '", ' +
'"timestamp": ' + str.tostring(timenow) + '}'
alert(jsonPayload, alert.freq_once_per_bar)
// --- Telemetry HUD ---
var table telemetry = table.new(position.top_right, 1, 2, border_width=1, border_color=color.rgb(40,40,40))
if barstate.islast
table.cell(telemetry, 0, 0, "EXECUTION ENGINE", text_color=color.rgb(180,180,180), bgcolor=color.rgb(20,20,20), text_size=size.small)
table.cell(telemetry, 0, 1, currentState.phaseName, text_color=color.white, bgcolor=currentState.phaseColor, text_size=size.small)