Last active
June 15, 2021 09:32
-
-
Save lequant40/dc3433dd722a12c81dd9504d31c7e7d9 to your computer and use it in GitHub Desktop.
Exemple of call to Portfolio Optimizer API, to comput (arithmetic) assets returns from assets prices, directly from a webpage
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 | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment