Skip to content

Instantly share code, notes, and snippets.

@SproutSeeds
Created July 24, 2019 05:45
Show Gist options
  • Save SproutSeeds/b7a218f220ad6d97550a0e2ffaf8821e to your computer and use it in GitHub Desktop.
Save SproutSeeds/b7a218f220ad6d97550a0e2ffaf8821e to your computer and use it in GitHub Desktop.
// Required Packages
const fetch = require('node-fetch');
const fs = require('fs');
// Defining my API Key
const key = 'key';
// Defining myData array that will be filled with objects with the design shown above.
let pairs = [
{ base: 'AUD', compared: ['CAD', 'CHF', 'JPY', 'NZD', 'USD'] },
{ base: 'BGN', compared: ['RON'] },
{ base: 'CAD', compared: ['CHF', 'JPY'] },
{ base: 'CHF', compared: ['BGN', 'JPY', 'RON'] },
{
base: 'EUR',
compared: [
'AUD',
'CAD',
'CHF',
'CZK',
'DKK',
'GBP',
'HKD',
'HUF',
'ILS',
'JPY',
'NOK',
'NZD',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'TRY',
'USD',
'ZAR'
]
},
{
base: 'GBP',
compared: [
'AUD',
'BGN',
'CAD',
'CHF',
'HKD',
'JPY',
'NZD',
'PLN',
'RON',
'SGD',
'USD',
'ZAR'
]
},
{ base: 'HKD', compared: ['JPY'] },
{ base: 'NZD', compared: ['CAD', 'CHF', 'JPY', 'USD'] },
{ base: 'SGD', compared: ['HKD', 'JPY'] },
{ base: 'TRY', compared: ['BGN', 'JPY', 'RON'] },
{
base: 'USD',
compared: [
'BGN',
'CAD',
'CHF',
'CZK',
'DKK',
'HKD',
'HUF',
'GBP',
'HKD',
'HUF',
'ILS',
'JPY',
'MXN',
'NOK',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'TRY',
'ZAR'
]
}
];
let baseCurrency;
let pairCurrency;
let url;
function cyclePairs(candleCount, granularity) {
pairs.forEach(element => {
baseCurrency = element.base;
element.compared.forEach(pair => {
pairCurrency = pair;
// console.log(`${baseCurrency}/${pairCurrency}`);
getData(baseCurrency, pairCurrency, candleCount, granularity);
});
});
}
async function getData(base, pair, candleCount, granularity) {
url = `https://api-fxpractice.oanda.com/v3/instruments/${base}_${pair}/candles?price=B&granularity=${granularity}&count=${candleCount}`;
let header = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key}`,
'Accept-Datetime-Format': 'RFC3339',
'Accept-Encoding': 'gzip, deflate',
Connection: 'Keep-Alive'
}
};
try {
let response = await fetch(url, header);
let data = response.json().then(data => {
writeData(data);
});
} catch (err) {
console.log(`${base}_${pair}`);
}
// console.log(data.candles.length);
}
function writeData(obj) {
let instrument = obj.instrument;
let granularity = obj.granularity;
let candles = obj.candles[0].bid;
let volume = obj.candles[0].volume;
let time = obj.candles[0].time;
let customObj = {
instrument: instrument,
time: time,
granularity: granularity,
volume: volume,
candles: candles
};
let fileName = instrument;
let pathToFile = `./instruments/${fileName}.json`;
try {
let rawData = fs.readFileSync(pathToFile);
let myInstrument = JSON.parse(rawData);
myInstrument.data.push(customObj);
let allRecords = JSON.stringify(myInstrument, null, 2);
fs.writeFileSync(pathToFile, allRecords);
} catch (err) {
console.log(customObj);
}
// console.log(customObj);
}
// MAIN FUNCTION
function main() {
cyclePairs('1', 'D');
// getData('USD', 'CAD', '1', 'D');
}
main();
// cyclePairs(10, 'M1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment