Discover the latest prices of your favorite coins using WebSockets on Binance platform

There are various functions available in Binance's Web Socket Streams including Aggregate Streams, Trade Streams, and Kline/Candlestick Streams. You can find more information about these functions here.

I am trying to retrieve the current price and last 24-hour percentage change of my selected coins in real time. However, I'm having trouble figuring out how to access this information. Ideally, the prices should be updated in real time, while the 24-hour percentage change can be refreshed every minute.

Currently, I am using CoinCap as a reference, which provides an easy way to:

  1. Retrieve the 24-hour percentage change by calling the endpoint
    https://api.coincap.io/v2/assets?ids=bitcoin,ethereum
  2. Obtain real-time prices by calling the endpoint
    wss://ws.coincap.io/prices?assets=bitcoin,ethereum

The issue with CoinCap is that it does not allow me to filter prices based on the exchange I prefer, which is Binance. As a result, I receive prices unrelated to Binance.

var socket = new WebSocket('wss://ws.coincap.io/prices?assets=bitcoin,ethereum');
socket.addEventListener('message', function (event)
{
// parse & show the data
});

For example, the Kline/Candlestick Streams description states:

The Kline/Candlestick Stream provides updates to the current klines/candlestick every second

It also presents the following data format:

{
"e": "kline", // Event type
"E": 123456789, // Event time
"s": "BNBBTC", // Symbol
"k": {
"t": 123400000, // Kline start time
"T": 123460000, // Kline close time
"s": "BNBBTC", // Symbol
"i": "1m", // Interval
"f": 100, // First trade ID
"L": 200, // Last trade ID
"o": "0.0010", // Open price
"c": "0.0020", // Close price
"h": "0.0025", // High price
"l": "0.0015", // Low price
"v": "1000", // Base asset volume
"n": 100, // Number of trades
"x": false, // Is this kline closed?
"q": "1.0000", // Quote asset volume
"V": "500", // Taker buy base asset volume
"Q": "0.500", // Taker buy quote asset volume
"B": "123456" // Ignore
}
}

Based on this data, what is the accurate current price matching the value displayed on the Binance Platform here?

Answer №1

If you're looking for real-time price updates, consider using miniTicker from Binance API. Check out the relevant documentation here. The current last price can be found in the c attribute, while the price 24 hours ago is stored in the o attribute.

Answer №2

Remember to utilize <symbol>@ticker with the added 24-hour percentage.

{
  "e": "24hrTicker",  // Event type
  "E": 1672515782136, // Event time
  "s": "BNBBTC",      // Symbol
  "p": "0.0015",      // Price change
  "P": "250.00",      // Price change percent <--- Here
  "w": "0.0018",      // Weighted average price
  "x": "0.0009",      // First trade(F)-1 price (first trade before the 24hr rolling window)
  "c": "0.0025",      // Last price
  "Q": "10",          // Last quantity
  "b": "0.0024",      // Best bid price
  "B": "10",          // Best bid quantity
  "a": "0.0026",      // Best ask price
  "A": "100",         // Best ask quantity
  "o": "0.0010",      // Open price
  "h": "0.0025",      // High price
  "l": "0.0010",      // Low price
  "v": "10000",       // Total traded base asset volume
  "q": "18",          // Total traded quote asset volume
  "O": 0,             // Statistics open time
  "C": 86400000,      // Statistics close time
  "F": 0,             // First trade ID
  "L": 18150,         // Last trade Id
  "n": 18151          // Total number of trades
}

Source: websocket stream

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Enhancing Your Tables: A Guide to Implementing Filters in jQuery.fancyTable or Similar Tools

Currently, I am utilizing the fancyTable plugin to showcase a table of Django results. I have successfully integrated a search bar and sortable columns (ascending/descending). Filters are also incorporated, but at present, they only update the search bar w ...

How come the deleteOne and findById methods in mongoose are able to function properly even after the document has

During my development of an API using nodejs to interact with a MongoDB database, I encountered a peculiar issue after deleting a document. My API consists of various endpoints for retrieving all animals in the database, fetching a specific animal using i ...

How can Express JS be configured to make URL calls from a local environment?

I encountered an issue with my code (Weather App using OpenWeatherMap Api) when I attempted to move my apiKey and apiUrl to the .env file. An error appeared in the terminal, but it's unclear why it occurred. Below is my code: const express = require( ...

Using the React useEffect hook to manage state in React applications

When a button is clicked in my app, the state of endpoint changes and data is fetched from my API. While waiting for the data to be retrieved, I wanted to display a loading icon which was quite challenging to implement but after much testing, I finally got ...

Maintain the structure of ajax POST requests and display a real-time feed of

I've been developing an openvz script to run virtual machines at home. I've been exploring JavaScript and AJAX functions to send post requests, which are then displayed in the innerHTML section of my webpage. However, I've encountered an iss ...

Next.js customization script

How can I efficiently create a class toggle function for a slide-in menu in Next.js? I've developed a menu with a sliding functionality that toggles when a button with the class "toggled" is clicked. However, I'm unsure if this approach aligns w ...

Leverage Arrays with Bootstrap SelectPicker in Javascript

I am looking to populate a Bootstrap SelectPicker with values from an array. I am unsure of how to loop through the array to extract the values. Currently, I have manually added values to the SelectPicker like this. <!DOCTYPE html> <html> < ...

Add the file to the current directory

As a newer Angular developer, I am embarking on the task of creating a web page that enables users to upload files, with the intention of storing them in a specific folder within the working directory. The current location of the upload page component is ...

Unable to locate the module model in sequelize

Having some trouble setting up a basic connection between Postgres and SQL using Sequelize. I keep getting an error where I can't require the model folder, even though in this tutorial he manages to require the model folder and add it to the sync, lik ...

Creating nested tables by assigning unique Div IDs to each table element

Can we utilize a straightforward JavaScript/jQuery function to nest elements inside table elements? For instance, every square comes with its own unique ID (A1, B7, C5). How can I use this ID to insert the checker image in any selected tile upon clicking? ...

req.next does not exist as a function [END]

I am currently working on developing a website and I have encountered an issue in the dashboard stage. The error message TypeError: req.next is not a function keeps appearing, particularly when trying to create a subpage for the dashboard. I am utilizing t ...

The sequence of text is not appearing correctly

I'm attempting to populate a paragraph with JSON data, while inserting a divider between data gathered on different dates. function fetchAllLogs(employeeId) { $.getJSON('datafile.json', function(data) { $("#" + employeeId).html( ...

Can you explain the exact purpose of npm install --legacy-peer-deps? When would it be advisable to use this command, and what are some potential scenarios where it

Encountered a new error today: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1cfc4d9d5d5d6c8cfe1918f908f91">[em ...

Adding a sub collection to a Firestore document - step by step instructions

Could someone please provide documentation on adding sub collections in Firestore within a web app? I attempted to do so, but encountered difficulties. How can I successfully add a sub collection using code in my apps? Or should I consider using Firebase ...

React Error: Invalid Element Type with Named Exports

I've been diving into the world of React hooks and functions, working on three different files. First, there's one that establishes a context called SummaryContext. The second file contains a class component that consumes this context named WikiS ...

react router 2 will display a 404 error when the requested page is not

Currently, I am utilizing react router version 2.8.1 and attempting to configure my custom 404 page for my single-page application (SPA). However, I am encountering issues in actually returning a proper 404 status code. My main goal is to prevent Google fr ...

When I switch to a different navigation system, the css class gets removed

Let me clarify what I am looking for: Upon entering the index.php page, LINK_1 button is active. When I switch to LINK_2, it becomes active. I have only one index.php page where I include parts of external pages using PHP. Page_1 https://i.sstatic.net/Yv ...

Issue with JSON object key saving due to array length and element inconsistency

I am dealing with an API that returns a JSON Array of objects in the following format: [ { person_id:"12212", person_name:"some name", deptt : "dept-1" }, { person_id: "12211", person_name: "some name 2" deptt : "de ...

Out of the total 1341 test cases, Yarn is currently only running 361

Currently, I am immersed in a project containing over 1300 test cases. Despite my efforts to clone the entire project again, it appears there is an issue with my npm. Upon executing yarn test, only 361 out of 1341 tests are running. I am puzzled as to how ...

jQuery request avoids encountering any 'Access-Control-Allow-Origin error

I am attempting to retrieve content from one website and transfer it to another website. Unfortunately, I keep encountering the error mentioned in the title during my jQuery request. Below is the code I am using: $.ajax({ url: 'destinationURL' ...