Skip to content

nb module¶

Numba-compiled functions.

Provides an arsenal of Numba-compiled functions that are used by accessors and for measuring portfolio performance. These only accept NumPy arrays and other Numba-compatible types.

>>> import numpy as np
>>> import vectorbt as vbt

>>> price = np.array([1.1, 1.2, 1.3, 1.2, 1.1])
>>> returns = vbt.generic.nb.pct_change_1d_nb(price)

>>> # vectorbt.returns.nb.cum_returns_1d_nb
>>> vbt.returns.nb.cum_returns_1d_nb(returns, 0)
array([0., 0.09090909, 0.18181818, 0.09090909, 0.])

Note

vectorbt treats matrices as first-class citizens and expects input arrays to be 2-dim, unless function has suffix _1d or is meant to be input to another function. Data is processed along index (axis 0).

All functions passed as argument should be Numba-compiled.


alpha_1d_nb function¶

alpha_1d_nb(
    returns,
    benchmark_rets,
    ann_factor,
    risk_free=0.0
)

Annualized alpha.


alpha_nb function¶

alpha_nb(
    returns,
    benchmark_rets,
    ann_factor,
    risk_free=0.0
)

2-dim version of alpha_1d_nb().


annualized_return_1d_nb function¶

annualized_return_1d_nb(
    returns,
    ann_factor
)

Mean annual growth rate of returns.

This is equivalent to the compound annual growth rate.


annualized_return_nb function¶

annualized_return_nb(
    returns,
    ann_factor
)

2-dim version of annualized_return_1d_nb().


annualized_volatility_1d_nb function¶

annualized_volatility_1d_nb(
    returns,
    ann_factor,
    levy_alpha=2.0,
    ddof=1
)

Annualized volatility of a strategy.


annualized_volatility_nb function¶

annualized_volatility_nb(
    returns,
    ann_factor,
    levy_alpha=2.0,
    ddof=1
)

2-dim version of annualized_volatility_1d_nb().


beta_1d_nb function¶

beta_1d_nb(
    returns,
    benchmark_rets
)

Beta.


beta_nb function¶

beta_nb(
    returns,
    benchmark_rets
)

2-dim version of beta_1d_nb().


calmar_ratio_1d_nb function¶

calmar_ratio_1d_nb(
    returns,
    ann_factor
)

Calmar ratio, or drawdown ratio, of a strategy.


calmar_ratio_nb function¶

calmar_ratio_nb(
    returns,
    ann_factor
)

2-dim version of calmar_ratio_1d_nb().


capture_1d_nb function¶

capture_1d_nb(
    returns,
    benchmark_rets,
    ann_factor
)

Capture ratio.


capture_nb function¶

capture_nb(
    returns,
    benchmark_rets,
    ann_factor
)

2-dim version of capture_1d_nb().


cond_value_at_risk_1d_nb function¶

cond_value_at_risk_1d_nb(
    returns,
    cutoff=0.05
)

Conditional value at risk (CVaR) of a returns stream.

NaN returns are ignored, same as in value_at_risk_1d_nb().


cond_value_at_risk_nb function¶

cond_value_at_risk_nb(
    returns,
    cutoff=0.05
)

2-dim version of cond_value_at_risk_1d_nb().


cum_returns_1d_nb function¶

cum_returns_1d_nb(
    returns,
    start_value
)

Cumulative returns.


cum_returns_final_1d_nb function¶

cum_returns_final_1d_nb(
    returns,
    start_value=0.0
)

Total return.


cum_returns_final_nb function¶

cum_returns_final_nb(
    returns,
    start_value=0.0
)

2-dim version of cum_returns_final_1d_nb().


cum_returns_nb function¶

cum_returns_nb(
    returns,
    start_value
)

2-dim version of cum_returns_1d_nb().


down_capture_1d_nb function¶

down_capture_1d_nb(
    returns,
    benchmark_rets,
    ann_factor
)

Capture ratio for periods when the benchmark return is negative.


down_capture_nb function¶

down_capture_nb(
    returns,
    benchmark_rets,
    ann_factor
)

2-dim version of down_capture_1d_nb().


downside_risk_1d_nb function¶

downside_risk_1d_nb(
    returns,
    ann_factor,
    required_return=0.0
)

Downside deviation below a threshold.


downside_risk_nb function¶

downside_risk_nb(
    returns,
    ann_factor,
    required_return=0.0
)

2-dim version of downside_risk_1d_nb().


drawdown_1d_nb function¶

drawdown_1d_nb(
    returns
)

Drawdown of cumulative returns.


drawdown_nb function¶

drawdown_nb(
    returns
)

2-dim version of drawdown_1d_nb().


get_return_nb function¶

get_return_nb(
    input_value,
    output_value
)

Calculate return from input and output value.


information_ratio_1d_nb function¶

information_ratio_1d_nb(
    returns,
    benchmark_rets,
    ddof=1
)

Information ratio of a strategy.


information_ratio_nb function¶

information_ratio_nb(
    returns,
    benchmark_rets,
    ddof=1
)

2-dim version of information_ratio_1d_nb().


martin_ratio_1d_nb function¶

martin_ratio_1d_nb(
    returns,
    ann_factor,
    risk_free=0.0
)

Martin ratio, or ulcer performance index: the annualized return minus the annualized risk-free return, divided by the ulcer index.

risk_free is a constant per-period return and is compounded over ann_factor periods. Return NaN when the ulcer index is zero or NaN.


martin_ratio_nb function¶

martin_ratio_nb(
    returns,
    ann_factor,
    risk_free=0.0
)

2-dim version of martin_ratio_1d_nb().


max_drawdown_1d_nb function¶

max_drawdown_1d_nb(
    returns
)

Total maximum drawdown (MDD).


max_drawdown_nb function¶

max_drawdown_nb(
    returns
)

2-dim version of max_drawdown_1d_nb().


omega_ratio_1d_nb function¶

omega_ratio_1d_nb(
    returns,
    ann_factor,
    risk_free=0.0,
    required_return=0.0
)

Omega ratio of a strategy..


omega_ratio_nb function¶

omega_ratio_nb(
    returns,
    ann_factor,
    risk_free=0.0,
    required_return=0.0
)

2-dim version of omega_ratio_1d_nb().


returns_1d_nb function¶

returns_1d_nb(
    value,
    init_value
)

Calculate returns from value.


returns_nb function¶

returns_nb(
    value,
    init_value
)

2-dim version of returns_1d_nb().


rolling_alpha_nb function¶

rolling_alpha_nb(
    returns,
    window,
    minp,
    benchmark_rets,
    ann_factor,
    risk_free=0.0
)

Rolling version of alpha_nb().


rolling_annualized_return_nb function¶

rolling_annualized_return_nb(
    returns,
    window,
    minp,
    ann_factor
)

Rolling version of annualized_return_nb().


rolling_annualized_volatility_nb function¶

rolling_annualized_volatility_nb(
    returns,
    window,
    minp,
    ann_factor,
    levy_alpha=2.0,
    ddof=1
)

Rolling version of annualized_volatility_nb().


rolling_beta_nb function¶

rolling_beta_nb(
    returns,
    window,
    minp,
    benchmark_rets
)

Rolling version of beta_nb().


rolling_calmar_ratio_nb function¶

rolling_calmar_ratio_nb(
    returns,
    window,
    minp,
    ann_factor
)

Rolling version of calmar_ratio_nb().


rolling_capture_nb function¶

rolling_capture_nb(
    returns,
    window,
    minp,
    benchmark_rets,
    ann_factor
)

Rolling version of capture_nb().


rolling_cond_value_at_risk_nb function¶

rolling_cond_value_at_risk_nb(
    returns,
    window,
    minp,
    cutoff=0.05
)

Rolling version of cond_value_at_risk_nb().


rolling_cum_returns_final_nb function¶

rolling_cum_returns_final_nb(
    returns,
    window,
    minp,
    start_value=0.0
)

Rolling version of cum_returns_final_nb().


rolling_down_capture_nb function¶

rolling_down_capture_nb(
    returns,
    window,
    minp,
    benchmark_rets,
    ann_factor
)

Rolling version of down_capture_nb().


rolling_downside_risk_nb function¶

rolling_downside_risk_nb(
    returns,
    window,
    minp,
    ann_factor,
    required_return=0.0
)

Rolling version of downside_risk_nb().


rolling_information_ratio_nb function¶

rolling_information_ratio_nb(
    returns,
    window,
    minp,
    benchmark_rets,
    ddof=1
)

Rolling version of information_ratio_nb().


rolling_martin_ratio_nb function¶

rolling_martin_ratio_nb(
    returns,
    window,
    minp,
    ann_factor,
    risk_free=0.0
)

Rolling version of martin_ratio_nb().


rolling_max_drawdown_nb function¶

rolling_max_drawdown_nb(
    returns,
    window,
    minp
)

Rolling version of max_drawdown_nb().


rolling_omega_ratio_nb function¶

rolling_omega_ratio_nb(
    returns,
    window,
    minp,
    ann_factor,
    risk_free=0.0,
    required_return=0.0
)

Rolling version of omega_ratio_nb().


rolling_sharpe_ratio_nb function¶

rolling_sharpe_ratio_nb(
    returns,
    window,
    minp,
    ann_factor,
    risk_free=0.0,
    ddof=1
)

Rolling version of sharpe_ratio_nb().


rolling_sortino_ratio_nb function¶

rolling_sortino_ratio_nb(
    returns,
    window,
    minp,
    ann_factor,
    required_return=0.0
)

Rolling version of sortino_ratio_nb().


rolling_tail_ratio_nb function¶

rolling_tail_ratio_nb(
    returns,
    window,
    minp
)

Rolling version of tail_ratio_nb().


rolling_ulcer_index_nb function¶

rolling_ulcer_index_nb(
    returns,
    window,
    minp
)

Rolling version of ulcer_index_nb().


rolling_up_capture_nb function¶

rolling_up_capture_nb(
    returns,
    window,
    minp,
    benchmark_rets,
    ann_factor
)

Rolling version of up_capture_nb().


rolling_value_at_risk_nb function¶

rolling_value_at_risk_nb(
    returns,
    window,
    minp,
    cutoff=0.05
)

Rolling version of value_at_risk_nb().


sharpe_ratio_1d_nb function¶

sharpe_ratio_1d_nb(
    returns,
    ann_factor,
    risk_free=0.0,
    ddof=1
)

Sharpe ratio of a strategy.

Returns NaN if the mean and the standard deviation are both zero, and signed infinity if only the standard deviation is zero. Constant returns are treated as having zero standard deviation, even if rounding in the mean leaves a tiny non-zero value.


sharpe_ratio_nb function¶

sharpe_ratio_nb(
    returns,
    ann_factor,
    risk_free=0.0,
    ddof=1
)

2-dim version of sharpe_ratio_1d_nb().


sortino_ratio_1d_nb function¶

sortino_ratio_1d_nb(
    returns,
    ann_factor,
    required_return=0.0
)

Sortino ratio of a strategy.


sortino_ratio_nb function¶

sortino_ratio_nb(
    returns,
    ann_factor,
    required_return=0.0
)

2-dim version of sortino_ratio_1d_nb().


tail_ratio_1d_nb function¶

tail_ratio_1d_nb(
    returns
)

Ratio between the right (95%) and left tail (5%).


tail_ratio_nb function¶

tail_ratio_nb(
    returns
)

2-dim version of tail_ratio_1d_nb().


total_return_apply_nb function¶

total_return_apply_nb(
    idxs,
    col,
    returns
)

Calculate total return from returns.


ulcer_index_1d_nb function¶

ulcer_index_1d_nb(
    returns
)

Ulcer index: the root mean square of the drawdowns.

Unlike the maximum drawdown, it accounts for both the depth and the duration of drawdowns. The result is a fraction, for example 0.05 means 5%.

Uses drawdown_1d_nb(), treating NaN returns as zero-return observations. Empty input returns NaN; a series without drawdowns returns zero.


ulcer_index_nb function¶

ulcer_index_nb(
    returns
)

2-dim version of ulcer_index_1d_nb().


up_capture_1d_nb function¶

up_capture_1d_nb(
    returns,
    benchmark_rets,
    ann_factor
)

Capture ratio for periods when the benchmark return is positive.


up_capture_nb function¶

up_capture_nb(
    returns,
    benchmark_rets,
    ann_factor
)

2-dim version of up_capture_1d_nb().


value_at_risk_1d_nb function¶

value_at_risk_1d_nb(
    returns,
    cutoff=0.05
)

Value at risk (VaR) of a returns stream.


value_at_risk_nb function¶

value_at_risk_nb(
    returns,
    cutoff=0.05
)

2-dim version of value_at_risk_1d_nb().