Download engine/lifecycle.py from abyXsmart/Confluence_Reversal_Engine: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/spaces/abyXsmart/Confluence_Reversal_Engine/resolve/main/engine/lifecycle.py
- Command line
-
hf download hf://spaces/abyXsmart/Confluence_Reversal_Engine/engine/lifecycle.py
-
curl -L -o lifecycle.py https://huggingface.co/spaces/abyXsmart/Confluence_Reversal_Engine/resolve/main/engine/lifecycle.py
2.36 kB
| """ | |
| Signal lifecycle state machine (spec §5). | |
| States: | |
| PENDING — scored & emitted, price hasn't reached the entry zone yet | |
| ACTIVE — price entered the entry zone; stop/targets armed | |
| HIT_TARGET1 / HIT_TARGET2 — take-profit levels reached | |
| HIT_STOP — stop-loss reached | |
| EXPIRED — 24h timeout without entry, or structure invalidated | |
| CANCELLED — manual / auto cancel | |
| `advance()` is a pure transition function the active-signal monitor calls each | |
| poll with the latest price; it returns the (possibly new) state and an outcome | |
| note. Outcome/result_r are persisted for the performance log (spec §7). | |
| """ | |
| from __future__ import annotations | |
| PENDING = "PENDING" | |
| ACTIVE = "ACTIVE" | |
| HIT_TARGET1 = "HIT_TARGET1" | |
| HIT_TARGET2 = "HIT_TARGET2" | |
| HIT_STOP = "HIT_STOP" | |
| EXPIRED = "EXPIRED" | |
| CANCELLED = "CANCELLED" | |
| TERMINAL = {HIT_TARGET2, HIT_STOP, EXPIRED, CANCELLED} | |
| EXPIRY_SEC = 24 * 3600 | |
| def realized_r(direction: str, entry: float, stop: float, exit_price: float) -> float: | |
| """Result in R units (risk multiples).""" | |
| risk = abs(entry - stop) | |
| if risk <= 0: | |
| return 0.0 | |
| move = (exit_price - entry) if direction == "long" else (entry - exit_price) | |
| return round(move / risk, 3) | |
| def advance(state: str, direction: str, price: float, entry: float, stop: float, | |
| target1: float, target2: float, age_sec: float) -> tuple[str, str]: | |
| """Compute next state from current price. Returns (new_state, note).""" | |
| if state in TERMINAL: | |
| return state, "terminal" | |
| long = direction == "long" | |
| if state == PENDING: | |
| # entered the zone? | |
| entered = price <= entry if long else price >= entry | |
| if entered: | |
| return ACTIVE, "entered entry zone" | |
| if age_sec >= EXPIRY_SEC: | |
| return EXPIRED, "no entry within 24h" | |
| return PENDING, "waiting for entry" | |
| if state in (ACTIVE, HIT_TARGET1): | |
| stopped = price <= stop if long else price >= stop | |
| if stopped: | |
| return HIT_STOP, "stop hit" | |
| hit_t2 = price >= target2 if long else price <= target2 | |
| if hit_t2: | |
| return HIT_TARGET2, "target2 hit" | |
| hit_t1 = price >= target1 if long else price <= target1 | |
| if hit_t1 and state == ACTIVE: | |
| return HIT_TARGET1, "target1 hit" | |
| return state, "in trade" | |
| return state, "noop" | |