I try to calculate the sma for the BTC/USDT pair with nodejs. Therefor I installed the talib module and created a socket to the binance markter ticker.
const WebSocketBinance_BTCUSDT = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@ticker');
let BTCUSDT = new Coin('Bitcoin in US-Dollar Tether');
WebSocketBinance_BTCUSDT.on('message', function incoming(data) {
CoinMarket.BTCUSDT.update(JSON.parse(data));
});
So far everything works as expected. The Coin-Class looks like:
class Coin {
constructor(bezeichnung) {
this.name = bezeichnung;
this.price = new Array();
}
get current() {
return Number(this.price[this.price.length -1] ).toFixed(4)
}
update(dataset) {
this.price.push(Number(dataset.a).toFixed(4));
}
ma(width){
if (this.price.length < width) return;
let val = 0;
for (let i = 1; i <= width; i++) {
val += Number(this.price[this.price.length - i]) ;
}
return Number(val/(width)).toFixed(4);
}
}
module.exports = {
Coin
}
My problem is, that if I call ma(20) the value differs from that shown on Binance or Tradingview. Also TALib calculates a wrong Baseband for Bollinger Bands. I think this means that I use a wrong dataset to calculate a correct intraday moving average. But what values should I use ?
Best regards