1

Im using binance to get data about Ethereum. I did the single kLine response with an GET request to the API so I get the old data but now I want to keep the kLines and the price updating automaticly.

For this I need to connect with the Binance web socket. How do I do this? Im using Javascript.

0

2 Answers 2

6

This code opens a web socket connected with Binance. It receives data about (the symbol) ETH/USDT each 2 seconds (depth). Every 30 minutes the data sets variable "x" to true so you know when to add a line.

If you want to change the 30 minutes, symbol, depth or kline data you should check out the Binance api documentation on how to do it correctly.

// Symbol: ETH/USDT - Kline 30 minutes.
var socket = new WebSocket('wss://stream.binance.com:9443/ws/ethusdt@kline_30m');
    
// When message received from web socket then...
socket.onmessage = function (event) {

    // Easier and shorter.
    var data = JSON.parse(event.data);

    // "x" means: Is this kline closed? Return "true" if closed. Closed means new line to be added.
    if (data.k.x === true) {
        log("Add line.");

        // Adding a line with my custom function.
        addLine(data);
    } else {
        // Updating line with my custom function.
        updatePrice(data);
    }
}
1
  • I have to note: I sometimes have the feeling data retrieved from the websocket is not equal to the data retrieved from the GET /api/v3/klines API request. I've been running my code for about a year now and results keep being different when generating trough websocket vs kline api call. Code to generate results is exactly the same for both data retrievers. Explenation could be: unstable/not fast enough internet, bug in code or data retrieved from websocket is not same as data from api call. My assumption goes to the last one. Have not fully checked for bugs in my code yet.
    – Allart
    Feb 8, 2022 at 10:15
0

I would like to add another solution which handles the connection and configuration part under the hood. Using Mida, a JavaScript framework for connecting to exchanges/brokers to get data and/or trade automatically.

https://github.com/Reiryoku-Technologies/Mida

First we need to login to Binance

import { login, } from "@reiryoku/mida";

const myAccount = await login("Binance/Spot", {
    apiKey: "***",
    apiSecrect: "***",
});

Then to watch ETHUSDT live prices and candles we just use the market watcher API

import { MidaMarketWatcher, MidaTimeframe, } from "@reiryoku/mida";

const marketWatcher = new MidaMarketWatcher({ tradingAccount: myAccount, });

await marketWatcher.watch("ETHUSDT", {
    watchPeriods: true,
    timeframes: [ MidaTimeframe.M30, ],
});

// Listen M30 candlesticks being closed
marketWatcher.on("period-close", (event) => {
   const candle = event.descriptor.period;

   console.log("M30 candle closed at " + candle.close);
});

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.