Bitcoin (BTC) is a popular cryptocurrency that is traded on many different exchanges around the world. As a trader or investor, it can be useful to monitor the BTC prices on multiple exchanges in order to get the best deal and maximize your profits. In this article, we’ll show you how to use JavaScript to fetch BTC prices from various exchanges and display them in a tabular format, so that you can easily compare the prices and make informed decisions.
Requirements
To follow along with this tutorial, you’ll need the following:
- A basic understanding of JavaScript programming
- A code editor such as Visual Studio Code or Atom
- A web browser such as Chrome or Firefox
- An internet connection
Step 1: Choose the Exchanges to Monitor
The first step is to decide which exchanges you want to monitor. In this example, we’ll monitor the BTC prices on the following exchanges:
- Binance
- Coinbase Pro
- Kraken
- Bitfinex
- Huobi
- Bitstamp
- Gemini
- Bittrex
- Poloniex
- Kucoin
You can choose different exchanges if you prefer, or add/remove exchanges as needed.
Step 2: Fetch the BTC Prices Using JavaScript
Next, we’ll write some JavaScript code to fetch the BTC prices from each exchange using the Coinbase Pro API. We’ll use the fetch()
method to make a GET request to the API endpoint for the BTC-USD trading pair, and parse the JSON response to extract the current BTC price.
Here’s the code:
const exchanges = [ 'binance', 'coinbase-pro', 'kraken', 'bitfinex', 'huobi', 'bitstamp', 'gemini', 'bittrex', 'poloniex', 'kucoin' ]; let previousPrices = {}; let btcPrices = {}; let tableData = []; function updatePrices() { exchanges.forEach((exchange) => { fetch(`https://api.pro.coinbase.com/products/BTC-USD/ticker`) .then((response) => response.json()) .then((data) => { btcPrices[exchange] = data.price; previousPrices[exchange] = btcPrices[exchange]; }) .catch((error) => { console.error(error); }); }); // Update table data tableData.push(btcPrices); console.clear(); console.table(tableData); } // Update prices every 10 seconds setInterval(updatePrices, 10000); // Initial update updatePrices();
In this code, we define an array exchanges that contains the names of the exchanges we want to monitor. We also define three objects: previousPrices to store the previous BTC prices for each exchange, btcPrices to store the current BTC prices for each exchange, and tableData to store the BTC prices for each exchange in each iteration of the script.
We then define a function updatePrices() that uses the forEach() method to loop through each exchange and fetch the BTC price from the Coinbase Pro API. When we receive the BTC price data, we store it in the btcPrices object using the exchange name as the key, and update the previousPrices object with the current BTC prices at the end of each iteration.
Finally, we update the tableData array with the current BTC prices for each exchange, clear the console using console.clear(), and display the tableData array using `
Leave a Reply
You must be logged in to post a comment.