Skip to content

Instantly share code, notes, and snippets.

@davidzhaodz
Last active January 4, 2021 17:26
Show Gist options
  • Save davidzhaodz/89ead9561faa5ec35c207e0f57dfa378 to your computer and use it in GitHub Desktop.
Save davidzhaodz/89ead9561faa5ec35c207e0f57dfa378 to your computer and use it in GitHub Desktop.
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