Open In App

Get Top 100 Cryptocurrencies Ranking Using Python

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to get the top 100 coins with Python. This article will provide an overview of interacting with the Live Coin Watch cryptocurrency data API using the Python programming language. By following this tutorial, users will be able to print a ranked list of the top 100 coins on the terminal by sending an HTTP POST request to the coins/list endpoint and print the price and 24-hour trading volume of Bitcoin by sending an HTTP POST request to the coins/single endpoint.

Problem

Cryptocurrency market data is decentralized and inherently presents challenges for traders to gain a complete understanding of market conditions. Using aggregated market data, users can more accurately determine cryptocurrency asset performance and market ranking.

For example, if you were to evaluate market data from the Binance API, the prices, volume, ranking, etc would be specific to that exchange. Another factor to consider is that some assets are traded on both centralized and decentralized exchanges.

Get Top 100 Using Python:

Create a free account on Live Coin Watch and copy your API key

 

 

Python3




import requests
import json
import time
  
  
payload = json.dumps({
  "currency": "USD",
  "sort": "rank",
  "order": "ascending",
  "offset": 0,
  "limit": 100,
  "meta": True
})
headers = {
  'content-type': 'application/json',
  'x-api-key': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
}
  
response = requests.request("POST", url, 
                            headers=headers, 
                            data=payload)
response_json = response.json()
  
i = 0
print("Top 100 Coins:\n")
while i <100 :
  this = response_json[i]
  rank = this['rank']
  name = this['name']
  code = this['code']
  volume = this['volume']
  rate = this['rate']
  link = "https://livecoinwatch.com/price/" + name.replace(" ", "") + "-" + code
  ts = time.gmtime()
  print(str(rank) + '. ' + name + " ${:,.2f}".format(rate) + " Volume: ${:,.2f}".format(volume) + " " + time.asctime(ts) + "\n" + link + "\n")
  i += 1


Output:

 

Code Breakdown:

Importing Modules:

Import the requests, json, and time modules. The requests module is an HTTP library for Python used to send the HTTP POST request. The json module is used to handle the conversion of JSON data sent to and returned from the API. The time module is used for creating the timestamp.

import requests
import json
import time

Provide API URL:

Provide the API URL endpoint to make the request. In this case, we are using the coins/list endpoint to retrieve a ranked list of the top 100 coins.

url = "https://api.livecoinwatch.com/coins/list"

Request body and header:

Provide the following for your request body and header:

  • currency – your country’s currency code
  • sort – the sorting parameter- rank, price, volume, code, name, age
  • order – ascending or descending
  • offset – offset of the list, default is 0
  • limit – limit of the list, default 10, maximum 100
  • meta – set true to return full coin information
  • content-type application/json (The MIME media type for JSON text
  • x-api-key – your API key from https://www.livecoinwatch.com/tools/api
     
 payload = json.dumps({
 "currency": "USD",
 "sort": "rank",
 "order": "ascending",
 "offset": 0,
 "limit": 100,
 "meta": True
})
headers = {
 'content-type': 'application/json',
 'x-api-key': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
}

Send HTTP POST request:

Send the HTTP POST request using the requests.request() method.

response = requests.request("POST", url, headers=headers, data=payload)

Store API response:

Store the API response to the variable response_json as a JSON object using the response.json() method.

response_json = response.json()

Print the details of the coins:

Use a while loop to iterate through the return object (JSON array of length 100) and print the results to the console. Declare a variable i for the while loop counter. If you were requesting the top 50 coins for example, instead of the top 100, simply change the while statement condition to i <50.

Inside the while loop, declare a variable ‘this’ to store the current JSON object in the array (whatever i is pointing at for each iteration, the first iteration i = 0 will be the first coin on the list).

  • Declare a variable rank to store the coin’s rank from the return JSON object key “rank”.
  • Declare a variable name to store the coin’s name from the return JSON object key “name”.
  • Declare a variable code to store the coin’s code from the return JSON object key “code”.
  • Declare a variable volume to store the coin’s volume from the return JSON object key “volume”.
  • Declare a variable rate to store the coin’s rate from the return JSON object key “rate”.
  • Declare a variable link to create a hyperlink for the coin’s listing on Live Coin Watch.

The link is formed by concatenating the base URL “https://livecoinwatch.com/price/” + the name we stored from the return JSON (white space removed using the Python String replace() method), followed by a hyphen and the coin’s code.

Declare a variable ts to store each timestamp, using the time.gmtime() method to return a time.struct_time object in UTC.

At the end of each loop iteration, print the rank, name, rate, volume, timestamp, and hyperlink for each coin on the list, and return numerical quantities with commas and two decimal places using {:,.2f}

Increase the counter i by 1 until the condition of the while statement is reached and our full list has been printed.

i = 0
print("Top 100 Coins:\n")
while i <100 :
 this = response_json[i]
 rank = this['rank']
 name = this['name']
 volume = this['volume']
 rate = this['rate']
 link = "https://livecoinwatch.com/price/" + name.replace(" ", "") + "-" + code
 ts = time.gmtime()
 print(str(rank) + '. ' + name + " ${:,.2f}".format(rate) + " Volume: ${:,.2f}".format(volume) + " " + time.asctime(ts) + "\n" + link + "\n")
 i += 1

Get the Price and Volume of Bitcoin:

You can track the price of a single cryptocurrency by sending an HTTP POST request to the coins/single endpoint. Replace the variable code with the code of the cryptocurrency you would like to search for.
 

Python3




import requests
import json
import time
  
  
code = "BTC"
  
payload = json.dumps({
  "currency": "USD",
  "code": "BTC",
  "meta": "true"
})
headers = {
  'content-type': 'application/json',
  'x-api-key': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
}
  
response = requests.request("POST", url, 
                            headers=headers,
                            data=payload)
response_json = response.json()
  
rank = response_json['rank']
name = response_json['name']
volume = response_json['volume']
rate = response_json['rate']
link = "https://livecoinwatch.com/price/" + name.replace(" ", "") + "-" + code
ts = time.gmtime()
  
print(name + " ${:,.2f}".format(rate) + " Volume: ${:,.2f}".format(volume) + " " + time.asctime(ts) + "\n" + link + "\n")


Output:

 



Last Updated : 13 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads