This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When backtesting trading stategies with exchange traded funds (ETFs), it is most of the time necessary | |
// to extend these ETFs closing prices time series to have a longer history. | |
// When the underlying index of the ETF is available, one problem is to how to merge the index data | |
// with the ETF data so that there is no price level discontinuity, but before, the first problem to solve | |
// is how to reconciliate potentially different dates between: | |
// - The underlying index calculation dates | |
// - Available trading dates for the other ETFs also used in the trading strategy | |
// Fictious example: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Compute the mean-variance minimum variance frontier, as a list of (portfolio volatility, portfolio return) pairs. | |
* | |
* @param {Array.<number>} returns, the assets returns, e.g. [0.01, 0.05] | |
* @param {Array.<Array.<number>>} covarianceMatrix, the covariance matrix of the assets, e.g. [[1, 0], [0, 1]] | |
* @param {number} portfolios, the number of portfolios to compute on the mean-variance minimum variance frontier, e.g. 25 | |
* | |
* @return {Array.<Array.<number>>}, the list of (portfolio volatility, portfolio return) pairs to be inserted into a Google Spreadsheet | |
**/ | |
function computeMinimumVarianceFrontier(returns, covarianceMatrix, portfolios) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Sub GenerateRandomPortfolios() | |
'Define the Portfolio Optimizer REST API endpoint to target | |
Dim EndPoint As String | |
EndPoint = "https://api.portfoliooptimizer.io/v1/portfolios/generation/random" | |
''Create the JSON body for the REST API request | |
Dim Body As New Dictionary | |
'The number of assets | |
Body.Add "assets", 5 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependencies | |
var fs = require('fs'); | |
const axios = require('axios'); | |
const { mean, std } = require('mathjs'); | |
// Definition of the risk free returns, and of the indexes prices | |
// In sample period | |
var nbPeriods = 12 * 24; // months of returns data, until 30/12/2012 | |
var rf = [0.67,0.70,0.71,0.68,0.69,0.64,0.63,0.63,0.64,0.63,0.61,0.61,0.62,0.63,0.63,0.63,0.62,0.62,0.60,0.60,0.58,0.57,0.57,0.52,0.50,0.49,0.47,0.45,0.45,0.45,0.45,0.43,0.42,0.39,0.36,0.32,0.31,0.32,0.33,0.30,0.30,0.29,0.26,0.26,0.22,0.24,0.27,0.25,0.24,0.24,0.24,0.24,0.25,0.25,0.25,0.25,0.24,0.25,0.26,0.25,0.24,0.28,0.28,0.32,0.34,0.34,0.35,0.37,0.38,0.41,0.45,0.45,0.47,0.47,0.46,0.46,0.46,0.44,0.44,0.43,0.43,0.43,0.43,0.40,0.40,0.40,0.41,0.41,0.41,0.41,0.42,0.42,0.40,0.41,0.41,0.41,0.41,0.41,0.42,0.42,0.39,0.41,0.42,0.41,0.41,0.41,0.41,0.42,0.41,0.42,0.41,0.40,0.40,0.41,0.40,0.39,0.35,0.34,0.36,0.36,0.36,0.37,0.36,0.36,0.37,0.38,0.38,0.40,0.38,0.40,0.42,0.42,0.45,0.46,0.46,0.46,0.45,0.46,0.49,0.50,0.49,0.50,0.49,0.47,0.39,0.39,0.34,0.31,0.29 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('https://api.portfoliooptimizer.io/v1/portfolio/optimization/mean-variance-efficient', | |
{ | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ assets: 13, | |
assetsReturns: [...], | |
assetsCovarianceMatrix: [[...], ...], | |
constraints: { | |
portfolioVolatility: 0.05/Math.sqrt(260), | |
maximumAssetsWeights: [0.20,0.10,0.20,0.20,0.20,0.20,0.20,0.20,0.10,0.10,0.20,0.50,0.50], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('https://api.portfoliooptimizer.io/v1/factors/residualization', | |
{ | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ factors: [{factorReturns: [...], factorResidualization: true | |
factorReturns: [...], | |
... | |
}] | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('https://api.portfoliooptimizer.io/v1/portfolio/optimization/hierarchical-clustering-based-risk-parity', | |
{ | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ assets: 10, | |
assetsCovarianceMatrix: [[1.000647799, -0.003050479, 0.010033224, -0.010759689, -0.005036503, 0.008762563, 0.998201625, -0.001393196, -0.001254522, -0.009365991], | |
[-0.003050479, 1.009021349, 0.008613817, 0.007334478, -0.009492688, 0.013031817, -0.009420720, -0.015346223, 1.010520047, 1.013334849], | |
[0.010033224, 0.008613817, 1.000739363, -0.000637885, 0.001783293, 1.001574768, 0.006385368, 0.001922316, 0.012902050, 0.007997935], | |
[-0.010759689, 0.007334478, -0.000637885, 1.011854725, 0.005759976, 0.000905812, -0.011912269, 0.000461894, 0.012572661, 0.009621670 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('https://api.portfoliooptimizer.io/v1/portfolio/optimization/hierarchical-risk-parity/clustering-based', | |
{ | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ assets: 5, | |
assetsCovarianceMatrix: [[1,1,0,0,0],[1,1,0,0,0],[0,0,1,0,0],[0,0,0,1,1],[0,0,0,1,1]], | |
clusters: 3, | |
acrossClusterAllocationMethod: "equalWeighting", | |
withinClusterAllocationMethod: "equalWeighting", | |
}) |