Here is an article on simplifying Ethereum live data by loading only 1-hour Klines:
Simplifying Ethereum Stream Data: A Step-by-Step Guide
When it comes to real-time market data, simplicity is key. In this article, we will show you how to optimize your Ethereum live data by loading only the 1-hour Klines you need.
Why 1h Klines Are Enough
Due to the large number of available switches and markets, too much data can be overwhelming. However, 1h Klines offer a good balance between coverage and storage needs. By focusing only on the top N 1h lines per ticker, you can get a more accurate picture of market activity without overstocking.
WebSockets for Data Streaming
Use WebSockets with your Ethereum provider (e.g. Binance) to simplify retrieving data from the stream. WebSockets allow for two-way communication between your application and the provider’s server. This allows you to send data updates from your client (in this case, a web application) to the server.
Detailed Guide
- Set up a WebSocket connection: Create a WebSocket connection with your Ethereum provider as per their API documentation.
- Set up Kline upload parameters: Enter the indicators and time intervals you want to receive Klines for (e.g. 1h).
- Use built-in WebSockets streaming features: When new data is available, use the “onmessage” event to deliver the latest Kline data to your client.
- Filter out unnecessary data: Filter out the median values with a simple algorithm (like the one below) and only keep the top N wedges per ticker for 1 hour.
Here is an example code in JavaScript with WebSockets with Binance API:
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
// Specify the indicators and intervals for which you want to get Klines
const tickers = ['ETH', 'BTC'];
const interval = { h: 1 };
// The function filters out unnecessary data
function getTopNKlines(ws, tickers) {
const klines = {};
// Get the top N wedges of the ticker
ws.on('message', (data) => {
const { ticker, kline } = JSON.parse(data);
if (!klines[ticker]) {
ticker[ticker] = [];
}
wedges[ticker].push(wedge);
});
// Filter out the median values
Object.values(klines).forEach((klinesArray) => {
const middleIndex = Math.floor(klinesArray.length / 2);
if (middleIndex > 0 && middleIndex < klinesArray.length - 1) {
klinesArray.splice(middleIndex, 1);
}
});
return Object.fromEntries(Object.entries(wedges).map(([ticker, field]) => [ticker, field]));
}
ws.on('error', (error) => console.error(error));
ws.on('close', () => console.log('WebSocket connection closed'));
wss.listen(8080, () => {
console.log('WebSocket server listening on port 8080');
});
Conclusion
By following these steps and using WebSockets with the Binance API, you can simplify the process of obtaining Ethereum live data without compromising on coverage or storage requirements. This approach helps you stay organized and focus on the market operations that are most important to your application. Happy coding!