Last active
January 16, 2024 13:00
-
-
Save lequant40/008b303e30dd880d3a232763da6e357e to your computer and use it in GitHub Desktop.
Example of call to Portfolio Optimizer from Excel
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 | |
'The number of portfolios to generate | |
Body.Add "portfolios", 25 | |
'The exposure constraints | |
Dim Constraints As New Dictionary | |
Constraints.Add "minimumPortfolioExposure", 0 | |
Constraints.Add "maximumPortfolioExposure", 1 | |
Body.Add "constraints", Constraints | |
'Create the headers for the REST API request | |
Dim Headers As New Collection | |
'(Optional) API key | |
'Headers.Add WebHelpers.CreateKeyValue("X-API-Key", "my-api-key") | |
Dim Options As New Dictionary | |
Options.Add "Headers", Headers | |
'Send the request to Portfolio Optimizer | |
Dim Client As New WebClient | |
Dim Response As WebResponse | |
Set Response = Client.PostJson(EndPoint, Body, Options) | |
'Decode the JSON reply from Portfolio Optimizer | |
If Response.StatusCode = WebStatusCode.Ok Then | |
'Browse each randomly-generated portfolio assets weights in the response | |
For Each Portfolio In Response.Data("portfolios") | |
For Each AssetsWeight In Portfolio("assetsWeights") | |
'Do something with the asset weight AssetsWeight | |
Next AssetsWeight | |
Next Portfolio | |
Else | |
'HTTP 429 error will typically happen with a free unauthenticated usage, | |
'but it is not managed by this code. | |
Debug.Print "Error: " & Response.Content | |
End If | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment