Skip to content

qs_adapter module

Adapter class for quantstats.

Note

Accessors do not utilize caching.

We can access the adapter from ReturnsAccessor:

>>> import numpy as np
>>> import pandas as pd
>>> import vectorbt as vbt
>>> import quantstats as qs

>>> np.random.seed(42)
>>> rets = pd.Series(np.random.uniform(-0.1, 0.1, size=(100,)))
>>> benchmark_rets = pd.Series(np.random.uniform(-0.1, 0.1, size=(100,)))

>>> rets.vbt.returns.qs.r_squared(benchmark=benchmark_rets)
0.0011582111228735541

Which is the same as:

>>> qs.stats.r_squared(rets, benchmark_rets)

So why not just using qs.stats?

First, we can define all parameters such as benchmark returns once and avoid passing them repeatedly to every function. Second, vectorbt automatically translates parameters passed to ReturnsAccessor for the use in quantstats.

>>> # Defaults that vectorbt understands
>>> ret_acc = rets.vbt.returns(
...     benchmark_rets=benchmark_rets,
...     freq='d',
...     year_freq='365d',
...     defaults=dict(risk_free=0.001)
... )

>>> ret_acc.qs.r_squared()
0.0011582111228735541

>>> ret_acc.qs.sharpe()
-1.9158923252075455

>>> # Defaults that only quantstats understands
>>> qs_defaults = dict(
...     benchmark=benchmark_rets,
...     periods=365,
...     periods_per_year=365,
...     rf=0.001
... )
>>> ret_acc_qs = rets.vbt.returns.qs(defaults=qs_defaults)

>>> ret_acc_qs.r_squared()
0.0011582111228735541

>>> ret_acc_qs.sharpe()
-1.9158923252075455

The adapter automatically passes the returns to the particular function. It also merges the defaults defined in the settings, the defaults passed to ReturnsAccessor, and the defaults passed to QSAdapter itself, and matches them with the argument names listed in the function's signature.

For example, the periods and periods_per_year arguments default to the annualization factor ReturnsAccessor.ann_factor, which itself is based on the freq argument. This makes the results produced by quantstats and vectorbt at least somewhat similar.

>>> vbt.settings.array_wrapper['freq'] = 'h'
>>> vbt.settings.returns['year_freq'] = '365d'

>>> rets.vbt.returns.sharpe_ratio()  # ReturnsAccessor
-9.38160953971508

>>> rets.vbt.returns.qs.sharpe()  # quantstats via QSAdapter
-9.38160953971508

We can still override any argument by overriding its default or by passing it directly to the function:

>>> rets.vbt.returns.qs(defaults=dict(periods=252)).sharpe()
-1.5912029345745982

>>> rets.vbt.returns.qs.sharpe(periods=252)
-1.5912029345745982

>>> qs.stats.sharpe(rets)
-1.5912029345745982

attach_qs_methods function

attach_qs_methods(
    cls,
    replace_signature=True
)

Class decorator to attach quantstats methods.


QSAdapter class

QSAdapter(
    returns_accessor,
    defaults=None,
    **kwargs
)

Adapter class for quantstats.

Superclasses

Inherited members


adjusted_sortino method

QSAdapter.adjusted_sortino(
    *,
    rf=0,
    periods=252,
    annualize=True,
    smart=False
)

See quantstats.stats.adjusted_sortino.


aggregate_returns method

QSAdapter.aggregate_returns(
    *,
    period=None,
    compounded=True
)

See quantstats.utils.aggregate_returns.


autocorr_penalty method

QSAdapter.autocorr_penalty(
    *,
    prepare_returns=False
)

See quantstats.stats.autocorr_penalty.


avg_loss method

QSAdapter.avg_loss(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.avg_loss.


avg_return method

QSAdapter.avg_return(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.avg_return.


avg_win method

QSAdapter.avg_win(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.avg_win.


basic_report method

QSAdapter.basic_report(
    *,
    benchmark=None,
    rf=0.0,
    grayscale=False,
    figsize=(8, 5),
    display=True,
    compounded=True,
    periods_per_year=252,
    match_dates=False
)

See quantstats.reports.basic.


best method

QSAdapter.best(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.best.


cagr method

QSAdapter.cagr(
    *,
    rf=0.0,
    compounded=True
)

See quantstats.stats.cagr.


calmar method

QSAdapter.calmar(
    *,
    prepare_returns=True
)

See quantstats.stats.calmar.


common_sense_ratio method

QSAdapter.common_sense_ratio(
    *,
    prepare_returns=True
)

See quantstats.stats.common_sense_ratio.


comp method

QSAdapter.comp()

See quantstats.stats.comp.


compare method

QSAdapter.compare(
    *,
    benchmark,
    aggregate=None,
    compounded=True,
    round_vals=None,
    prepare_returns=True
)

See quantstats.stats.compare.


compsum method

QSAdapter.compsum()

See quantstats.stats.compsum.


conditional_value_at_risk method

QSAdapter.conditional_value_at_risk(
    *,
    sigma=1,
    confidence=0.95,
    prepare_returns=True
)

See quantstats.stats.conditional_value_at_risk.


consecutive_losses method

QSAdapter.consecutive_losses(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.consecutive_losses.


consecutive_wins method

QSAdapter.consecutive_wins(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.consecutive_wins.


cpc_index method

QSAdapter.cpc_index(
    *,
    prepare_returns=True
)

See quantstats.stats.cpc_index.


cvar method

QSAdapter.cvar(
    *,
    sigma=1,
    confidence=0.95,
    prepare_returns=True
)

See quantstats.stats.cvar.


defaults property

Defaults for QSAdapter.

Merges qs_adapter.defaults from settings, returns_accessor.defaults (with adapted naming), and defaults from QSAdapter.


defaults_mapping property

Common argument names in quantstats mapped to ReturnsAccessor.defaults.


distribution method

QSAdapter.distribution(
    *,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.distribution.


expected_return method

QSAdapter.expected_return(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.expected_return.


expected_shortfall method

QSAdapter.expected_shortfall(
    *,
    sigma=1,
    confidence=0.95
)

See quantstats.stats.expected_shortfall.


exponential_stdev method

QSAdapter.exponential_stdev(
    *,
    window=30,
    is_halflife=False
)

See quantstats.utils.exponential_stdev.


exposure method

QSAdapter.exposure(
    *,
    prepare_returns=True
)

See quantstats.stats.exposure.


full_report method

QSAdapter.full_report(
    *,
    benchmark=None,
    rf=0.0,
    grayscale=False,
    figsize=(8, 5),
    display=True,
    compounded=True,
    periods_per_year=252,
    match_dates=False
)

See quantstats.reports.full.


gain_to_pain_ratio method

QSAdapter.gain_to_pain_ratio(
    *,
    rf=0,
    resolution='D'
)

See quantstats.stats.gain_to_pain_ratio.


greeks method

QSAdapter.greeks(
    *,
    benchmark,
    periods=252.0,
    prepare_returns=True
)

See quantstats.stats.greeks.


group_returns method

QSAdapter.group_returns(
    *,
    groupby,
    compounded=False
)

See quantstats.utils.group_returns.


html_report method

QSAdapter.html_report(
    *,
    benchmark=None,
    rf=0.0,
    grayscale=False,
    title='Strategy Tearsheet',
    output=None,
    compounded=True,
    periods_per_year=252,
    download_filename='quantstats-tearsheet.html',
    figfmt='svg',
    template_path=None,
    match_dates=False,
    **kwargs
)

See quantstats.reports.html.


implied_volatility method

QSAdapter.implied_volatility(
    *,
    periods=252,
    annualize=True
)

See quantstats.stats.implied_volatility.


information_ratio method

QSAdapter.information_ratio(
    *,
    benchmark,
    prepare_returns=True
)

See quantstats.stats.information_ratio.


kelly_criterion method

QSAdapter.kelly_criterion(
    *,
    prepare_returns=True
)

See quantstats.stats.kelly_criterion.


kurtosis method

QSAdapter.kurtosis(
    *,
    prepare_returns=True
)

See quantstats.stats.kurtosis.


log_returns method

QSAdapter.log_returns(
    *,
    rf=0.0,
    nperiods=None
)

See quantstats.utils.log_returns.


make_index method

QSAdapter.make_index(
    *,
    rebalance='1M',
    period='max',
    returns=None,
    match_dates=False
)

See quantstats.utils.make_index.


make_portfolio method

QSAdapter.make_portfolio(
    *,
    start_balance=100000.0,
    mode='comp',
    round_to=None
)

See quantstats.utils.make_portfolio.


metrics_report method

QSAdapter.metrics_report(
    *,
    benchmark=None,
    rf=0.0,
    display=True,
    mode='basic',
    sep=False,
    compounded=True,
    periods_per_year=252,
    prepare_returns=True,
    match_dates=False,
    **kwargs
)

See quantstats.reports.metrics.


monthly_returns method

QSAdapter.monthly_returns(
    *,
    eoy=True,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.monthly_returns.


omega method

QSAdapter.omega(
    *,
    rf=0.0,
    required_return=0.0,
    periods=252
)

See quantstats.stats.omega.


outlier_loss_ratio method

QSAdapter.outlier_loss_ratio(
    *,
    quantile=0.01,
    prepare_returns=True
)

See quantstats.stats.outlier_loss_ratio.


outlier_win_ratio method

QSAdapter.outlier_win_ratio(
    *,
    quantile=0.99,
    prepare_returns=True
)

See quantstats.stats.outlier_win_ratio.


outliers method

QSAdapter.outliers(
    *,
    quantile=0.95
)

See quantstats.stats.outliers.


payoff_ratio method

QSAdapter.payoff_ratio(
    *,
    prepare_returns=True
)

See quantstats.stats.payoff_ratio.


plot_daily_returns method

QSAdapter.plot_daily_returns(
    *,
    grayscale=False,
    figsize=(10, 4),
    fontname='Arial',
    lw=0.5,
    log_scale=False,
    ylabel='Returns',
    subtitle=True,
    savefig=None,
    show=True,
    prepare_returns=True
)

See quantstats.plots.daily_returns.


plot_distribution method

QSAdapter.plot_distribution(
    *,
    fontname='Arial',
    grayscale=False,
    ylabel=True,
    figsize=(10, 6),
    subtitle=True,
    compounded=True,
    savefig=None,
    show=True,
    prepare_returns=True
)

See quantstats.plots.distribution.


plot_drawdown method

QSAdapter.plot_drawdown(
    *,
    grayscale=False,
    figsize=(10, 5),
    fontname='Arial',
    lw=1,
    log_scale=False,
    match_volatility=False,
    compound=False,
    ylabel='Drawdown',
    resample=None,
    subtitle=True,
    savefig=None,
    show=True
)

See quantstats.plots.drawdown.


plot_drawdowns_periods method

QSAdapter.plot_drawdowns_periods(
    *,
    periods=5,
    lw=1.5,
    log_scale=False,
    fontname='Arial',
    grayscale=False,
    figsize=(10, 5),
    ylabel=True,
    subtitle=True,
    compounded=True,
    savefig=None,
    show=True,
    prepare_returns=True
)

See quantstats.plots.drawdowns_periods.


plot_earnings method

QSAdapter.plot_earnings(
    *,
    start_balance=100000.0,
    mode='comp',
    grayscale=False,
    figsize=(10, 6),
    title='Portfolio Earnings',
    fontname='Arial',
    lw=1.5,
    subtitle=True,
    savefig=None,
    show=True
)

See quantstats.plots.earnings.


plot_histogram method

QSAdapter.plot_histogram(
    *,
    resample='M',
    fontname='Arial',
    grayscale=False,
    figsize=(10, 5),
    ylabel=True,
    subtitle=True,
    compounded=True,
    savefig=None,
    show=True,
    prepare_returns=True
)

See quantstats.plots.histogram.


plot_log_returns method

QSAdapter.plot_log_returns(
    *,
    benchmark=None,
    grayscale=False,
    figsize=(10, 5),
    fontname='Arial',
    lw=1.5,
    match_volatility=False,
    compound=True,
    cumulative=True,
    resample=None,
    ylabel='Cumulative Returns',
    subtitle=True,
    savefig=None,
    show=True,
    prepare_returns=True
)

See quantstats.plots.log_returns.


plot_monthly_heatmap method

QSAdapter.plot_monthly_heatmap(
    *,
    annot_size=10,
    figsize=(10, 5),
    cbar=True,
    square=False,
    compounded=True,
    eoy=False,
    grayscale=False,
    fontname='Arial',
    ylabel=True,
    savefig=None,
    show=True
)

See quantstats.plots.monthly_heatmap.


plot_monthly_returns method

QSAdapter.plot_monthly_returns(
    *,
    annot_size=10,
    figsize=(10, 5),
    cbar=True,
    square=False,
    compounded=True,
    eoy=False,
    grayscale=False,
    fontname='Arial',
    ylabel=True,
    savefig=None,
    show=True
)

See quantstats.plots.monthly_returns.


plot_returns method

QSAdapter.plot_returns(
    *,
    benchmark=None,
    grayscale=False,
    figsize=(10, 6),
    fontname='Arial',
    lw=1.5,
    match_volatility=False,
    compound=True,
    cumulative=True,
    resample=None,
    ylabel='Cumulative Returns',
    subtitle=True,
    savefig=None,
    show=True,
    prepare_returns=True
)

See quantstats.plots.returns.


plot_rolling_beta method

QSAdapter.plot_rolling_beta(
    *,
    benchmark,
    window1=126,
    window1_label='6-Months',
    window2=252,
    window2_label='12-Months',
    lw=1.5,
    fontname='Arial',
    grayscale=False,
    figsize=(10, 3),
    ylabel=True,
    subtitle=True,
    savefig=None,
    show=True,
    prepare_returns=True
)

See quantstats.plots.rolling_beta.


plot_rolling_sharpe method

QSAdapter.plot_rolling_sharpe(
    *,
    benchmark=None,
    rf=0.0,
    period=126,
    period_label='6-Months',
    periods_per_year=252,
    lw=1.25,
    fontname='Arial',
    grayscale=False,
    figsize=(10, 3),
    ylabel='Sharpe',
    subtitle=True,
    savefig=None,
    show=True
)

See quantstats.plots.rolling_sharpe.


plot_rolling_sortino method

QSAdapter.plot_rolling_sortino(
    *,
    benchmark=None,
    rf=0.0,
    period=126,
    period_label='6-Months',
    periods_per_year=252,
    lw=1.25,
    fontname='Arial',
    grayscale=False,
    figsize=(10, 3),
    ylabel='Sortino',
    subtitle=True,
    savefig=None,
    show=True
)

See quantstats.plots.rolling_sortino.


plot_rolling_volatility method

QSAdapter.plot_rolling_volatility(
    *,
    benchmark=None,
    period=126,
    period_label='6-Months',
    periods_per_year=252,
    lw=1.5,
    fontname='Arial',
    grayscale=False,
    figsize=(10, 3),
    ylabel='Volatility',
    subtitle=True,
    savefig=None,
    show=True
)

See quantstats.plots.rolling_volatility.


plot_snapshot method

QSAdapter.plot_snapshot(
    *,
    grayscale=False,
    figsize=(10, 8),
    title='Portfolio Summary',
    fontname='Arial',
    lw=1.5,
    mode='comp',
    subtitle=True,
    savefig=None,
    show=True,
    log_scale=False
)

See quantstats.plots.snapshot.


plot_yearly_returns method

QSAdapter.plot_yearly_returns(
    *,
    benchmark=None,
    fontname='Arial',
    grayscale=False,
    hlw=1.5,
    hlcolor='red',
    hllabel='',
    match_volatility=False,
    log_scale=False,
    figsize=(10, 5),
    ylabel=True,
    subtitle=True,
    compounded=True,
    savefig=None,
    show=True,
    prepare_returns=True
)

See quantstats.plots.yearly_returns.


plots_report method

QSAdapter.plots_report(
    *,
    benchmark=None,
    grayscale=False,
    figsize=(8, 5),
    mode='basic',
    compounded=True,
    periods_per_year=252,
    prepare_returns=True,
    match_dates=False
)

See quantstats.reports.plots.


profit_factor method

QSAdapter.profit_factor(
    *,
    prepare_returns=True
)

See quantstats.stats.profit_factor.


profit_ratio method

QSAdapter.profit_ratio(
    *,
    prepare_returns=True
)

See quantstats.stats.profit_ratio.


r2 method

QSAdapter.r2(
    *,
    benchmark
)

See quantstats.stats.r2.


r_squared method

QSAdapter.r_squared(
    *,
    benchmark,
    prepare_returns=True
)

See quantstats.stats.r_squared.


rar method

QSAdapter.rar(
    *,
    rf=0.0
)

See quantstats.stats.rar.


recovery_factor method

QSAdapter.recovery_factor(
    *,
    prepare_returns=True
)

See quantstats.stats.recovery_factor.


remove_outliers method

QSAdapter.remove_outliers(
    *,
    quantile=0.95
)

See quantstats.stats.remove_outliers.


returns_accessor property

Returns accessor.


risk_of_ruin method

QSAdapter.risk_of_ruin(
    *,
    prepare_returns=True
)

See quantstats.stats.risk_of_ruin.


risk_return_ratio method

QSAdapter.risk_return_ratio(
    *,
    prepare_returns=True
)

See quantstats.stats.risk_return_ratio.


rolling_greeks method

QSAdapter.rolling_greeks(
    *,
    benchmark,
    periods=252,
    prepare_returns=True
)

See quantstats.stats.rolling_greeks.


rolling_sharpe method

QSAdapter.rolling_sharpe(
    *,
    rf=0.0,
    rolling_period=126,
    annualize=True,
    periods_per_year=252,
    prepare_returns=True
)

See quantstats.stats.rolling_sharpe.


rolling_sortino method

QSAdapter.rolling_sortino(
    *,
    rf=0,
    rolling_period=126,
    annualize=True,
    periods_per_year=252,
    **kwargs
)

See quantstats.stats.rolling_sortino.


rolling_volatility method

QSAdapter.rolling_volatility(
    *,
    rolling_period=126,
    periods_per_year=252,
    prepare_returns=True
)

See quantstats.stats.rolling_volatility.


ror method

QSAdapter.ror()

See quantstats.stats.ror.


serenity_index method

QSAdapter.serenity_index(
    *,
    rf=0
)

See quantstats.stats.serenity_index.


sharpe method

QSAdapter.sharpe(
    *,
    rf=0.0,
    periods=252,
    annualize=True,
    smart=False
)

See quantstats.stats.sharpe.


skew method

QSAdapter.skew(
    *,
    prepare_returns=True
)

See quantstats.stats.skew.


smart_sharpe method

QSAdapter.smart_sharpe(
    *,
    rf=0.0,
    periods=252,
    annualize=True
)

See quantstats.stats.smart_sharpe.


smart_sortino method

QSAdapter.smart_sortino(
    *,
    rf=0,
    periods=252,
    annualize=True
)

See quantstats.stats.smart_sortino.


sortino method

QSAdapter.sortino(
    *,
    rf=0,
    periods=252,
    annualize=True,
    smart=False
)

See quantstats.stats.sortino.


tail_ratio method

QSAdapter.tail_ratio(
    *,
    cutoff=0.95,
    prepare_returns=True
)

See quantstats.stats.tail_ratio.


to_drawdown_series method

QSAdapter.to_drawdown_series()

See quantstats.stats.to_drawdown_series.


to_excess_returns method

QSAdapter.to_excess_returns(
    *,
    rf,
    nperiods=None
)

See quantstats.utils.to_excess_returns.


to_log_returns method

QSAdapter.to_log_returns(
    *,
    rf=0.0,
    nperiods=None
)

See quantstats.utils.to_log_returns.


to_prices method

QSAdapter.to_prices(
    *,
    base=100000.0
)

See quantstats.utils.to_prices.


treynor_ratio method

QSAdapter.treynor_ratio(
    *,
    benchmark,
    periods=252.0,
    rf=0.0
)

See quantstats.stats.treynor_ratio.


ulcer_index method

QSAdapter.ulcer_index()

See quantstats.stats.ulcer_index.


ulcer_performance_index method

QSAdapter.ulcer_performance_index(
    *,
    rf=0
)

See quantstats.stats.ulcer_performance_index.


upi method

QSAdapter.upi(
    *,
    rf=0
)

See quantstats.stats.upi.


value_at_risk method

QSAdapter.value_at_risk(
    *,
    sigma=1,
    confidence=0.95,
    prepare_returns=True
)

See quantstats.stats.value_at_risk.


var method

QSAdapter.var(
    *,
    sigma=1,
    confidence=0.95,
    prepare_returns=True
)

See quantstats.stats.var.


volatility method

QSAdapter.volatility(
    *,
    periods=252,
    annualize=True,
    prepare_returns=True
)

See quantstats.stats.volatility.


win_loss_ratio method

QSAdapter.win_loss_ratio(
    *,
    prepare_returns=True
)

See quantstats.stats.win_loss_ratio.


win_rate method

QSAdapter.win_rate(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.win_rate.


worst method

QSAdapter.worst(
    *,
    aggregate=None,
    compounded=True,
    prepare_returns=True
)

See quantstats.stats.worst.