Previous topic

fynance.features.metrics.roll_mdd

Next topic

fynance.features.metrics.roll_z_score

fynance.features.metrics.roll_sharpe

fynance.features.metrics.roll_sharpe(X, rf=0, period=252, w=None, log=False, axis=0, dtype=None, ddof=0)

Compute rolling sharpe ratio of each X’ series.

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

Time-series of prices, performances or index.

rf : float 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.

period : int, optional

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

w : int, 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.

log : bool, 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.

dtype : np.dtype, optional

The type of the output array. If dtype is not given, infer the data type from X input.

ddof : int, 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.

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

[7]https://en.wikipedia.org/wiki/Sharpe_ratio

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])