I want to record my algorithm's exposure to risk factors each day, like exposure to momentum risk factor as the backtest runs. Right now, my record_vars function looks like this...but it doesn't work. Says risk_exposures is undefined.
def record_vars(context, data):
rolling_mean_risk_exposures = risk_exposures.rolling(63, axis=0).mean()[62:].fillna(0)
absolute_mean_style_exposure = rolling_mean_risk_exposures[style].abs()
algo.record(num_positions=len(context.portfolio.positions))
algo.record(style_exposure=context.absolute_mean_style_exposure)