Created
June 24, 2026 01:29
-
-
Save quantra-go-algo/e8f633a66b63c3c4655e8ef00c1eca3e to your computer and use it in GitHub Desktop.
guardrailed-llm-agent — Step 4: Discretizing into Market States (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
| # Trend: above/below zero | |
| trend_bucket = np.where(feat['trend'] >= 0, 'TREND_UP', 'TREND_DOWN') | |
| # Volatility: above/below rolling median (adaptive threshold) | |
| vol_med = feat['vol'].rolling(252, min_periods=60).median() | |
| vol_bucket = np.where(feat['vol'] > vol_med, 'VOL_HIGH', 'VOL_LOW') | |
| # Z-score: three buckets: oversold / neutral / overbought | |
| z = feat['z'] | |
| z_bucket = np.where(z <= -Z_EDGE, 'Z_LOW', | |
| np.where(z >= Z_EDGE, 'Z_HIGH', 'Z_MID')) | |
| # Combine into one interpretable state string | |
| feat['state'] = [ | |
| f'{trend_bucket[i]}|{vol_bucket[i]}|{z_bucket[i]}' | |
| for i in range(len(feat)) | |
| ] | |
| # Example states: | |
| # 'TREND_UP|VOL_LOW|Z_HIGH' → uptrend, calm market, overbought | |
| # 'TREND_DOWN|VOL_HIGH|Z_LOW' → downtrend, stressed market, oversold |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment