Skip to content

Instantly share code, notes, and snippets.

@lequant40
lequant40 / hierarchical-clustering-based.js
Last active Oct 21, 2021
Hierarchical Clustering-based portfolio optimization algorithm
View hierarchical-clustering-based.js
fetch('https://api.portfoliooptimizer.io/v1/portfolio/optimization/hierarchical-clustering-based-risk-parity',
{
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",
})
@lequant40
lequant40 / hierarchical-risk-parity.js
Last active Oct 18, 2021
Hierarchical Risk Parity portfolio optimization algorithm
View hierarchical-risk-parity.js
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
@lequant40
lequant40 / residualization-example.js
Last active Jul 26, 2021
residualization-example
View residualization-example.js
fetch('https://api.portfoliooptimizer.io/v1/factors/residualization',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ factors: [{factorReturns: [...],
factorReturns: [...],
...
}],
residualizedFactor: .
})
View efficiente-index-reproduction
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],
@lequant40
lequant40 / gem_random_portfolios.js
Last active May 28, 2021
Illustration of random portfolios analysis of the Global Equities Momentum (GEM) TAA strategy
View gem_random_portfolios.js
// 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
@lequant40
lequant40 / random_portfolios_from_excel.vba
Last active Jun 15, 2021
Example of call to Portfolio Optimizer from Excel
View random_portfolios_from_excel.vba
Public Sub GenerateRandomPortfolios()
'Define the Portfolio Optimizer REST API endpoint to target
Dim EndPoint As String
EndPoint = "https://api.portfoliooptimizer.io/v1/portfolio/construction/random"
''Create the JSON body for the REST API request
Dim Body As New Dictionary
'The number of assets
Body.Add "assets", 5
@lequant40
lequant40 / arithmetic_returns_from_webpage.js
Last active Jun 15, 2021
Exemple of call to Portfolio Optimizer API, to comput (arithmetic) assets returns from assets prices, directly from a webpage
View arithmetic_returns_from_webpage.js
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
@lequant40
lequant40 / minimum_variance_frontier_appsscript.js
Last active Jun 15, 2021
Exemple of call to Portfolio Optimizer API, to compute the mean-variance minimum variance frontier
View minimum_variance_frontier_appsscript.js
/**
* 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) {
@lequant40
lequant40 / alignDates.gs
Last active Jun 5, 2017
Extending ETFs time series with proxies - How to align dates and values
View alignDates.gs
// 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: