In this chapter, we will get a number of top 100 coins by their market cap with python. If you have read the previous chapter of this cryptocurrency project then you will find out that in this chapter we have only changed the GET’s url content a little bit to achieve the above-mentioned outcome. Below is the url we need to use to retrieve the above-mentioned outcome.

 url = "https://min-api.cryptocompare.com/data/top/mktcapfull"
 data = {'tsym': currency_symbol, 'limit': 100}

Besides that, we have also created a button to start the top_coin_by_cap method. Below is the full program up until now.

import json
from tkinter import *
import tkinter.ttk as tk
import requests
from Loader import Loader

win = Tk()  # Create tk instance
win.title("Crypto Calculator")  # Add a title
win.resizable(0, 0)  # Disable resizing the GUI
win.configure(background='white')  # change window background color

selectorFrame = Frame(win, background="white")  # create top frame to hold search button and combobox
selectorFrame.pack(anchor="nw", pady=2, padx=10)
currency_label = Label(selectorFrame, text="Select market / currency pair :", background="white")
currency_label.pack(anchor="w")

# create currency frame and text widget to display the incoming exchange rate data
s = StringVar()  # create string variable
currencyFrame = Frame(win)
currencyFrame.pack(side=TOP)
currency = Label(currencyFrame)
currency.pack()
text_widget = Text(currency, fg='white', background='black')
text_widget.pack()
s.set("Click the find button below to load the crypto currency - currency exchange rate from CCCAGG market")
text_widget.insert(END, s.get())

buttonFrame = Frame(win)  # create a bottom frame to hold the load button
buttonFrame.pack(side=BOTTOM, fill=X, pady=6)

# Create a combo box for crypto currency
crypto = tk.Combobox(buttonFrame)
crypto.pack(side=LEFT, padx=3)

# Create a combo box for markets
market = tk.Combobox(selectorFrame)
market.pack(side=LEFT, padx=3)

# create loader object then load the data into the crypto and market combo box
loader = Loader(crypto, market)
loader.load_data()

def top_coin_by_cap():  # Get a number of top 100 coins by their market cap

    sell_buy = ''
    try:
        currency_symbol = based.get()  # get the currency symbol
        url = "https://min-api.cryptocompare.com/data/top/mktcapfull"
        data = {'tsym': currency_symbol, 'limit': 100}
        r = requests.get(url, params=data)
        exchange_rate_s = json.loads(json.dumps(r.json()))

    except:
        print("An exception occurred")
    count = 0  # used to control new line for each symbol
    for value in exchange_rate_s['Data']:  # populate exchange rate string and the currency tuple
        for key, value1 in value.items():
            for key2, value2 in value1.items():
                if (type(value2) == dict):
                    for key3, value3 in value2.items():
                        if (key3 == "IMAGEURL"):
                            continue
                        sell_buy += str(key3) + " : " + str(value3) + "\n"
                    count += 1
                    if count == 2:
                        sell_buy += "\n"
                        count = 0

    text_widget.delete('1.0', END)  # clear all those previous text first
    s.set(sell_buy)
    text_widget.insert(INSERT, s.get())  # populate the text widget with new exchange rate data



def search_coin():  # display all market data of the currency/cryptocurrency pair

    sell_buy = ''
    try:
        currency_symbol = based.get()  # get the currency symbol
        url = "https://min-api.cryptocompare.com/data/top/totalvolfull"
        data = {'tsym': currency_symbol, 'limit': 100}
        r = requests.get(url, params=data)
        exchange_rate_s = json.loads(json.dumps(r.json()))

    except:
        print("An exception occurred")
    count = 0  # used to control new line for each symbol
    for value in exchange_rate_s['Data']:  # populate exchange rate string and the currency tuple
        for key, value1 in value.items():
            for key2, value2 in value1.items():
                if (type(value2) == dict):
                    for key3, value3 in value2.items():
                        if (key3 == "IMAGEURL"):
                            continue
                        sell_buy += str(key3) + " : " + str(value3) + "\n"
                    count += 1
                    if count == 2:
                        sell_buy += "\n"
                        count = 0

    text_widget.delete('1.0', END)  # clear all those previous text first
    s.set(sell_buy)
    text_widget.insert(INSERT, s.get())  # populate the text widget with new exchange rate data


def search_currency():  # display all market data of the cryptocurrency-currency pair

    sell_buy = ''
    try:
        base_crypto = crypto.get()  # get the desired crypto currency
        currency_symbol = based.get()  # get the currency symbol
        market_exchange = market.get()  # get the market
        url = "https://min-api.cryptocompare.com/data/generateAvg"
        data = {'fsym': base_crypto, 'tsym': currency_symbol, 'e': market_exchange}
        r = requests.get(url, params=data)
        exchange_rate_s = json.loads(json.dumps(r.json()))
    except:
        print("An exception occurred")

    for key, value in exchange_rate_s.items():  # populate exchange rate string and the currency tuple
        for key1, value1 in value.items():
            sell_buy += str(key1) + " : " + str(value1) + "\n"

    text_widget.delete('1.0', END)  # clear all those previous text first
    s.set(sell_buy)
    text_widget.insert(INSERT, s.get())  # populate the text widget with new exchange rate data

# Create currency combo box
base_currency = StringVar()  # create a string variable
based = tk.Combobox(selectorFrame, textvariable=base_currency)
based.pack(side=LEFT, padx=3)

action_search = tk.Button(selectorFrame, text="Search", command=search_currency,
                          state=DISABLED)  # button used to get the cryptocurrency/currency pair exchange rate
action_search.pack(side=LEFT)

# get top 100 coins by their total volume across all markets in the last 24 hours
action_coin_volume = tk.Button(selectorFrame, text="Trade Volume", command=search_coin, state=DISABLED)  # button used to get the top currency/cryptocurrency pair data
action_coin_volume.pack(side=LEFT)

# get top 100 coins by their market cap
action_coin_market_cap = tk.Button(selectorFrame, text="Market Cap", command=top_coin_by_cap, state=DISABLED)  # button used to get the top currency/cryptocurrency pair data
action_coin_market_cap.pack(side=LEFT)

def get_exchange_rate():  # this method will display the incoming exchange rate data after the api called

    global exchange_rate
    global base_crypto

    # read the currency file
    c_string = ''
    with open('currency.txt') as fp:
        for currency in fp.readlines():
            c_string += currency[:-1] + ","
    c_string = c_string[:-1]

    base_crypto = crypto.get()  # get the desired crypto currency
    try:
        url = "https://min-api.cryptocompare.com/data/price"  # url for API call
        data = {'fsym': base_crypto, 'tsyms': c_string}
        r = requests.get(url, params=data)
        exchange_rate_s = json.loads(json.dumps(r.json()))

    except:
        print("An exception occurred")

    curr1 = tuple()  # the tuple which will be populated by currency
    sell_buy = ''

    for key, value in exchange_rate_s.items():  # populate exchange rate string and the currency tuple
        sell_buy += base_crypto + ":" + key + "  " + str(value) + "\n"
        curr1 += (key,)

    # fill up combo boxes for both currency and cryptocurrency
    based['values'] = curr1
    based.current(0)

    text_widget.delete('1.0', END)  # clear all those previous text first
    s.set(sell_buy)
    text_widget.insert(INSERT, s.get())  # populate the text widget with new exchange rate data

    # enable all buttons
    action_search.config(state=NORMAL)
    action_coin_volume.config(state=NORMAL)
    action_coin_market_cap.config(state=NORMAL)


action_vid = tk.Button(buttonFrame, text="Load",
                       command=get_exchange_rate)  # button used to load the exchange rate of currency pairs
action_vid.pack()

win.iconbitmap(r'ico.ico')
win.mainloop()
EUR vs top cryptocurrency

This project will continue for a while and we might rearrange the button and combo box in the future to suit the layout of the user interface.

Please follow and like us:
error0

Leave a Reply

Your email address will not be published. Required fields are marked *