Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
df[df.index >= _dt(d.year, d.month, d.day)]) * pct
metrics['3Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-3, today.month, today.day)
], 0., compounded) * pct
metrics['5Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-5, today.month, today.day)
], 0., compounded) * pct
metrics['10Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-10, today.month, today.day)
], 0., compounded) * pct
metrics['All-time (ann.) %'] = _stats.cagr(df, 0., compounded) * pct
# best/worst
if mode.lower() == 'full':
metrics['~~~'] = blank
metrics['Best Day %'] = _stats.best(df) * pct
metrics['Worst Day %'] = _stats.worst(df) * pct
metrics['Best Month %'] = _stats.best(df, aggregate='M') * pct
metrics['Worst Month %'] = _stats.worst(df, aggregate='M') * pct
metrics['Best Year %'] = _stats.best(df, aggregate='A') * pct
metrics['Worst Year %'] = _stats.worst(df, aggregate='A') * pct
# dd
metrics['~~~~'] = blank
for ix, row in dd.iterrows():
metrics[ix] = row
metrics['Recovery Factor'] = _stats.recovery_factor(df)
metrics['Ulcer Index'] = _stats.ulcer_index(df, rf)
# win rate
if mode.lower() == 'full':
metrics['~~~~~'] = blank
df[df.index >= _dt(today.year-3, today.month, today.day)
], 0., compounded) * pct
metrics['5Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-5, today.month, today.day)
], 0., compounded) * pct
metrics['10Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-10, today.month, today.day)
], 0., compounded) * pct
metrics['All-time (ann.) %'] = _stats.cagr(df, 0., compounded) * pct
# best/worst
if mode.lower() == 'full':
metrics['~~~'] = blank
metrics['Best Day %'] = _stats.best(df) * pct
metrics['Worst Day %'] = _stats.worst(df) * pct
metrics['Best Month %'] = _stats.best(df, aggregate='M') * pct
metrics['Worst Month %'] = _stats.worst(df, aggregate='M') * pct
metrics['Best Year %'] = _stats.best(df, aggregate='A') * pct
metrics['Worst Year %'] = _stats.worst(df, aggregate='A') * pct
# dd
metrics['~~~~'] = blank
for ix, row in dd.iterrows():
metrics[ix] = row
metrics['Recovery Factor'] = _stats.recovery_factor(df)
metrics['Ulcer Index'] = _stats.ulcer_index(df, rf)
# win rate
if mode.lower() == 'full':
metrics['~~~~~'] = blank
metrics['Avg. Up Month %'] = _stats.avg_win(df, aggregate='M') * pct
metrics['Avg. Down Month %'] = _stats.avg_loss(df, aggregate='M') * pct
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
_po.sortino = stats.sortino
_po.cagr = stats.cagr
_po.rar = stats.rar
_po.skew = stats.skew
_po.kurtosis = stats.kurtosis
metrics['5Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-5, today.month, today.day)
], 0., compounded) * pct
metrics['10Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-10, today.month, today.day)
], 0., compounded) * pct
metrics['All-time (ann.) %'] = _stats.cagr(df, 0., compounded) * pct
# best/worst
if mode.lower() == 'full':
metrics['~~~'] = blank
metrics['Best Day %'] = _stats.best(df) * pct
metrics['Worst Day %'] = _stats.worst(df) * pct
metrics['Best Month %'] = _stats.best(df, aggregate='M') * pct
metrics['Worst Month %'] = _stats.worst(df, aggregate='M') * pct
metrics['Best Year %'] = _stats.best(df, aggregate='A') * pct
metrics['Worst Year %'] = _stats.worst(df, aggregate='A') * pct
# dd
metrics['~~~~'] = blank
for ix, row in dd.iterrows():
metrics[ix] = row
metrics['Recovery Factor'] = _stats.recovery_factor(df)
metrics['Ulcer Index'] = _stats.ulcer_index(df, rf)
# win rate
if mode.lower() == 'full':
metrics['~~~~~'] = blank
metrics['Avg. Up Month %'] = _stats.avg_win(df, aggregate='M') * pct
metrics['Avg. Down Month %'] = _stats.avg_loss(df, aggregate='M') * pct
metrics['Win Days %%'] = _stats.win_rate(df) * pct
metrics['Win Month %%'] = _stats.win_rate(df, aggregate='M') * pct