-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-iol.js
196 lines (173 loc) · 5.48 KB
/
node-iol.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const axios = require('axios');
const querystring = require('querystring');
const fs = require('fs').promises;
const credentials = require ('./auth.json');
let apiUrl = 'https://api.invertironline.com/api/v2';
async function getAccountStatus (token) {
let res = await axios.get(`${apiUrl}/estadocuenta`,
{
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getPortfolioArgentina (token) {
let res = await axios.get(`${apiUrl}/portafolio/argentina`,
{
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getPortfolioUSA (token) {
let res = await axios.get(`${apiUrl}/portafolio/estados_Unidos`,
{
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getOperations (token) {
let res = await axios.get(`${apiUrl}/operaciones`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getOperation (token, number) {
let res = await axios.get(`${apiUrl}/operaciones/${number}`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function cancelOperation (token, number) {
let res = await axios.delete(`${apiUrl}/operaciones/${number}`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function buy (token, market, asset, quantity, price, term, validTill) {
let res = await axios.post(`${apiUrl}/operar/Comprar`,
{
mercado: market,
simbolo: asset,
cantidad: quantity,
precio: price,
plazo: term,
validez: validTill
},
{
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function sell (token, market, asset, quantity, price, term, validTill) {
let res = await axios.post(`${apiUrl}/operar/operar/Vender`,
{
mercado: market,
simbolo: asset,
cantidad: quantity,
precio: price,
validez: validTill,
plazo: term
},
{
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getTicker (token, market, asset) {
let res = await axios.get(`${apiUrl}/${market}/Titulos/${asset}`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getOptions (token, market, asset) {
let res = await axios.get(`${apiUrl}/${market}/Titulos/${asset}/Opciones`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getTickerValue (token, market, asset) {
let res = await axios.get(`${apiUrl}/${market}/Titulos/${asset}/Cotizacion`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getPanels (token, asset, panel, country) {
let res = await axios.get(`${apiUrl}/Cotizaciones/${asset}/${panel}/${country}`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getTickerValuesBetweenDates (token, market, asset, dateFrom, dateTill, adjusted) {
let res = await axios.get(`${apiUrl}/${market}/Titulos/${asset}/Cotizacion/seriehistorica/${dateFrom}/${dateTill}/${adjusted}`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getInstrumentsByCountry (token, country) {
let res = await axios.get(`${apiUrl}/${country}/Titulos/Cotizacion/Instrumentos`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function getPanelsByInstrumentAndCountry (token, country, asset) {
let res = await axios.get(`${apiUrl}/${country}/Titulos/Cotizacion/Paneles/${asset}`, {
headers: { Authorization: `Bearer ${token.access_token}` }
})
return res.data;
}
async function readToken() {
let token = await fs.readFile('token.json')
return JSON.parse(token)
}
async function fileExists (file) {
try {
let esta = await fs.stat(file).catch(e => e ? false : true) ? true : false
return esta
} catch (e) {
if (e) return false
}
}
async function auth () {
let exists = await fileExists('token.json')
if (exists) {
let tokenData = await fs.readFile('token.json')
token = JSON.parse(tokenData)
if (new Date(token['.expires']) < new Date()) {
await fs.unlink('token.json')
await getToken(token.refresh_token)
return await readToken()
} else {
return(token);
}
} else {
await getToken()
return await readToken()
}
}
async function getToken (refreshToken) {
let creds = '';
if (refreshToken) {
creds = querystring.stringify({ refresh_token: refreshToken, grant_type: 'refresh_token' });
} else {
creds = querystring.stringify(credentials);
}
let token = await axios.post('https://api.invertironline.com/token', creds)
await fs.appendFile('token.json', JSON.stringify(token.data))
}
module.exports = {
getAccountStatus,
getPortfolioArgentina,
getPortfolioUSA,
getOperation,
getOperations,
cancelOperation,
buy,
sell,
getTicker,
getOptions,
getTickerValue,
getPanels,
getTickerValuesBetweenDates,
getInstrumentsByCountry,
getPanelsByInstrumentAndCountry,
auth
}