Last active
January 4, 2021 17:26
-
-
Save davidzhaodz/89ead9561faa5ec35c207e0f57dfa378 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 add_vertical_barrier(t_events, close, num_days=1): | |
""" | |
:param t_events: (series) series of events (symmetric CUSUM filter) | |
:param close: (series) close prices | |
:param num_days: (int) maximum number of days a trade can be active | |
:return: (series) timestamps of vertical barriers | |
""" | |
t1 = close.index.searchsorted(t_events + pd.Timedelta(days=num_days)) | |
t1 = t1[t1 < close.shape[0]] | |
t1 = pd.Series(close.index[t1], index=t_events[:t1.shape[0]]) # NaNs at end | |
return t1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment