Created
June 24, 2026 01:29
-
-
Save quantra-go-algo/0efb0aec70582bd3921e0ffbcf4058ec to your computer and use it in GitHub Desktop.
guardrailed-llm-agent — Step 7: Hard Guardrails: When the LLM Isn't Enough (snippet 1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| flat_days = 0 # counts consecutive days at zero exposure | |
| for t in df_seg.index: | |
| dd = equity / peak - 1.0 | |
| target = float(proposed_pos.loc[t]) | |
| if cooldown > 0: | |
| target = 0.0 | |
| cooldown -= 1 | |
| else: | |
| # KEY FIX: after MAX_FLAT_DAYS, force re-entry at full size. | |
| # Without this, a frozen equity curve never recovers its drawdown | |
| # and the DD check keeps firing indefinitely. | |
| if flat_days >= MAX_FLAT_DAYS: | |
| target = target * REENTRY_SIZE # come back at full size so equity recovers the drawdown faster | |
| else: | |
| if realized_vol > vol_stop: | |
| target = 0.0 | |
| if dd < -dd_limit: | |
| target = 0.0 | |
| # Track consecutive flat days | |
| flat_days = flat_days + 1 if target == 0.0 else 0 | |
| # Update equity with today's actual position | |
| equity *= math.exp(target * ret - TC * abs(target - prev_target)) | |
| peak = max(peak, equity) | |
| prev_target = target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment