Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

A JavaScript library to allocate portfolios of financial assets.

License

Notifications You must be signed in to change notification settings

DiogoAngelim/portfolio_allocation_js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PortfolioAllocation v0.0.6 (Changelog)

Travis Build Status

PortfolioAllocation is a JavaScript library to allocate portfolios of fincancial assets, i.e. to compute the proportions of a given set of financial instruments (stocks, bonds, exchange traded funds - ETFs, mutual funds...) to hold in a portfolio so as to optimize specific quantitative criteria related to this portfolio.

PortfolioAllocation has been developed in JavaScript because I heavily use Google Sheets to analyse trading strategies on my blog Le Quant 40, as well as in my personal trading, and Google Sheets is easily extensible thanks to Google Apps Script, a JavaScript-based language.

I hope you will enjoy !

Features

  • Compatible with Google Sheets
  • Compatible with any browser supporting ECMAScript 5 for front-end development
  • Compatible with Node.js for back-end development
  • Code continuously tested and integrated by Travis CI
  • Code heavily documented using JSDoc

Portfolio allocation algorithms

Helper algorithms

  • Portfolio weights rounding
    The rounding algorithm described in the research paper Rounding on the standard simplex: Regular grids for global optimization from Immanuel M. Bomze and al. allows to compute real-life portfolio weights from the theoretical portfolio weights obtained from a portfolio allocation algorithm.

  • Portfolio numerical optimization
    Numerical optimization algorithms (grid search...) allow to determine numerically the weights of the optimal portfolio(s) minimizing a given objective function.

  • Mean-variance efficient frontier and corner portfolios computation, through the Markowitz critical line algorithm
    The set of all mean-variance efficient portfolios (the mean-variance efficient frontier), as well its generating discrete set (the set of corner portfolios) can be efficiently computed by a specialized algorithm developped by Harry M. Markowitz.

Usage

Usage in Google Sheets

If you would like to use PortfolioAllocation in Google Sheets, you can either:

or:

  • Import the JavaScript files from the dist/gs directory into your spreadsheet script

In both cases, calling the PortfolioAllocation 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 standard data ranges (A1:B3...), e.g.:
function computeERCPortfolioWeights(covarianceMatrix) {
  // Note: The input range coming from the spreadsheet and representing a covariance matrix
  // is directly usable.
    
  // Compute the ERC portfolio weights
  var ercWeights = PortfolioAllocation.equalRiskContributionWeights(covarianceMatrix);
  
  // Return them to the spreadsheet
  return ercWeights;
}
  • Using pure Google Apps Script functions, for which computations are allowed by Google to last longer, e.g.:
function computeERCPortfolioWeights() {
  // Adapted from https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangerow-column-numrows
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 var range = sheet.getRange(1, 1, 3, 3); // A1:B3
 var values = range.getValues();

 // Convert the above range into an array
 var covarianceMatrix = [];
 for (var row in values) {
   for (var col in values[row]) {
     covarianceMatrix.push(values[row][col]);
   }
 }
 
  // Compute the ERC portfolio weights
  var ercWeights = PortfolioAllocation.equalRiskContributionWeights(covarianceMatrix);
  
  // Do something with them (use them in a computation, write them back to the spreadsheet, etc.)
  ...
}

You can find examples of PortfolioAllocation usage in this spreadsheet.

Usage inside a browser

If you would like to use PortfolioAllocation 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 to use it, e.g.:

<script src="portfolio_allocation.dist.min.js" type="text/javascript"></script>
<script type="text/javascript">
  var w = PortfolioAllocation.riskBudgetingWeights([[0.1,0], [0,0.2]], [0.25, 0.75]);
  // w = [0.44948974243459944, 0.5505102575654006]
</script>

Usage with Node.js

If you would like to use PortfolioAllocation 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 PortfolioAllocation = require('portfolio-allocation');
...
var w = PortfolioAllocation.riskBudgetingWeights([[0.1,0], [0,0.2]], [0.25, 0.75]);
// w = [0.44948974243459944, 0.5505102575654006]

Examples

Risk-based portfolio allocations

PortfolioAllocation.equalWeights(5); 
// EW portfolio

PortfolioAllocation.equalRiskContributionWeights([[0.1, 0], [0, 0.2]]); 
// ERC portfolio

PortfolioAllocation.mostDiversifiedWeights([[0.1, 0], [0, 0.2]], {eps: 1e-10, maxIter: 10000});
// MDP portfolio

PortfolioAllocation.minCorrWeights([[0.1, 0], [0, 0.2]]);
// MCA portfolio

PortfolioAllocation.clusterRiskParityWeights([[0.1,0], [0,0.2]], {clusteringMode: 'ftca'});
// CRP portfolio

How to contribute ?

Fork the project from Github...

Instal the Grunt dependencies and command line

npm install
npm install -g grunt-cli

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_allocation.dev.min.js:

npm test
grunt test

Submit a pull-request...

License

MIT License

About

A JavaScript library to allocate portfolios of financial assets.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.3%
  • HTML 0.7%