通过API计算比特币价格的实际RSI(相对强弱指数):
我的脚本目前向API发出14个单独请求,以计算比特币的实际RSI。现在我找到了一个请求,我只需要向API发出一个请求。
https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=14

将新API集成到现有脚本中需要做什么?我只想向API提出一个请求。
// Create a url for currencies + date
const getURL = (currencyFrom, currencyTo, timestamp) =>
`https://min-api.cryptocompare.com/data/pricehistorical?fsym=${currencyFrom}&tsyms=${currencyTo}&ts=${timestamp}`;
// Get a timestamp from exactly x days ago
const timeStampForDaysAgo =
nrOfDays => Date.now() - 1000 * 60 * 60 * 24 * nrOfDays;
const getBTCDollarValueForDays = nrOfDays => Promise
.all(Array.from(
Array(nrOfDays),
(_, i) =>
fetch(getURL("BTC", "USD", timeStampForDaysAgo(i)))
.then(r => r.json()))
);
const getDollarValueFromResponse = obj => obj.BTC.USD;
// Main:
getBTCDollarValueForDays(14)
.then(vals => vals.map(getDollarValueFromResponse))
.then(calcRSI)
.then(console.log.bind(console, "RSI is:"));
function calcRSI(xs) {
let sumGain = 0;
let sumLoss = 0;
const TOLERANCE = 50;
for (let i = 1;
i < xs.length;
i += 1) {
const curr = xs[i];
const prev = xs[i - 1]
const diff = curr - prev;
if (diff >= 0) {
sumGain += diff;
} else {
sumLoss -= diff;
}
}
if (sumGain === 0) return 0;
if (Math.abs(sumLoss) < TOLERANCE) return 100;
const relStrength = sumGain / sumLoss;
return 100 - (100 / (1 + relStrength));
};
&#13;