Rolling Sharpe Ratio (SHARPE)
Summary
The rolling Sharpe ratio: the mean return in excess of a risk-free rate, divided by the standard deviation of that excess return, over a trailing window. A drift-over-volatility measure of the series itself, scaled to a year. Above 0 the window's returns beat the risk-free rate on average; the larger the value, the more return per unit of volatility.
Chart
Chart metadata i
Drawn on the price pane over the close as a line; below it, a separate indicator pane with a zero line showing outReal as a solid line.
Metadata flags used: outReal: line, positive, negative
Reference data (CSV) · Chart (SVG)
Formula
With simple returns , per-bar risk-free rate , excess returns and annualization , over the trailing returns:
The result is by the sign of when every excess return in the window is equal, and NaN when they are also exactly 0.
Notes
- This is Sharpe's 1994 ex post ratio with a constant benchmark, over simple returns and the sample (
n−1) standard deviation. - A window whose returns are all equal has no spread: the result is +Inf or -Inf by the sign of the mean excess return, and NaN when that mean is exactly 0. With an annualization of 0 it is NaN there and 0 elsewhere.
- The risk-free rate is per bar, in the same unit as the returns. For an annual rate
R, pass(1+R)^(1/A) − 1, whereAis the annualization; it is not de-annualized for you. - An annualization of 1 gives the raw per-bar ratio.
- A zero price leaves no return to measure from it: the return of the bar after it counts as 0.
Inputs
inReal— The price series; its returns are computed internally
Outputs
outReal— Annualized Sharpe ratio of the trailing window, or ±Inf or NaN where the window has no spread
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInTimePeriod | integer | 20 | 2–100000 | Number of returns in the window; one price more is read |
optInRiskFreeRate | real | 0 | any real | Risk-free return per bar, subtracted from each return |
optInAnnualization | real | 252 | ≥ 0 | Bars per year; the per-bar ratio is multiplied by its square root |
Properties
Numerical Stability: Start-Independent
| ☐ Overlap Input |
| ✅ Independent Y-Axis i |
| ☐ Candlestick |
| ✅ Can Output NaN or ±Inf i |
| ☐ Identity at Period 1 |
| ☐ Display Shift |
| ☐ Uses Transcendental |
| ☐ Input Must Be Positive |
Example
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double series[252]; /* your data, oldest first */
double out[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_SHARPE( 0, n - 1, series, 20, 0.0, 252.0, &outBegIdx, &outNBElement, out );
printf( "%d values from bar %d; last out = %f\n", outNBElement, outBegIdx, out[outNBElement - 1] );use ta_lib::Core;
let series = vec![0.0; 252]; // your data, oldest first
let mut out = vec![0.0; 252];
let core = Core::new();
let range = core.sharpe(0, series.len() - 1, &series, 20, 0.0, 252.0, &mut out)?;
println!("{} values from bar {}; last out = {}", range.count, range.beg_idx, out[range.count - 1]);import io.github.talib.Core;
import io.github.talib.OutRange;
double[] series = new double[252]; // your data, oldest first
double[] out = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.sharpe(0, series.length - 1, series, 20, 0.0, 252.0, out);
System.out.println(r.count() + " values from bar " + r.begIdx() + "; last out = " + out[r.count() - 1]);using System;
using TALib;
var series = new double[252]; // your data, oldest first
var outReal = new double[252];
var core = Core.Default;
OutRange r = core.Sharpe(0, series.Length - 1, series, 20, 0.0, 252.0, outReal);
Console.WriteLine($"{r.Count} values from bar {r.BegIdx}; last outReal = {outReal[r.Count - 1]}");import { sharpe } from 'ta-lib';
const series = new Float64Array(252); // your data, oldest first
const { begIdx, values } = sharpe(series, {
timePeriod: 20,
riskFreeRate: 0,
annualization: 252,
});
console.log(values.length + ' values from bar ' + begIdx + '; last values = ' + values[values.length - 1]);Implementation
Added in 0.8.4.
TA-Lib Definition: sharpe.c · sharpe.yaml
| Native | File |
|---|---|
| C | ta_SHARPE.c |
| Rust | sharpe.rs |
| Java | Core_SHARPE.java |
| C# | Core_SHARPE.cs |
| TypeScript | sharpe.ts |
Python: talib.SHARPE, see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.
Aliases
Sharpe Ratio, Reward-to-Variability Ratio, Rolling Sharpe
See Also
References
- Sharpe, W. F. "The Sharpe Ratio." The Journal of Portfolio Management, 21(1), Fall 1994, 49-58.