quantstats_lumi

  1#!/usr/bin/env python
  2# -*- coding: UTF-8 -*-
  3#
  4# QuantStats: Portfolio analytics for quants
  5# https://github.com/ranaroussi/quantstats
  6#
  7# Copyright 2019-2023 Ran Aroussi
  8#
  9# Licensed under the Apache License, Version 2.0 (the "License");
 10# you may not use this file except in compliance with the License.
 11# You may obtain a copy of the License at
 12#
 13#     http://www.apache.org/licenses/LICENSE-2.0
 14#
 15# Unless required by applicable law or agreed to in writing, software
 16# distributed under the License is distributed on an "AS IS" BASIS,
 17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18# See the License for the specific language governing permissions and
 19# limitations under the License.
 20
 21from . import version
 22
 23__version__ = version.version
 24__author__ = "Ran Aroussi"
 25
 26from . import stats, utils, plots, reports
 27
 28__all__ = ["stats", "plots", "reports", "utils", "extend_pandas"]
 29
 30# try automatic matplotlib inline
 31utils._in_notebook(matplotlib_inline=True)
 32
 33
 34def extend_pandas():
 35    """
 36    Extends pandas by exposing methods to be used like:
 37    df.sharpe(), df.best('day'), ...
 38    """
 39    from pandas.core.base import PandasObject as _po
 40
 41    _po.compsum = stats.compsum
 42    _po.comp = stats.comp
 43    _po.expected_return = stats.expected_return
 44    _po.geometric_mean = stats.geometric_mean
 45    _po.ghpr = stats.ghpr
 46    _po.outliers = stats.outliers
 47    _po.remove_outliers = stats.remove_outliers
 48    _po.best = stats.best
 49    _po.worst = stats.worst
 50    _po.consecutive_wins = stats.consecutive_wins
 51    _po.consecutive_losses = stats.consecutive_losses
 52    _po.exposure = stats.exposure
 53    _po.win_rate = stats.win_rate
 54    _po.avg_return = stats.avg_return
 55    _po.avg_win = stats.avg_win
 56    _po.avg_loss = stats.avg_loss
 57    _po.volatility = stats.volatility
 58    _po.rolling_volatility = stats.rolling_volatility
 59    _po.implied_volatility = stats.implied_volatility
 60    _po.sharpe = stats.sharpe
 61    _po.smart_sharpe = stats.smart_sharpe
 62    _po.rolling_sharpe = stats.rolling_sharpe
 63    _po.sortino = stats.sortino
 64    _po.smart_sortino = stats.smart_sortino
 65    _po.adjusted_sortino = stats.adjusted_sortino
 66    _po.rolling_sortino = stats.rolling_sortino
 67    _po.omega = stats.omega
 68    _po.cagr = stats.cagr
 69    _po.rar = stats.rar
 70    _po.skew = stats.skew
 71    _po.kurtosis = stats.kurtosis
 72    _po.calmar = stats.calmar
 73    _po.ulcer_index = stats.ulcer_index
 74    _po.ulcer_performance_index = stats.ulcer_performance_index
 75    _po.upi = stats.upi
 76    _po.serenity_index = stats.serenity_index
 77    _po.risk_of_ruin = stats.risk_of_ruin
 78    _po.ror = stats.ror
 79    _po.value_at_risk = stats.value_at_risk
 80    _po.var = stats.var
 81    _po.conditional_value_at_risk = stats.conditional_value_at_risk
 82    _po.cvar = stats.cvar
 83    _po.expected_shortfall = stats.expected_shortfall
 84    _po.tail_ratio = stats.tail_ratio
 85    _po.payoff_ratio = stats.payoff_ratio
 86    _po.win_loss_ratio = stats.win_loss_ratio
 87    _po.profit_ratio = stats.profit_ratio
 88    _po.profit_factor = stats.profit_factor
 89    _po.gain_to_pain_ratio = stats.gain_to_pain_ratio
 90    _po.cpc_index = stats.cpc_index
 91    _po.common_sense_ratio = stats.common_sense_ratio
 92    _po.outlier_win_ratio = stats.outlier_win_ratio
 93    _po.outlier_loss_ratio = stats.outlier_loss_ratio
 94    _po.recovery_factor = stats.recovery_factor
 95    _po.risk_return_ratio = stats.risk_return_ratio
 96    _po.max_drawdown = stats.max_drawdown
 97    _po.to_drawdown_series = stats.to_drawdown_series
 98    _po.kelly_criterion = stats.kelly_criterion
 99    _po.monthly_returns = stats.monthly_returns
100    _po.pct_rank = stats.pct_rank
101
102    _po.treynor_ratio = stats.treynor_ratio
103    _po.probabilistic_sharpe_ratio = stats.probabilistic_sharpe_ratio
104    _po.probabilistic_sortino_ratio = stats.probabilistic_sortino_ratio
105    _po.probabilistic_adjusted_sortino_ratio = (
106        stats.probabilistic_adjusted_sortino_ratio
107    )
108
109    # methods from utils
110    _po.to_returns = utils.to_returns
111    _po.to_prices = utils.to_prices
112    _po.to_log_returns = utils.to_log_returns
113    _po.log_returns = utils.log_returns
114    _po.exponential_stdev = utils.exponential_stdev
115    _po.rebase = utils.rebase
116    _po.aggregate_returns = utils.aggregate_returns
117    _po.to_excess_returns = utils.to_excess_returns
118    _po.multi_shift = utils.multi_shift
119    _po.curr_month = utils._pandas_current_month
120    _po.date = utils._pandas_date
121    _po.mtd = utils._mtd
122    _po.qtd = utils._qtd
123    _po.ytd = utils._ytd
124
125    # methods that requires benchmark stats
126    _po.r_squared = stats.r_squared
127    _po.r2 = stats.r2
128    _po.information_ratio = stats.information_ratio
129    _po.greeks = stats.greeks
130    _po.rolling_greeks = stats.rolling_greeks
131    _po.compare = stats.compare
132
133    # plotting methods
134    _po.plot_snapshot = plots.snapshot
135    _po.plot_earnings = plots.earnings
136    _po.plot_daily_returns = plots.daily_returns
137    _po.plot_distribution = plots.distribution
138    _po.plot_drawdown = plots.drawdown
139    _po.plot_drawdowns_periods = plots.drawdowns_periods
140    _po.plot_histogram = plots.histogram
141    _po.plot_log_returns = plots.log_returns
142    _po.plot_returns = plots.returns
143    _po.plot_rolling_beta = plots.rolling_beta
144    _po.plot_rolling_sharpe = plots.rolling_sharpe
145    _po.plot_rolling_sortino = plots.rolling_sortino
146    _po.plot_rolling_volatility = plots.rolling_volatility
147    _po.plot_yearly_returns = plots.yearly_returns
148    _po.plot_monthly_heatmap = plots.monthly_heatmap
149
150    _po.metrics = reports.metrics
151
152
153# extend_pandas()
def extend_pandas():
 35def extend_pandas():
 36    """
 37    Extends pandas by exposing methods to be used like:
 38    df.sharpe(), df.best('day'), ...
 39    """
 40    from pandas.core.base import PandasObject as _po
 41
 42    _po.compsum = stats.compsum
 43    _po.comp = stats.comp
 44    _po.expected_return = stats.expected_return
 45    _po.geometric_mean = stats.geometric_mean
 46    _po.ghpr = stats.ghpr
 47    _po.outliers = stats.outliers
 48    _po.remove_outliers = stats.remove_outliers
 49    _po.best = stats.best
 50    _po.worst = stats.worst
 51    _po.consecutive_wins = stats.consecutive_wins
 52    _po.consecutive_losses = stats.consecutive_losses
 53    _po.exposure = stats.exposure
 54    _po.win_rate = stats.win_rate
 55    _po.avg_return = stats.avg_return
 56    _po.avg_win = stats.avg_win
 57    _po.avg_loss = stats.avg_loss
 58    _po.volatility = stats.volatility
 59    _po.rolling_volatility = stats.rolling_volatility
 60    _po.implied_volatility = stats.implied_volatility
 61    _po.sharpe = stats.sharpe
 62    _po.smart_sharpe = stats.smart_sharpe
 63    _po.rolling_sharpe = stats.rolling_sharpe
 64    _po.sortino = stats.sortino
 65    _po.smart_sortino = stats.smart_sortino
 66    _po.adjusted_sortino = stats.adjusted_sortino
 67    _po.rolling_sortino = stats.rolling_sortino
 68    _po.omega = stats.omega
 69    _po.cagr = stats.cagr
 70    _po.rar = stats.rar
 71    _po.skew = stats.skew
 72    _po.kurtosis = stats.kurtosis
 73    _po.calmar = stats.calmar
 74    _po.ulcer_index = stats.ulcer_index
 75    _po.ulcer_performance_index = stats.ulcer_performance_index
 76    _po.upi = stats.upi
 77    _po.serenity_index = stats.serenity_index
 78    _po.risk_of_ruin = stats.risk_of_ruin
 79    _po.ror = stats.ror
 80    _po.value_at_risk = stats.value_at_risk
 81    _po.var = stats.var
 82    _po.conditional_value_at_risk = stats.conditional_value_at_risk
 83    _po.cvar = stats.cvar
 84    _po.expected_shortfall = stats.expected_shortfall
 85    _po.tail_ratio = stats.tail_ratio
 86    _po.payoff_ratio = stats.payoff_ratio
 87    _po.win_loss_ratio = stats.win_loss_ratio
 88    _po.profit_ratio = stats.profit_ratio
 89    _po.profit_factor = stats.profit_factor
 90    _po.gain_to_pain_ratio = stats.gain_to_pain_ratio
 91    _po.cpc_index = stats.cpc_index
 92    _po.common_sense_ratio = stats.common_sense_ratio
 93    _po.outlier_win_ratio = stats.outlier_win_ratio
 94    _po.outlier_loss_ratio = stats.outlier_loss_ratio
 95    _po.recovery_factor = stats.recovery_factor
 96    _po.risk_return_ratio = stats.risk_return_ratio
 97    _po.max_drawdown = stats.max_drawdown
 98    _po.to_drawdown_series = stats.to_drawdown_series
 99    _po.kelly_criterion = stats.kelly_criterion
100    _po.monthly_returns = stats.monthly_returns
101    _po.pct_rank = stats.pct_rank
102
103    _po.treynor_ratio = stats.treynor_ratio
104    _po.probabilistic_sharpe_ratio = stats.probabilistic_sharpe_ratio
105    _po.probabilistic_sortino_ratio = stats.probabilistic_sortino_ratio
106    _po.probabilistic_adjusted_sortino_ratio = (
107        stats.probabilistic_adjusted_sortino_ratio
108    )
109
110    # methods from utils
111    _po.to_returns = utils.to_returns
112    _po.to_prices = utils.to_prices
113    _po.to_log_returns = utils.to_log_returns
114    _po.log_returns = utils.log_returns
115    _po.exponential_stdev = utils.exponential_stdev
116    _po.rebase = utils.rebase
117    _po.aggregate_returns = utils.aggregate_returns
118    _po.to_excess_returns = utils.to_excess_returns
119    _po.multi_shift = utils.multi_shift
120    _po.curr_month = utils._pandas_current_month
121    _po.date = utils._pandas_date
122    _po.mtd = utils._mtd
123    _po.qtd = utils._qtd
124    _po.ytd = utils._ytd
125
126    # methods that requires benchmark stats
127    _po.r_squared = stats.r_squared
128    _po.r2 = stats.r2
129    _po.information_ratio = stats.information_ratio
130    _po.greeks = stats.greeks
131    _po.rolling_greeks = stats.rolling_greeks
132    _po.compare = stats.compare
133
134    # plotting methods
135    _po.plot_snapshot = plots.snapshot
136    _po.plot_earnings = plots.earnings
137    _po.plot_daily_returns = plots.daily_returns
138    _po.plot_distribution = plots.distribution
139    _po.plot_drawdown = plots.drawdown
140    _po.plot_drawdowns_periods = plots.drawdowns_periods
141    _po.plot_histogram = plots.histogram
142    _po.plot_log_returns = plots.log_returns
143    _po.plot_returns = plots.returns
144    _po.plot_rolling_beta = plots.rolling_beta
145    _po.plot_rolling_sharpe = plots.rolling_sharpe
146    _po.plot_rolling_sortino = plots.rolling_sortino
147    _po.plot_rolling_volatility = plots.rolling_volatility
148    _po.plot_yearly_returns = plots.yearly_returns
149    _po.plot_monthly_heatmap = plots.monthly_heatmap
150
151    _po.metrics = reports.metrics

Extends pandas by exposing methods to be used like: df.sharpe(), df.best('day'), ...