Last active
January 4, 2021 17:23
-
-
Save davidzhaodz/f1056016f553f9463924ad268669d26b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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