sharpe¶
Defined in fynance.features.metrics
- sharpe(X, rf=0, period=252, log=False, axis=0, dtype=None, ddof=0)[source]
Compute the Sharpe ratio for each X’ series.
Annualized excess return per unit of volatility — the most widely used risk-adjusted performance metric. Higher is better; ratios above 1 are usually considered good and above 2 excellent for long-horizon strategies. Note that the Sharpe ratio penalizes both upside and downside volatility symmetrically; use the Sortino or Calmar ratio (
calmar) when only downside risk should be penalized.The
periodargument controls annualization (252 for daily trading data, 12 for monthly, etc.). For a rolling estimate, seeroll_sharpe.- Parameters:
- Xnp.ndarray[dtype, ndim=1 or 2]
Time-series of prices, performances or index.
- rffloat, optional
Means the annualized risk-free rate, default is 0.
- periodint, optional
Number of period per year, default is 252 (trading days).
- logbool, optional
If true compute sharpe with the formula for log-returns, default is False.
- axis{0, 1}, optional
Axis along wich the computation is done. Default is 0.
- dtypenp.dtype, optional
The type of the output array. If dtype is not given, infer the data type from X input.
- ddofint, optional
Means Delta Degrees of Freedom, the divisor used in calculations is
T - ddof, whereTrepresents the number of elements in time axis. Default is 0.
- Returns:
- dtype or np.ndarray[dtype, ndim=1]
Value of Sharpe ratio for each series.
See also
mdd,calmar,drawdown,roll_sharpe
Notes
Sharpe ratio [7] is computed as the annualized expected returns (
annual_return) minus the risk-free rate (noted \(rf\)) over the annualized volatility of returns (annual_volatility) such that:\[\begin{split}sharpeRatio = \frac{E(R) - rf}{\sqrt{period \times Var(R)}} \\ \\\end{split}\]where, \(R_1 = 0\) and \(R_{2:T} = \begin{cases}ln(\frac{X_{2:T}} {X_{1:T-1}}) \text{, if log=True}\\ \frac{X_{2:T}}{X_{1:T-1}} - 1 \text{, otherwise} \\ \end{cases}\)
References
Examples
Assume a series X of monthly prices:
>>> X = np.array([70, 100, 80, 120, 160, 80]).astype(np.float64) >>> sharpe(X, period=12) 0.24475518072327812 >>> sharpe(X.reshape([6, 1]), period=12) array([0.24475518])