def rolling_cond_value_at_risk_nb(returns, window, minp, cutoff=0.05): """Rolling version of `cond_value_at_risk_nb`.""" def _apply_func_nb(i, col, _returns, _cutoff): return cond_value_at_risk_1d_nb(_returns, _cutoff) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, cutoff)
def rolling_apply(self, window, apply_func_nb, *args, on_matrix=False): """See `vectorbt.generic.nb.rolling_apply_nb` and `vectorbt.generic.nb.rolling_apply_matrix_nb` for `on_matrix=True`. Example: ```python-repl >>> mean_nb = njit(lambda col, i, a: np.nanmean(a)) >>> print(df.vbt.rolling_apply(3, mean_nb)) a b c 2020-01-01 1.0 5.0 1.000000 2020-01-02 1.5 4.5 1.500000 2020-01-03 2.0 4.0 2.000000 2020-01-04 3.0 3.0 2.333333 2020-01-05 4.0 2.0 2.000000 >>> mean_matrix_nb = njit(lambda i, a: np.nanmean(a)) >>> print(df.vbt.rolling_apply(3, mean_matrix_nb, on_matrix=True)) a b c 2020-01-01 2.333333 2.333333 2.333333 2020-01-02 2.500000 2.500000 2.500000 2020-01-03 2.666667 2.666667 2.666667 2020-01-04 2.777778 2.777778 2.777778 2020-01-05 2.666667 2.666667 2.666667 ```""" checks.assert_numba_func(apply_func_nb) if on_matrix: result = nb.rolling_apply_matrix_nb(self.to_2d_array(), window, apply_func_nb, *args) else: result = nb.rolling_apply_nb(self.to_2d_array(), window, apply_func_nb, *args) return self.wrap(result)
def rolling_annualized_return_nb(returns, window, minp, ann_factor): """Rolling version of `annualized_return_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor): return annualized_return_1d_nb(_returns, _ann_factor) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor)
def rolling_cum_returns_final_nb(returns, window, minp, start_value=0.): """Rolling version of `cum_returns_final_nb`.""" def _apply_func_nb(i, col, _returns, _start_value): return cum_returns_final_1d_nb(_returns, _start_value) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, start_value)
def rolling_calmar_ratio_nb(returns, window, minp, ann_factor): """Rolling version of `calmar_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor): return calmar_ratio_1d_nb(_returns, _ann_factor) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor)
def rolling_tail_ratio_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int]) -> tp.Array2d: """Rolling version of `tail_ratio_nb`.""" def _apply_func_nb(i, col, _returns): return tail_ratio_1d_nb(_returns) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb)
def rolling_beta_nb(returns, window, minp, benchmark_rets): """Rolling version of `beta_nb`.""" def _apply_func_nb(i, col, _returns, _benchmark_rets): return beta_1d_nb(_returns, _benchmark_rets[i + 1 - len(_returns):i + 1, col]) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, benchmark_rets)
def rolling_beta_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], benchmark_rets: tp.Array2d) -> tp.Array2d: """Rolling version of `beta_nb`.""" def _apply_func_nb(i, col, _returns, _benchmark_rets): return beta_1d_nb(_returns, _benchmark_rets[i + 1 - len(_returns):i + 1, col]) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, benchmark_rets)
def rolling_calmar_ratio_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], ann_factor: float) -> tp.Array2d: """Rolling version of `calmar_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor): return calmar_ratio_1d_nb(_returns, _ann_factor) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor)
def rolling_down_capture_nb(returns, window, minp, benchmark_rets, ann_factor): """Rolling version of `down_capture_nb`.""" def _apply_func_nb(i, col, _returns, _benchmark_rets, _ann_factor): return down_capture_1d_nb( _returns, _benchmark_rets[i + 1 - len(_returns):i + 1, col], _ann_factor) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, benchmark_rets, ann_factor)
def rolling_cond_value_at_risk_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], cutoff: float = 0.05) -> tp.Array2d: """Rolling version of `cond_value_at_risk_nb`.""" def _apply_func_nb(i, col, _returns, _cutoff): return cond_value_at_risk_1d_nb(_returns, _cutoff) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, cutoff)
def rolling_cum_returns_final_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], start_value: float = 0.) -> tp.Array2d: """Rolling version of `cum_returns_final_nb`.""" def _apply_func_nb(i, col, _returns, _start_value): return cum_returns_final_1d_nb(_returns, _start_value) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, start_value)
def rolling_sortino_ratio_nb(returns, window, minp, ann_factor, required_return=0.): """Rolling version of `sortino_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor, _required_return): return sortino_ratio_1d_nb(_returns, _ann_factor, _required_return) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor, required_return)
def rolling_downside_risk_nb(returns, window, minp, ann_factor, required_return=0.): """Rolling version of `downside_risk_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor, _required_return): return downside_risk_1d_nb(_returns, _ann_factor, _required_return) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor, required_return)
def rolling_down_capture_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], benchmark_rets: tp.Array2d, ann_factor: float) -> tp.Array2d: """Rolling version of `down_capture_nb`.""" def _apply_func_nb(i, col, _returns, _benchmark_rets, _ann_factor): return down_capture_1d_nb( _returns, _benchmark_rets[i + 1 - len(_returns):i + 1, col], _ann_factor) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, benchmark_rets, ann_factor)
def rolling_sortino_ratio_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], ann_factor: float, required_return: float = 0.) -> tp.Array2d: """Rolling version of `sortino_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor, _required_return): return sortino_ratio_1d_nb(_returns, _ann_factor, _required_return) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor, required_return)
def rolling_information_ratio_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], benchmark_rets: tp.Array2d, ddof: int = 1) -> tp.Array2d: """Rolling version of `information_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _benchmark_rets, _ddof): return information_ratio_1d_nb( _returns, _benchmark_rets[i + 1 - len(_returns):i + 1, col], _ddof) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, benchmark_rets, ddof)
def rolling_sharpe_ratio_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], ann_factor: float, risk_free: float = 0., ddof: int = 1) -> tp.Array2d: """Rolling version of `sharpe_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor, _risk_free, _ddof): return sharpe_ratio_1d_nb(_returns, _ann_factor, _risk_free, _ddof) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor, risk_free, ddof)
def rolling_sharpe_ratio_nb(returns, window, minp, ann_factor, risk_free=0., ddof=1): """Rolling version of `sharpe_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor, _risk_free, _ddof): return sharpe_ratio_1d_nb(_returns, _ann_factor, _risk_free, _ddof) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor, risk_free, ddof)
def rolling_information_ratio_nb(returns, window, minp, benchmark_rets, ddof=1): """Rolling version of `information_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _benchmark_rets, _ddof): return information_ratio_1d_nb( _returns, _benchmark_rets[i + 1 - len(_returns):i + 1, col], _ddof) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, benchmark_rets, ddof)
def rolling_annualized_volatility_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], ann_factor: float, levy_alpha: float = 2.0, ddof: int = 1) -> tp.Array2d: """Rolling version of `annualized_volatility_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor, _levy_alpha, _ddof): return annualized_volatility_1d_nb(_returns, _ann_factor, _levy_alpha, _ddof) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor, levy_alpha, ddof)
def rolling_alpha_nb(returns: tp.Array2d, window: int, minp: tp.Optional[int], benchmark_rets: tp.Array2d, ann_factor: float, risk_free: float = 0.) -> tp.Array2d: """Rolling version of `alpha_nb`.""" def _apply_func_nb(i, col, _returns, _benchmark_rets, _ann_factor, _risk_free): return alpha_1d_nb(_returns, _benchmark_rets[i + 1 - len(_returns):i + 1, col], _ann_factor, _risk_free) return generic_nb.rolling_apply_nb( returns, window, minp, _apply_func_nb, benchmark_rets, ann_factor, risk_free)
def rolling_annualized_volatility_nb(returns, window, minp, ann_factor, levy_alpha=2.0, ddof=1): """Rolling version of `annualized_volatility_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor, _levy_alpha, _ddof): return annualized_volatility_1d_nb(_returns, _ann_factor, _levy_alpha, _ddof) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor, levy_alpha, ddof)
def rolling_omega_ratio_nb(returns, window, minp, ann_factor, risk_free=0., required_return=0.): """Rolling version of `omega_ratio_nb`.""" def _apply_func_nb(i, col, _returns, _ann_factor, _risk_free, _required_return): return omega_ratio_1d_nb(_returns, _ann_factor, _risk_free, _required_return) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, ann_factor, risk_free, required_return)
def rolling_alpha_nb(returns, window, minp, benchmark_rets, ann_factor, risk_free=0.): """Rolling version of `alpha_nb`.""" def _apply_func_nb(i, col, _returns, _benchmark_rets, _ann_factor, _risk_free): return alpha_1d_nb(_returns, _benchmark_rets[i + 1 - len(_returns):i + 1, col], _ann_factor, _risk_free) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb, benchmark_rets, ann_factor, risk_free)
def rolling_tail_ratio_nb(returns, window, minp): """Rolling version of `tail_ratio_nb`.""" def _apply_func_nb(i, col, _returns): return tail_ratio_1d_nb(_returns) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb)
def rolling_max_drawdown_nb(returns, window, minp): """Rolling version of `max_drawdown_nb`.""" def _apply_func_nb(i, col, _returns): return max_drawdown_1d_nb(_returns) return generic_nb.rolling_apply_nb(returns, window, minp, _apply_func_nb)