Changelog)
PortfolioAnalytics v0.0.4 (In order to track my personal stock market investments performances, as well as to analyse trading strategies on my blog Le Quant 40, I wanted to use portfolio performances measures computed in JavaScript.
Why in JavaScript ? Because I am a fan of Google Sheets, which is easily extensible thanks to Google Apps Script, a JavaScript-based language.
After several fruitless hours of Googling (incomplete codes, incorrect codes, undocumented codes...), I decided to create my own JavaScript library of such portfolio performances measures, hoping that it could be useful to other people...
Features
- Compatible with Google Sheets
- Compatible with any browser supporting ECMAScript 5 (i.e., front-end development)
- Compatible with Node.js (i.e., back-end development)
- (Performances) Automatically uses JavaScript Typed Arrays
- (Accuracy) Internally uses accurate numerical algorithms (e.g., corrected two pass algorithms for mean, variance, skewness and kurtosis, accurate algorithm for error function...)
- Code continuously tested and integrated by Travis CI
- Code heavily documented using JSDoc
Usage
Usage in Google Sheets
If you would like to use PortfolioAnalytics in Google Sheets, you can either:
- (Recommended) Import the external Google Apps Script library with Script ID 1NXwj16pdgcJT-XG5LiWRJyRW604Dj26U4lqgGsJJfOKLum4y9grakXPI into your spreadsheet script
or:
- Import the JavaScript files from the dist/gs directory into your spreadsheet script
In both cases, providing data to the PortfolioAnalytics functions is then accomplished your preferred way:
- Using a wrapper function in your spreadsheet script, directly accessible from your spreadsheet, to which you can provide a standard data range (A1:A99...), e.g.:
{ // Convert the input range coming from the spreadsheet into an array var aInternalArray = ; for var i=0; i<iEquityCurveRangelength; ++i aInternalArray; // Compute the index var ulcerIndex = PortfolioAnalytics; // Return it to the spreadsheet return ulcerIndex;}
- Using pure Google Apps Script functions - typically the getRange(...) familly of functions -, optimized for speed, e.g.:
{ // Adapted from https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangerow-column-numrows var ss = SpreadsheetApp; var sheet = ss0; var range = sheet; // A1:A100 var values = range; // Convert the above range into an array var aInternalArray = ; for var row in values for var col in valuesrow aInternalArray; // Compute the index var ulcerIndex = PortfolioAnalytics; // Do something with it (use it in a computation, write it back to the spreadsheet, etc.) ...}
You can find examples of PortfolioAnalytics usage in this spreadsheet.
Usage inside a browser
If you would like to use PortfolioAnalytics inside a browser you can download its source code and/or its minified source code.
You then just need to include this code in an HTML page, e.g.:
To be noted that if the browser is compatible with JavaScript Typed Arrays, you can provide such arrays in input to PortfolioAnalytics for better performances, e.g.:
PortfolioAnalytics.arithmeticReturns(new Float64Array([100.0, 109.75, 111.25]))// Will output a Float64Array
Usage with Node.js
If you would like to use PortfolioAnalytics with Node.js, you simply need to declare it as a dependency of your project
in your package.json
file.
Then, this is standard Node.js code, e.g.:
var PortfolioAnalytics = ;...var ui = PortfolioAnalytics;// ui == 0.07204222820421435
Examples
Drawdowns related measures
PortfolioAnalytics; // The maximum drawdown PortfolioAnalytics; // The drawdown function PortfolioAnalytics; // The top 'n' drawdowns (second largest drawdown, etc.) with their start/end indexes PortfolioAnalytics;// The Ulcer Index PortfolioAnalytics;// The Pain Index, also corresponding to the average of the drawdown function PortfolioAnalytics;// The conditional drawdown
Returns related measures
PortfolioAnalytics; // The cumulative return from first to last period PortfolioAnalytics; // The compound annual growth rate (CAGR) from first to last date PortfolioAnalytics; // The arithmetic returns for all periods PortfolioAnalytics;// The (percent) value at risk
Sharpe ratio related measures
PortfolioAnalytics; // The Sharpe ratio PortfolioAnalytics; // The Sharpe ratio adjusted for its bias PortfolioAnalytics; // The double Sharpe ratio (i.e., the Sharpe ratio, adjusted for its estimation risk) PortfolioAnalytics; // The confidence interval for the Sharpe ratio (here, at 5% significance level) PortfolioAnalytics; // The probabilistic Sharpe ratio (i.e., the probability that the Sharpe ratio is greater // than a reference Sharpe ratio, here 0) PortfolioAnalytics; // The minimum track record length (i.e., the minimal length of the track record of the performance // to have statistical confidence, here at 95%, that the Sharpe ratio is greater than a reference Sharpe ratio, here 0)
Returns to variability related measures
PortfolioAnalytics; // The gain to pain ratio
How to contribute ?
Github...
Fork the projet fromGrunt dependencies
Instal thenpm install
Develop...
Compile
- The following command generates the files to be used inside a browser or with Node.js in the
dist
directory:
grunt deliver
- The following command generates the files to be used in Google Sheets in the
dist\gs
directory:
grunt deliver-gs
Test
Any of the following two commands run the QUnit unit tests contained in the test
directory on the generated file dist\portfolio_analytics.dev.min.js
:
npm test
grunt test