Quick start
Este guia inicial do GorilaCORE tem como exemplo um cenário simples de utilização das APIs.
Configurações básicas
- Python
- JavaScript
import requests
# GorilaCORE url
base_url = "https://core.gorila.com.br"
# Preparar o cabeçalho para autenticação usando a sua chave API
authorization = {"Authorization": "c1deeef8-4812-4bd3-903e-8adeaff3903d"}
import axios from 'axios';
// GorilaCORE url
const baseUrl = 'https://core.gorila.com.br'
// Preparar o cabeçalho para autenticação usando a sua chave API
const authorization = {
headers: { 'Authorization': 'c1deeef8-4812-4bd3-903e-8adeaff3903d' }
}
Criar um Portfolio
- Python
- JavaScript
response = requests.post(
f"{base_url}/portfolios",
json={"name": "ALVARO DIAS PEREIRA", "autoRnC": False},
headers=authorization,
)
response.json()
'''
{
'id': 'e3021f03-0af7-4571-be92-ba97b537c89d',
'name': 'ALVARO DIAS PEREIRA',
'autoRnC': False
}
'''
const response = await axios.post(
`${baseUrl}/portfolios`,
{ name: "ALVARO DIAS PEREIRA", autoRnC: false },
authorization,
)
console.log(response.data)
/*
{
id: 'e3021f03-0af7-4571-be92-ba97b537c89d',
name: 'ALVARO DIAS PEREIRA',
autoRnC: False
}
*/
Incluindo algumas Transações (Transactions
)
- Python
- JavaScript
portfolio_id = "e3021f03-0af7-4571-be92-ba97b537c89d"
transactions = [
{
"type": "REGULAR",
"transactDate": "2022-10-03",
"quantity": 200,
"brokerId": "18945670000146",
"security": {
"isin": "BRPETRACNPR6", # PETR4
},
"side": "BUY",
"price": 24.28,
"fees": {
"brokerageFee": 8.00,
}
},
{
"type": "REGULAR",
"transactDate": "2022-10-18",
"brokerId": "27652684000162",
"security": {
"isin": "BRSTNCNTB4O9", # NTN-B
},
"side": "BUY",
"price": 4027.058131,
"quantity": 30.0,
},
{
"type": "REGULAR",
"transactDate": "2022-10-27",
"quantity": 105,
"brokerId": "18945670000146",
"security": {
"isin": "BRPETRACNPR6", # PETR4
},
"side": "SELL",
"price": 27.15,
"fees": {
"brokerageFee": 0.15,
}
},
]
for txn in transactions:
requests.post(
f"{base_url}/portfolios/{portfolio_id}/transactions",
json=txn,
headers=authorization,
)
const portfolioId = "e3021f03-0af7-4571-be92-ba97b537c89d"
const transactions = [
{
"type": "REGULAR",
"transactDate": "2022-10-03",
"quantity": 200,
"brokerId": "18945670000146",
"security": {
"isin": "BRPETRACNPR6", // PETR4
},
"side": "BUY",
"price": 24.28,
"fees": {
"brokerageFee": 8.00,
}
},
{
"type": "REGULAR",
"transactDate": "2022-10-18",
"brokerId": "27652684000162",
"security": {
"isin": "BRSTNCNTB4O9", // NTN-B
},
"side": "BUY",
"price": 4027.058131,
"quantity": 30.0,
},
{
"type": "REGULAR",
"transactDate": "2022-10-27",
"quantity": 105,
"brokerId": "18945670000146",
"security": {
"isin": "BRPETRACNPR6", // PETR4
},
"side": "SELL",
"price": 27.15,
"fees": {
"brokerageFee": 0.15,
}
}
]
for (const txn of transactions) {
await axios.post(
`${baseUrl}/portfolios/${portfolioId}/transactions`,
txn,
authorization,
)
}
Agora que adicionamos algumas transações a um portfólio, podemos começar a buscar dados úteis sobre ele.
Para as próximas chamadas de API, vamos examinar um período específico de um mês: de 2022-10-01
a 2022-10-31
.
Obter o Valor Patrimonial Líquido (NAV) do Portfolio
- Python
- JavaScript
portfolio_id = "e3021f03-0af7-4571-be92-ba97b537c89d"
params = {
"startDate": "2022-10-01",
"endDate": "2022-10-31",
"frequency": "DAILY",
}
response = requests.get(
f"{base_url}/portfolios/{portfolio_id}/nav",
params=params,
headers=authorization,
)
response.json()
"""
{
'timeseries': [
{ 'referenceDate': '2022-10-01', 'nav': 0, 'currency': 'BRL' },
{ 'referenceDate': '2022-10-02', 'nav': 0, 'currency': 'BRL' },
{ 'referenceDate': '2022-10-03', 'nav': 1572, 'currency': 'BRL' },
{ 'referenceDate': '2022-10-04', 'nav': 1410, 'currency': 'BRL' },
...
]
}
"""
const portfolioId = "e3021f03-0af7-4571-be92-ba97b537c89d"
const params = {
startDate: "2022-10-01",
endDate: "2022-10-31",
frequency: "DAILY",
}
const response = await axios.get(
`${baseUrl}/portfolios/${portfolioId}/nav`,
{ params, ...authorization }
)
console.log(response.data)
/*
{
timeseries: [
{ referenceDate: '2022-10-01', nav: 0, currency: 'BRL' },
{ referenceDate: '2022-10-02', nav: 0, currency: 'BRL' },
{ referenceDate: '2022-10-03', nav: 1572, currency: 'BRL' },
{ referenceDate: '2022-10-04', nav: 1410, currency: 'BRL' },
...
]
}
*/
Obter o Lucro e Prejuízo (P&L) do Portfolio
- Python
- JavaScript
portfolio_id = "e3021f03-0af7-4571-be92-ba97b537c89d"
params = {
"startDate": "2022-10-01",
"endDate": "2022-10-31",
"frequency": "DAILY",
"seriesType": "ACCUMULATED",
}
response = requests.get(
f"{base_url}/portfolios/{portfolio_id}/pnl",
params=params,
headers=authorization,
)
response.json()
"""
{
'timeseries': [
{ 'referenceDate': '2022-10-03', 'pnl': 1580, 'currency': 'BRL' },
{ 'referenceDate': '2022-10-04', 'pnl': 1418, 'currency': 'BRL' },
{ 'referenceDate': '2022-10-05', 'pnl': 1654, 'currency': 'BRL' },
{ 'referenceDate': '2022-10-06', 'pnl': 1876, 'currency': 'BRL' },
...
]
}
"""
const portfolioId = "e3021f03-0af7-4571-be92-ba97b537c89d"
const params = {
startDate: "2022-10-01",
endDate: "2022-10-31",
frequency: "DAILY",
seriesType: "ACCUMULATED",
}
const response = await axios.get(
`${baseUrl}/portfolios/${portfolioId}/pnl`,
{ params, ...authorization }
)
console.log(response.data)
/*
{
timeseries: [
{ referenceDate: '2022-10-03', pnl: 1580, currency: 'BRL' },
{ referenceDate: '2022-10-04', pnl: 1418, currency: 'BRL' },
{ referenceDate: '2022-10-05', pnl: 1654, currency: 'BRL' },
{ referenceDate: '2022-10-06', pnl: 1876, currency: 'BRL' },
...
]
}
*/
Obter o Retorno Ponderado pelo Tempo (TWR) do Portfolio
- Python
- JavaScript
portfolio_id = "e3021f03-0af7-4571-be92-ba97b537c89d"
params = {
"startDate": "2022-10-01",
"endDate": "2022-10-31",
"frequency": "DAILY",
"seriesType": "PER_PERIOD",
}
response = requests.get(
f"{base_url}/portfolios/{portfolio_id}/twr",
params=params,
headers=authorization,
)
response.json()
"""
{
'timeseries': [
{ 'referenceDate': '2022-10-03', 'twr': 0 },
{ 'referenceDate': '2022-10-04', 'twr': -0.10305343509999998 },
{ 'referenceDate': '2022-10-05', 'twr': 0.1673758864999999 },
{ 'referenceDate': '2022-10-06', 'twr': 0.13487241800000005 },
...
]
}
"""
const portfolioId = "e3021f03-0af7-4571-be92-ba97b537c89d"
const params = {
startDate: "2022-10-01",
endDate: "2022-10-31",
frequency: "DAILY",
seriesType: "PER_PERIOD"
}
const response = await axios.get(
`${baseUrl}/portfolios/${portfolioId}/twr`,
{ params, ...authorization }
)
console.log(response.data)
/*
{
timeseries: [
{ referenceDate: '2022-10-03', twr: 0 },
{ referenceDate: '2022-10-04', twr: -0.10305343509999998 },
{ referenceDate: '2022-10-05', twr: 0.1673758864999999 },
{ referenceDate: '2022-10-06', twr: 0.13487241800000005 },
...
]
}
*/
Obter a Taxa Interna de Retorno (IRR) do Portfolio
- Python
- JavaScript
portfolio_id = "e3021f03-0af7-4571-be92-ba97b537c89d"
params = {
"startDate": "2022-10-01",
"endDate": "2022-10-31",
}
response = requests.get(
f"{base_url}/portfolios/{portfolio_id}/positions/irr",
params=params,
headers=authorization,
)
response.json()
"""
{
"next": None,
"records": [
{
"broker": {
"id": "18945670000146",
"name": "INTER DTVM"
},
"irr": 0.12034529944391072,
"referenceDate": "2022-10-31",
"security": {
"assetClass": "STOCKS",
"id": 122720,
"isin": "BRPETRACNPR6",
"name": "PETR4",
"type": "STOCK_LOCAL",
"validityDate": "2023-09-01T00:00:00Z"
}
},
{
"broker": {
"id": "18945670000146",
"name": "INTER DTVM"
},
"irr": 0,
"referenceDate": "2022-10-31",
"security": {
"assetClass": "CASH",
"id": 177223,
"name": "BRL",
"type": "CASH",
"validityDate": "1994-07-01T00:00:00Z"
}
},
{
"broker": {
"id": "27652684000162",
"name": "GENIAL INVESTIMENTOS"
},
"irr": 0,
"referenceDate": "2022-10-31",
"security": {
"assetClass": "CASH",
"id": 177223,
"name": "BRL",
"type": "CASH",
"validityDate": "1994-07-01T00:00:00Z"
}
},
{
"broker": {
"id": "27652684000162",
"name": "GENIAL INVESTIMENTOS"
},
"irr": 0.005959030443407324,
"referenceDate": "2022-10-31",
"security": {
"assetClass": "FIXED_INCOME",
"dealType": "spread",
"id": 118697,
"index": "ipca",
"isin": "BRSTNCNTB4O9",
"maturityDate": "2023-05-15T00:00:00Z",
"name": "NTNB - 15/05/2023",
"type": "TREASURY_LOCAL_NTNB",
"validityDate": "2000-07-15T00:00:00Z",
"yield": 0.06
}
}
]
}
"""
const portfolioId = "e3021f03-0af7-4571-be92-ba97b537c89d"
const params = {
startDate: "2022-10-01",
endDate: "2022-10-31",
}
const response = await axios.get(
`${baseUrl}/portfolios/${portfolio_id}/positions/irr`,
{ params, ...authorization }
)
console.log(response.data)
/*
{
"next": null,
"records": [
{
"broker": {
"id": "18945670000146",
"name": "INTER DTVM"
},
"irr": 0.12034529944391072,
"referenceDate": "2022-10-31",
"security": {
"assetClass": "STOCKS",
"id": 122720,
"isin": "BRPETRACNPR6",
"name": "PETR4",
"type": "STOCK_LOCAL",
"validityDate": "2023-09-01T00:00:00Z"
}
},
{
"broker": {
"id": "18945670000146",
"name": "INTER DTVM"
},
"irr": 0,
"referenceDate": "2022-10-31",
"security": {
"assetClass": "CASH",
"id": 177223,
"name": "BRL",
"type": "CASH",
"validityDate": "1994-07-01T00:00:00Z"
}
},
{
"broker": {
"id": "27652684000162",
"name": "GENIAL INVESTIMENTOS"
},
"irr": 0,
"referenceDate": "2022-10-31",
"security": {
"assetClass": "CASH",
"id": 177223,
"name": "BRL",
"type": "CASH",
"validityDate": "1994-07-01T00:00:00Z"
}
},
{
"broker": {
"id": "27652684000162",
"name": "GENIAL INVESTIMENTOS"
},
"irr": 0.005959030443407324,
"referenceDate": "2022-10-31",
"security": {
"assetClass": "FIXED_INCOME",
"dealType": "spread",
"id": 118697,
"index": "ipca",
"isin": "BRSTNCNTB4O9",
"maturityDate": "2023-05-15T00:00:00Z",
"name": "NTNB - 15/05/2023",
"type": "TREASURY_LOCAL_NTNB",
"validityDate": "2000-07-15T00:00:00Z",
"yield": 0.06
}
}
]
}
*/