Hello.
What formula would produce a portfolio's daily volatility value? Specifically, the volatility number used in contest scoring.
Given that volatility(or lack of volatility) is so important my intention is to constraint it if possible.
My research shows multiple ways of measuring volatility, however it would be ideal to use the Quantopian method, since it is the one we are grade by.
Below is what I have thus far. When placed into one of my algorithms none of the volatility numbers are close to the ones observed in a full backtest.
It would be great if there was a lesson devoted to this topic. How to track vol and methods that can be used to manage it.
Your help would be greatly appreciated.
Edit: Updated the code to a 63 day look-back period(equivalent to 90 calendar days).
def initialize(context):
context.returnsList = []
def before_trading_start(context, data):
returns = context.portfolio.returns
context.returnsList.append(returns)
if len(context.returnsList) >= 64:
df = pd.DataFrame()
df['returns'] = context.returnsList
df['pct_chg'] = df.returns.pct_change()
df['log_rtn'] = np.log(1 + df.pct_chg)
df['vol'] = pd.rolling_std(df.log_rtn, window=63) * (255**0.5)
#df['vol'] = pd.rolling_std(df.log_rtn, window=63) * np.sqrt(252)#This didn't work either
vol = df.vol.iloc[-1]
#This wasn't correct either
#vol = np.std(context.returnsList[-63:], ddof=1) * np.sqrt(252)
record(vol=vol)