我正在尝试从Binance节点API计算RSI, 我已经达到某种程度的RSI,但与Binance上显示的“正确” RSI距离还很远。
RSI的计算公式如下
RSI = 100 – 100/ (1 + RS)
RS = Average Gain of n days UP / Average Loss of n days DOWN
还有我用于从Binance计算RSI的代码
const binance = require('node-binance-api')().options({
APIKEY: 'xxx',
APISECRET: 'xxx',
useServerTime: true,
test: true // True = SandboxMode
});
/* VARIABLES */
let listLow = [];
let changeUp = 0;
let changeDown = 0;
let last_close = 0;
let current_time = Date.now();
calculateRSI();
function calculateRSI() {
console.log("Generating RSI");
binance.candlesticks("ETHBTC", "1d", (error, ticks, symbol) => {
for (i = 0; i < ticks.length; i++) {
let last_tick = ticks[i];
let [time, open, high, low, close, volume, closeTime, assetVolume, trades, buyBaseVolume, buyAssetVolume, ignored] = last_tick;
listLow.push(close);
if (i == ticks.length -1 ) {
for (x = 0; x < listLow.length; x++) {
previous_close = (parseFloat(listLow[x - 1]));
current_close = (parseFloat(listLow[x]));
// HIGH
if (current_close > previous_close) {
upChange = current_close - previous_close;
changeUp += upChange;
if (x == listLow.length -1) {
last_close = current_close - previous_close;
}
}
// LOW
if (previous_close > current_close) {
downChange = previous_close - current_close;
changeDown += downChange;
if (x == listLow.length - 1) {
last_close = previous_close - current_close;
}
}
if (x == (listLow.length - 1)) {
AVGHigh = changeUp / (ticks.length);
AVGLow = changeDown / (ticks.length);
Upavg = (AVGHigh * (ticks.length -1) + last_close) / (ticks.length);
Downavg = (AVGLow * (ticks.length -1) + last_close) / (ticks.length);
RS = Upavg / Downavg;
console.log(RS + " RS");
RSI = (100 - (100 / (1 + RS)));
console.log(RSI);
}
}
}
}
}, {
limit: 14,
endTime: current_time
});
}
运行代码时,我得到RSI值:30.973505422917697
但是Binance说这是正确的RSI:39.615540。
我做错了什么,或者我想念什么?
感谢您抽出宝贵的时间阅读, //乔纳斯(Jonas)