Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save quantra-go-algo/0efb0aec70582bd3921e0ffbcf4058ec to your computer and use it in GitHub Desktop.

Select an option

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)
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