def sl_choice_nb(col, from_i, to_i, ts, stop, trailing, first): """`choice_func_nb` that returns the first index of `ts` being below the stop defined at `from_i-1`.""" ts = ts[from_i - 1:to_i, col] stop = stop[from_i - 1:to_i, col] if trailing: # Propagate the maximum value from the entry using expanding max stop = (1 - stop) * generic_nb.expanding_max_1d_nb(ts) # Get the absolute index of the first ts being below that stop exits = from_i + np.flatnonzero(ts[1:] <= stop[1:]) else: exits = from_i + np.flatnonzero(ts[1:] <= (1 - stop[0]) * ts[0]) if first: return exits[:1] return exits
def drawdown_1d_nb(returns): """Drawdown of cumulative returns.""" cum_returns = cum_returns_1d_nb(returns, start_value=100.) max_returns = generic_nb.expanding_max_1d_nb(cum_returns, minp=1) return cum_returns / max_returns - 1