Skip to content

Instantly share code, notes, and snippets.

@harunorimurata
Last active August 16, 2022 03:44
Show Gist options
  • Save harunorimurata/084495d888c441190a1a6dcf5dae327e to your computer and use it in GitHub Desktop.
Save harunorimurata/084495d888c441190a1a6dcf5dae327e to your computer and use it in GitHub Desktop.
function sharpeRatio(array, interest = 0) {
if (array.length < 3) return { mean: 0, sd: 0, sharpeRatio: 0 };
let mean = 0, sqsum = 0, l = array.length;
for (let n = 1; n < l; n++) {
const changeRate = (array[n] - array[n - 1]) / array[n - 1];
const x = changeRate - mean;
mean += x / n;
sqsum += (n - 1) * x * x / n;
}
const sd = Math.sqrt(sqsum / (l - 1));
const sharpeRatio = (mean - interest) / sd;
return { mean, sd, sharpeRatio };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment