Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save quantra-go-algo/223deb54d15cb5e0b24a12d7c81d39b3 to your computer and use it in GitHub Desktop.
guardrailed-llm-agent — Step 6: From Policy to Position: Sizing with Volatility Targeting (snippet 4)
def build_agent_positions(df_seg, policy, state_lag=None, vol_lag=None, mom_lag=None):
if state_lag is None: state_lag = df_seg['state'].shift(1)
if vol_lag is None: vol_lag = df_seg['vol'].shift(1)
pos = pd.Series(0.0, index=df_seg.index)
for t in df_seg.index:
s = state_lag.loc[t]
if pd.isna(s): continue
rec = policy.get(s, {'action': 'LONG', 'size': 0.5})
base = action_to_base_exposure(rec['action'], rec['size'])
pos.loc[t] = vol_scale(base, vol_lag.loc[t])
return pos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment