I have been reading AFML ( Marcos López de Prado ) and I am having trouble understanding snippet 3.1 which provides the following code:
def getDailyVol(close,span0=100):
# daily vol, reindexed to close
df0=close.index.searchsorted(close.index-pd.Timedelta(days=1))
df0=df0[df0>0]
df0=pd.Series(close.index[df0-1], index=close.index[close.shape[0]-df0.shape[0]:])
df0=close.loc[df0.index]/close.loc[df0.values].values-1 # daily returns
df0=df0.ewm(span=span0).std()
return df0
Could anyone explain what is wrong with computing daily volatility as:
span0 = 100
close.pct_change().ewm(span = span0).std()
The results in the two methods of computation differ as the previous date used in getDailyVol
is different from the day before.
For E-Mini S&P 500 Jun 23, I obtain (blue curve obtained using snippet 3.1 vs. orange curve obtained using pct_change
):
Could anyone please explain?
Thank you!