Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if mode.lower() == 'full':
ret_vol = _stats.volatility(df['returns']) * pct
if "benchmark" in df:
bench_vol = _stats.volatility(df['benchmark']) * pct
metrics['Volatility (ann.) %'] = [ret_vol, bench_vol]
metrics['R^2'] = _stats.r_squared(df['returns'], df['benchmark'])
else:
metrics['Volatility (ann.) %'] = [ret_vol]
metrics['Calmar'] = _stats.calmar(df)
metrics['Skew'] = _stats.skew(df)
metrics['Kurtosis'] = _stats.kurtosis(df)
metrics['~~~~~~~~~~'] = blank
metrics['Expected Daily %%'] = _stats.expected_return(df) * pct
metrics['Expected Monthly %%'] = _stats.expected_return(
df, aggregate='M') * pct
metrics['Expected Yearly %%'] = _stats.expected_return(
df, aggregate='A') * pct
metrics['Kelly Criterion %'] = _stats.kelly_criterion(df) * pct
metrics['Risk of Ruin %'] = _stats.risk_of_ruin(df)
metrics['Daily Value-at-Risk %'] = -abs(_stats.var(df) * pct)
metrics['Expected Shortfall (cVaR) %'] = -abs(_stats.cvar(df) * pct)
metrics['~~~~~~'] = blank
metrics['Payoff Ratio'] = _stats.payoff_ratio(df)
metrics['Profit Factor'] = _stats.profit_factor(df)
metrics['Common Sense Ratio'] = _stats.common_sense_ratio(df)
metrics['CPC Index'] = _stats.cpc_index(df)
ret_vol = _stats.volatility(df['returns']) * pct
if "benchmark" in df:
bench_vol = _stats.volatility(df['benchmark']) * pct
metrics['Volatility (ann.) %'] = [ret_vol, bench_vol]
metrics['R^2'] = _stats.r_squared(df['returns'], df['benchmark'])
else:
metrics['Volatility (ann.) %'] = [ret_vol]
metrics['Calmar'] = _stats.calmar(df)
metrics['Skew'] = _stats.skew(df)
metrics['Kurtosis'] = _stats.kurtosis(df)
metrics['~~~~~~~~~~'] = blank
metrics['Expected Daily %%'] = _stats.expected_return(df) * pct
metrics['Expected Monthly %%'] = _stats.expected_return(
df, aggregate='M') * pct
metrics['Expected Yearly %%'] = _stats.expected_return(
df, aggregate='A') * pct
metrics['Kelly Criterion %'] = _stats.kelly_criterion(df) * pct
metrics['Risk of Ruin %'] = _stats.risk_of_ruin(df)
metrics['Daily Value-at-Risk %'] = -abs(_stats.var(df) * pct)
metrics['Expected Shortfall (cVaR) %'] = -abs(_stats.cvar(df) * pct)
metrics['~~~~~~'] = blank
metrics['Payoff Ratio'] = _stats.payoff_ratio(df)
metrics['Profit Factor'] = _stats.profit_factor(df)
metrics['Common Sense Ratio'] = _stats.common_sense_ratio(df)
metrics['CPC Index'] = _stats.cpc_index(df)
metrics['Tail Ratio'] = _stats.tail_ratio(df)
def extend_pandas():
"""
extends pandas by exposing methods to be used like:
df.sharpe(), df.best('day'), ...
"""
from pandas.core.base import PandasObject as _po
_po.compsum = stats.compsum
_po.comp = stats.comp
_po.expected_return = stats.expected_return
_po.geometric_mean = stats.geometric_mean
_po.ghpr = stats.ghpr
_po.outliers = stats.outliers
_po.remove_outliers = stats.remove_outliers
_po.best = stats.best
_po.worst = stats.worst
_po.consecutive_wins = stats.consecutive_wins
_po.consecutive_losses = stats.consecutive_losses
_po.exposure = stats.exposure
_po.win_rate = stats.win_rate
_po.avg_return = stats.avg_return
_po.avg_win = stats.avg_win
_po.avg_loss = stats.avg_loss
_po.volatility = stats.volatility
_po.implied_volatility = stats.implied_volatility
_po.sharpe = stats.sharpe
bench_vol = _stats.volatility(df['benchmark']) * pct
metrics['Volatility (ann.) %'] = [ret_vol, bench_vol]
metrics['R^2'] = _stats.r_squared(df['returns'], df['benchmark'])
else:
metrics['Volatility (ann.) %'] = [ret_vol]
metrics['Calmar'] = _stats.calmar(df)
metrics['Skew'] = _stats.skew(df)
metrics['Kurtosis'] = _stats.kurtosis(df)
metrics['~~~~~~~~~~'] = blank
metrics['Expected Daily %%'] = _stats.expected_return(df) * pct
metrics['Expected Monthly %%'] = _stats.expected_return(
df, aggregate='M') * pct
metrics['Expected Yearly %%'] = _stats.expected_return(
df, aggregate='A') * pct
metrics['Kelly Criterion %'] = _stats.kelly_criterion(df) * pct
metrics['Risk of Ruin %'] = _stats.risk_of_ruin(df)
metrics['Daily Value-at-Risk %'] = -abs(_stats.var(df) * pct)
metrics['Expected Shortfall (cVaR) %'] = -abs(_stats.cvar(df) * pct)
metrics['~~~~~~'] = blank
metrics['Payoff Ratio'] = _stats.payoff_ratio(df)
metrics['Profit Factor'] = _stats.profit_factor(df)
metrics['Common Sense Ratio'] = _stats.common_sense_ratio(df)
metrics['CPC Index'] = _stats.cpc_index(df)
metrics['Tail Ratio'] = _stats.tail_ratio(df)
metrics['Outlier Win Ratio'] = _stats.outlier_win_ratio(df)
metrics['Outlier Loss Ratio'] = _stats.outlier_loss_ratio(df)