Skip to content

Commit

Permalink
Add investing query parameters and +37 currencies support (#25)
Browse files Browse the repository at this point in the history
* Add possibility to manipulate response

* Add some currency codes

* Update README.md

* Changes to be lint compatible

* Changed curriencies order on mapping.js to pairId ASC order

* Add test coverage to scenarios without some parameters

* Fixed test.js to lint compliance

* Apply suggestions from code review

* Update test/test.js

* Update test/test.js

* Update test/test.js

* Fix test/test.js

* Skip 1 test

Co-authored-by: Gustavo <gustavo.carneiro@g.globo>
Co-authored-by: Gustavo Carneiro <gustavo.mfc@me.com>
Co-authored-by: Davide Violante <DavideViolante@users.noreply.github.com>
  • Loading branch information
4 people committed Jun 20, 2021
1 parent 57f41f8 commit 5f8427e
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 15 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const { investing } = require('investing-com-api')

async function main () {
try {
const response = await investing('currencies/eur-usd')
const response1 = await investing('currencies/eur-usd')
const response2 = await investing('currencies/eur-usd', 3600, 24, '1-day') // With optional params
} catch (err) {
console.error(err)
}
Expand All @@ -22,17 +23,20 @@ async function main () {
### Response
```js
[
{ date: 1579651200000, value: 1.1093 },
{ date: 1579737600000, value: 1.1054 },
{ date: 1579824000000, value: 1.1025 },
{ date: 1580083200000, value: 1.1018 },
{ date: 1623812400000, value: 1.1093 },
{ date: 1623816000000, value: 1.1054 },
{ date: 1623819600000, value: 1.1025 },
{ date: 1623823200000, value: 1.1018 },
...
]
```


### Available inputs
- [mapping.js](https://github.com/DavideViolante/investing-com-api/blob/master/mapping.js)
- input (String) required, see [mapping.js](https://github.com/DavideViolante/investing-com-api/blob/master/mapping.js)
- interval (Number in seconds), data interval
- candleCount (Number) Max number of results
- period (String) time window: n-day, n-month or n-year where n is a number

### Run tests
- `npm test`
Expand All @@ -42,3 +46,6 @@ async function main () {

### Author
- [Davide Violante](https://github.com/DavideViolante/)

### Contributors
- [Gustavo Carneiro](https://github.com/gustavomfc/)
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ const axios = require('axios')
const { mapping } = require('./mapping')
const { mapResponse } = require('./functions')

function callInvesting (pairId) {
function callInvesting (pairId, interval, candleCount, period) {
return axios({
method: 'GET',
url: 'https://www.investing.com/common/modules/js_instrument_chart/api/data.php',
params: {
pair_id: pairId,
pair_interval: '86400', // 1 day
pair_interval: interval,
chart_type: 'area', // 'area', 'candlestick'
candle_count: '120', // days
volume_series: 'yes',
events: 'yes',
period: '1-year'
candle_count: candleCount,
volume_series: 'no',
events: 'no',
period: period
},
headers: {
'Referer': 'https://www.investing.com/',
Expand All @@ -22,7 +22,7 @@ function callInvesting (pairId) {
})
}

async function investing (input) {
async function investing (input, interval = 3600, candleCount = 24, period = '1-day') {
try {
if (!input) {
throw Error('Parameter input is required')
Expand All @@ -31,7 +31,7 @@ async function investing (input) {
if (!endpoint) {
throw Error(`No mapping found for ${input}, check mapping.js`)
}
const response = await callInvesting(endpoint.pairId)
const response = await callInvesting(endpoint.pairId, interval, candleCount, period)
if (!response.data.candles) {
throw Error('No response.data.candles found')
}
Expand Down
185 changes: 185 additions & 0 deletions mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,31 @@ exports.mapping = {
title: 'USD/JPY - US Dollar Japanese Yen',
name: 'USD/JPY'
},
'currencies/usd-chf': {
pairId: '4',
title: 'USD/CHF - US Dollar Swiss Franc',
name: 'USD/CHF'
},
'currencies/aud-usd': {
pairId: '5',
title: 'AUD/USD - Australian Dollar US Dollar',
name: 'AUD/USD'
},
'currencies/eur-gbp': {
pairId: '6',
title: 'EUR/GBP - Euro British Pound',
name: 'EUR/GBP'
},
'currencies/usd-cad': {
pairId: '7',
title: 'USD/CAD - US Dollar Canadian Dollar',
name: 'USD/CAD'
},
'currencies/nzd-usd': {
pairId: '8',
title: 'NZD/USD - New Zealand Dollar US Dollar',
name: 'USD/CAD'
},
'currencies/eur-jpy': {
pairId: '9',
title: 'EUR/JPY - Euro Japanese Yen',
Expand All @@ -34,11 +49,181 @@ exports.mapping = {
title: 'EUR/CHF - Euro Swiss Franc',
name: 'EUR/CHF'
},
'currencies/gbp-jpy': {
pairId: '11',
title: 'GBP/JPY - British Pound Japanese Yen',
name: 'GBP/JPY'
},
'currencies/gbp-chf': {
pairId: '12',
title: 'GBP/CHF - British Pound Swiss Franc',
name: 'GBP/CHF'
},
'currencies/chf-jpy': {
pairId: '13',
title: 'CHF/JPY - Swiss Franc Japanese Yen',
name: 'CHF/JPY'
},
'currencies/cad-chf': {
pairId: '14',
title: 'CAD/CHF - Canadian Dollar Swiss Franc',
name: 'CAD/CHF'
},
'currencies/eur-aud': {
pairId: '15',
title: 'EUR/AUD - Euro Australian Dollar',
name: 'EUR/AUD'
},
'currencies/eur-cad': {
pairId: '16',
title: 'EUR/CAD - Euro Canadian Dollar',
name: 'EUR/CAD'
},
'currencies/usd-zar': {
pairId: '17',
title: 'USD/ZAR - US Dollar South African Rand',
name: 'USD/ZAR'
},
'currencies/usd-try': {
pairId: '18',
title: 'USD/TRY - US Dollar Turkish Lira',
name: 'USD/TRY'
},
'currencies/ars-brl': {
pairId: '1473',
title: 'ARS/BRL - Argentinian Peso Brazil Real',
name: 'ARS/BRL'
},
'currencies/aud-brl': {
pairId: '1485',
title: 'AUD/BRL - Australian Dollar Brazil Real',
name: 'AUD/BRL'
},
'currencies/brl-ars': {
pairId: '1505',
title: 'BRL/ARS - Brazil Real Argentinian Peso',
name: 'BRL/ARS'
},
'currencies/brl-aud': {
pairId: '1506',
title: 'BRL/AUD - Brazil Real Australian Dollar',
name: 'BRL/AUD'
},
'currencies/brl-cad': {
pairId: '1507',
title: 'BRL/CAD - Brazil Real Canadian Dollar',
name: 'BRL/CAD'
},
'currencies/brl-chf': {
pairId: '1508',
title: 'BRL/CHF - Brazil Real Swiss Franc',
name: 'BRL/CHF'
},
'currencies/brl-clp': {
pairId: '1509',
title: 'BRL/CLP - Brazil Real Chilean Peso',
name: 'BRL/CLP'
},
'currencies/brl-dkk': {
pairId: '1510',
title: 'BRL/DKK - Brazil Real Danish Krone',
name: 'BRL/DKK'
},
'currencies/brl-hkd': {
pairId: '1512',
title: 'BRL/HKD - Brazil Real Hong Kong Dollar',
name: 'BRL/HKD'
},
'currencies/brl-jpy': {
pairId: '1513',
title: 'BRL/JPY - Brazil Real Japanese Yen',
name: 'BRL/JPY'
},
'currencies/brl-mxn': {
pairId: '1514',
title: 'BRL/MXN - Brazil Real Mexican Peso',
name: 'BRL/MXN'
},
'currencies/brl-sgd': {
pairId: '1515',
title: 'BRL/SGD - Brazil Real Singapore Dollar',
name: 'BRL/SGD'
},
'currencies/brl-usd': {
pairId: '1516',
title: 'BRL/USD - Brazil Real US Dollar',
name: 'BRL/USD'
},
'currencies/cad-brl': {
pairId: '1523',
title: 'CAD/BRL - Canadian Dollar Brazil Real',
name: 'CAD/BRL'
},
'currencies/chf-brl': {
pairId: '1544',
title: 'CHF/BRL - Swiss Franc Brazil Real',
name: 'CHF/BRL'
},
'currencies/eur-brl': {
pairId: '1617',
title: 'EUR/BRL - Euro Brazil Real',
name: 'EUR/BRL'
},
'currencies/gbp-brl': {
pairId: '1736',
title: 'GBP/BRL - British Pound Brazil Real',
name: 'GBP/BRL'
},
'currencies/jpy-brl': {
pairId: '1890',
title: 'JPY/BRL - Japanese Yen Brazil Real',
name: 'JPY/BRL'
},
'currencies/sgd-brl': {
pairId: '2027',
title: 'SGD/BRL - Singapore Dollar Brazil Real',
name: 'SGD/BRL'
},
'currencies/usd-brl': {
pairId: '2103',
title: 'USD/BRL - US Dollar Brazil Real',
name: 'USD/BRL'
},
'currencies/us-dollar-index': {
pairId: '8827',
title: 'US Dollar Index Futures',
name: 'Dollar index'
},
'currencies/aed-brl': {
pairId: '9277',
title: 'AED/BRL - UAE Dirham Brazil Real',
name: 'AED/BRL'
},
'currencies/cny-brl': {
pairId: '9493',
title: 'CNY/BRL - Chinese Yuan Brazil Real',
name: 'CNY/BRL'
},
'currencies/dkk-brl': {
pairId: '9575',
title: 'DKK/BRL - Danish Krone Brazil Real',
name: 'DKK/BRL'
},
'currencies/hkd-brl': {
pairId: '9617',
title: 'HKD/BRL - Hong Kong Dollar Brazil Real',
name: 'HKD/BRL'
},
'currencies/nok-brl': {
pairId: '9841',
title: 'NOK/BRL - Norwegian Krone Brazil Real',
name: 'NOK/BRL'
},
'currencies/sek-brl': {
pairId: '10130',
title: 'SEK/BRL - Swedish Krona Brazil Real',
name: 'SEK/BRL'
},
'commodities/gold': {
pairId: '8830',
title: 'Gold Futures',
Expand Down
24 changes: 23 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,34 @@ describe('Tests for Investing.com unofficial APIs', () => {
assert.strictEqual(mockData[1][1], mappedResponse[1].value)
})

it('should return data from investing.com', async () => {
it('should return data from investing.com with just input parameter', async () => {
const response = await investing('currencies/eur-usd')
assert.ok(response)
assert.ok(response.length)
})

it('should return data from investing.com with an 5min (1800s) interval between them', async () => {
const response = await investing('currencies/eur-usd', 1800)
const diff = response[1].date - response[0].date
assert.ok(diff === (1800 * 1000))
})

xit('should return data from investing.com with an 5min (1800s) interval between them and a maximum of 12 results', async () => {
const response = await investing('currencies/eur-usd', 1800, 12)
const diff = response[1].date - response[0].date
assert.ok(diff === (1800 * 1000))
assert.ok(response.length === 12)
})

it('should return data from investing.com with an 5min (1800s) interval between them and a maximum of 24 results in a 2-hour time window', async () => {
const response = await investing('currencies/eur-usd', 1800, 24, '2-hour')
const diff1 = response[1].date - response[0].date
const diff2 = response[1].date - response[response.length - 1].date
assert.ok(diff1 === (1800 * 1000))
assert.ok(diff2 <= (7200 * 1000))
assert.ok(response.length === 24)
})

it('should return undefined if no input is given', async () => {
const response = await investing()
assert.strictEqual(response, undefined)
Expand Down

0 comments on commit 5f8427e

Please sign in to comment.