Skip to content

Commit

Permalink
getHistoricalData uses mapping.js (DavideViolante#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
liardoecp committed Oct 23, 2024
1 parent 5c9df89 commit 95002cd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# TODO
- Aggiornare README.md
- getHistoricalData input deve usare mapping.js
- valore di default di resolution 'D'
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const puppeteer = require('puppeteer');
const { mapping } = require('./mapping');
const { getPairId } = require('./mapping');
const { getJsonContent, mapResponse } = require('./functions');
const getHistoricalData = require('./src/getHistoricalData');

Expand Down Expand Up @@ -67,7 +67,7 @@ async function callInvesting(pairId, period, interval, pointscount, pptrLaunchOp
async function investing(input, period = 'P1M', interval = 'P1D', pointscount = 120, pptrLaunchOptions) {
try {
checkParams(input, period, interval, pointscount);
const pairId = mapping[input]?.pairId || input;
const pairId = getPairId(input);
const resInvesting = await callInvesting(pairId, period, interval, pointscount, pptrLaunchOptions);
const results = mapResponse(resInvesting);
if (!results.length) {
Expand Down
4 changes: 3 additions & 1 deletion mapping.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.mapping = {
const mapping = {
'currencies/eur-usd': {
pairId: '1',
title: 'EUR/USD - Euro US Dollar',
Expand Down Expand Up @@ -945,3 +945,5 @@ exports.mapping = {
name: 'PIMCO Commodity Real Return Strategy Institutional',
},
};

exports.getPairId = (input) => mapping[input]?.pairId || input;
3 changes: 2 additions & 1 deletion src/getHistoricalData.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { mapResponse } = require('../functions');
const { getPairId } = require('../mapping');

const buildUrl = ({ input, resolution, from, to } = {}) => {
const query = new URLSearchParams({
symbol: input,
symbol: getPairId(input),
resolution,
from: from && from.getTime() / 1000,
to: to && to.getTime() / 1000,
Expand Down
58 changes: 58 additions & 0 deletions test/getHistoricalData.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,62 @@ describe('Tests for getHistoricalData()', () => {
];
expect(data).toEqual(expected);
});

it('should use mapping for input', async () => {
scope
.get('/d8f62270e64f9eb6e4e6a07c3ffeab0b/1729428526/9/9/16/history')
.query({
symbol: '1',
resolution: 'D',
from: 1729123200,
to: 1729209600,
})
.reply(200, {
't': [
1729123200,
],
'c': [
1.08309996128082008937099089962430298328399658203125,
],
'o': [
1.086099982261659935289799250313080847263336181640625,
],
'h': [
1.0872999429702800977537435755948536098003387451171875,
],
'l': [
1.0809999704360999661645337255322374403476715087890625,
],
'v': [
1,
],
'vo': [
'n/a',
],
'vac': [
'n/a',
],
's': 'ok',
});

const data = await getHistoricalData({
input: 'currencies/eur-usd',
resolution: 'D',
from: new Date(1729123200000),
to: new Date(1729209600000),
});

const expected = [
{
date: 1729123200000,
value: 1.08309996128082008937099089962430298328399658203125,
price_open: 1.086099982261659935289799250313080847263336181640625,
price_high: 1.0872999429702800977537435755948536098003387451171875,
price_low: 1.0809999704360999661645337255322374403476715087890625,
price_close: 1.08309996128082008937099089962430298328399658203125,
volume: 1,
},
];
expect(data).toEqual(expected);
});
});

0 comments on commit 95002cd

Please sign in to comment.