Skip to content

Instantly share code, notes, and snippets.

@davidzhaodz
Last active January 4, 2021 17:23
Show Gist options
  • Save davidzhaodz/f1056016f553f9463924ad268669d26b to your computer and use it in GitHub Desktop.
Save davidzhaodz/f1056016f553f9463924ad268669d26b to your computer and use it in GitHub Desktop.
def get_daily_vol(close, lookback=100):
"""
:param close: (data frame) Closing prices
:param lookback: (int) lookback period to compute volatility
:return: (series) of daily volatility value
"""
print('Calculating daily volatility for dynamic thresholds')
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=lookback).std()
return df0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment