roll_sharpe

Defined in fynance.features.metrics

roll_sharpe(X, rf=0, period=252, w=None, log=False, axis=0, dtype=None, ddof=0)[source]

Compute rolling sharpe ratio of each X’ series.

Parameters:
Xnp.ndarray[dtype, ndim=1 or 2]

Time-series of prices, performances or index.

rffloat or np.ndarray[dtype, ndim=1 or 2], optional

Means the annualized risk-free rate, default is 0. If an array is passed, it must be of the same shape than X.

periodint, optional

Number of period per year, default is 252 (trading days).

wint, optional

Size of the lagged window of the rolling function, must be positive. If w is None or w=0, then w=X.shape[axis]. Default is None.

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, where T represents the number of elements in time axis. Default is 0.

Returns:
np.ndarray[dtype, ndim=1 or 2]

Serires of rolling Sharpe ratio.

See also

roll_calmar, sharpe, roll_mdd

Notes

Sharpe ratio [7] is computed as the rolling annualized expected returns (roll_annual_return) minus the risk-free rate (noted \(rf\)) over the rolling annualized volatility of returns (roll_annual_volatility) such that \(\forall t \in [1:T]\):

\[sharpeRatio_t = \frac{E(R | R_{1:t}) - rf_t}{\sqrt{period \times Var(R | R_{1:t})}}\]

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 monthly series of prices:

>>> X = np.array([70, 100, 80, 120, 160, 80]).astype(np.float64)
>>> roll_sharpe(X, period=12)
array([ 0.        , 10.10344078,  0.77721579,  3.99243019,  6.754557  ,
        0.24475518])