Integration in a Web Page

1 minute read

In this post, I will show how to integrate the Portfolio Optimizer Web API in a web page.

As a working example, I will display the mean-variance minimum variance frontier1 of 4 indexes.

In addition, I will also display the returns and the volatilities of portfolios made of random weights, to allow a visual comparison between mean-variance return-efficient portfolios and random portfolios.

Note: A fully functional web page corresponding to this post is available … right before your eyes :eyes:! Just check the source code of this page.

Integration of the Portfolio Optimizer API in a web pagePermalink

Cross-origin requests are supported by the Portfolio Optimizer API.

This allows to easily integrate Portfolio Optimizer in a web page through the browsers native JavaScript XMLHttpRequest objects, or through any JavaScript library offering AJAX capabilities, like jQuery.

As an example, the following native JavaScript function calls the Arithmetic Returns API endpoint, like it is done in the web page you are currently viewing:

function arithmeticReturns(assetsPrices) {
var xhr = new XMLHttpRequest();
var url = "https://api.portfoliooptimizer.io/v1/assets/returns";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE) {
// Status 200 == all good
if (this.status === 200) {
// Parse the response as JSON
var json = JSON.parse(xhr.responseText);
// Decode the assets returns, c.f. the documentation
// at https://docs.portfoliooptimizer.io/
var assets = json.assets;
// Do whatever is needed with the assets returns
//
}
// Status 429 == there are too many requests in parallel
//
// Waiting 1s before sending the same XHR query again is required with
// the free usage of Portfolio Optimizer
else if (this.status === 429) {
setTimeout(function(){ arithmeticReturns(assetsPrices) }, 1000); ;
}
}
};
var data = JSON.stringify({"assets": assetsPrices.length,
"assetsPrices":assetsPrices});
xhr.send(data);
}

Example of integration: displaying the mean-variance minimum variance frontierPermalink

I will use Portfolio Optimizer to display the mean-variance minimum variance frontier of the following 4 indexes:

  • ICE US Treasury Short Bond
  • ICE US Treasury 20+ Year Bond
  • S&P 500
  • Gold spot price

PricesPermalink

Note: The index prices come from https://backtest.curvo.eu/. Be sure to check this site if you are an European investor in ETFs!

2005-122006-012006-022006-032006-042006-052006-062006-072006-082006-092006-102006-112006-122007-012007-022007-032007-042007-052007-062007-072007-082007-092007-102007-112007-122008-01Date10000.009761.849991.819840.029532.769322.359471.249472.669452.249637.589646.909317.589378.659572.009423.429389.079228.959374.329376.479275.989337.029060.858921.758783.698829.418799.22ICE US Treasury Short Bond12528.1112041.9612436.6611642.5710924.3710652.3910885.3611063.8411348.2711733.7611805.8411621.8411322.5411389.5511563.5011265.3711124.7611003.5810846.3211026.5711236.0110892.1710863.3311190.8111170.4511291.58ICE US Treasury 20+ Year Bond77826.7677771.3379578.1879044.5177338.9373181.0574173.5274315.7475586.6178704.0981038.1079426.4380723.9883311.1280092.4780337.7782126.7285952.6884199.3580386.2881603.0581825.0481584.6076510.7476186.4470899.09S&P 50026501.2228602.9328533.9129303.1431304.9030925.8929409.4330191.9929567.8428846.5928980.8029857.2029416.2030602.9730639.6130281.3430325.6629857.4229354.3729588.6929882.0531934.7433303.8632347.6934629.7037838.02Gold spot price
Indexes monthly prices (in EUR, base 10000)

Average returns and covariance matrixPermalink

One of the first step in an historical mean-variance analysis is to compute:

  • The historical mean arithmetic returns of the analyzed assets
  • The historical covariance matrix of the analyzed assets

With Portfolio Optimizer, this is done thanks to the following API endpoints:

Which gives:

Mean-variance minimum variance frontierPermalink

Thanks to the computed average arithmetic returns and covariance matrix, it is possible to plot the mean-variance minimum variance frontier of the analyzed assets.

With Portfolio Optimizer, this is done thanks to the /v1/portfolio/analysis/mean-variance/minimum-variance-frontier API endpoint.

Which gives:

Random portfoliosPermalink

With Portfolio Optimizer, it is also possible to simulate random portfolios, which are portfolios made of random assets weights.

This is done thanks to the /v1/portfolios/simulation/random API endpoint.

Which gives, on the same graph as the mean-variance minimum variance frontier:

  1. The mean-variance minimum variance frontier is the set of portfolios in the (V,E) plane with the lowest volatility for any given return. 

Tags:

Updated: