Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Am I calculating the sortino ratio correctly?

I wrote a node.js script to calculate the sortino ratio, because I wanted to know the sortino ratio for different cryptocurrencies. In the code I assume the risk free rate is 1%, I'll fix it later. It seem doesn't have much affect the calculation.

Here's the csv and json data: https://gist.github.com/x13machine/faa4942b939a0673cb39fd4bbb62c4e4#file-bitcoin-json

process.on('uncaughtException', function (err) {  
});

var fs = require('fs');

var coin = process.argv[2] || 'bitcoin';  
var prices = JSON.parse(fs.readFileSync('./coins/'+ coin + '.json', 'utf-8'));  
var stats = require("stats-lite")

var data = [];  
prices.forEach(function(row, i){  
    var date = new Date(row.Date);  
    if(date.getDate() !== 1)return ;  
    data.unshift({  
        date: date,  
        price: row.Close  
    });  
});

function norm(x){  
    return (x - 1) * 100;  
}

var min = norm(1.01 ** (1/12));

var rets = [];  
data.forEach(function(row,i){  
    if(i === 0)return ;  
    var lp = data[i - 1].price;  
    var ret = norm(row.price / lp);  
    if(ret < min)rets.push(ret);  
    //console.log(ret);  
});


var tr = data[data.length - 1].price / data[0].price;  
var years = (data[data.length - 1].date - data[0].date) / (24 * 60 * 60 * 1000 * 365.25);

var apr = norm(tr ** (1 / years));  
var st = stats.stdev(rets);  
console.log((apr - min) / st);
1 response

Formulas for alpha, beta, sharpe, sortino and information ratio in case might be helpful.

https://www.quantopian.com/posts/corrections-to-cumulative-risk-metrics