DEV Community

Cover image for Python adventure: Crypto-currency market explorer
codesharedot
codesharedot

Posted on

Python adventure: Crypto-currency market explorer

You may have heard about crytpo-currency, alt-coins and other things. So where do you get those crypto coins?

Python can help. Get the market data for a currency, then filter through the data and show the exchanges trading them.

Fetch data

So we can do that with Python code. First import a few modules and ask the user what coin they want:

#!/usr/bin/python3
import requests                                                                                                                                
import json                                                                                                                                    
import os                                                                                                                                      

coin = input("Coin: ")                                                                                                                         

Then we grab the JSON formatted currency data

#!/usr/bin/python3
api_url = "https://coinmarketbook.cc/api/ticker/" + coin                                                                               
response = requests.get(api_url)                                                                                                       
response_json = response.json()                                                                                                                
print(response_json)   

Filter data

The data has this format:

{'symbol': 'ETH', 'current_price': '218.94323290', 'bid': '484.52353643', 'ask': '0.00000000', 'volume_24h': '9692178886.24000000', 'market_cap': '23606418360.00000000', 'buy_order': '75703291.01651694', 'created_at': '2018-09-25T18:54:35.711Z', 'updated_at': '2019-09-20T14:09:35.216Z', 'sell_order': '0.00000000', 'name': 'Ethereum', 'markets': ['bitmex:ETH/USD', 'bitmex:ETHU19', 'binance:ETH/BTC', 'binance:ETH/USDT', 'binance:ETH/TUSD', 'binance:ETH/PAX', 'binance:ETH/USDC', 'bithumb:ETH/KRW', 'bitfinex:ETH/USD', 'bitfinex:ETH/BTC', 'bitfinex:ETH/EUR', 'bitfinex:ETH/JPY', 'bitfinex:ETH/GBP', 'bitfinex:ETH/USDT', 'bitfinex:ETH/F0:', 'okex:ETH/USDT', 'okex:ETH/USD', 'okex:ETH/CNY', 'okex:ETH/USDK', 'okex:ETH/BTC', 'huobipro:ETH/USDT', 'huobipro:ETH/BTC', 'huobipro:ETH/HUSD', 'bittrex:ETH/BTC', 'bittrex:ETH/USDT', 'bittrex:ETH/USD', 'poloniex:ETH/BTC', 'poloniex:ETH/USDT', 'poloniex:ETH/USDC', 'kucoin2:ETH/PAX', 'kucoin2:ETH/USDC', 'kucoin2:ETH/TUSD', 'kucoin2:ETH/USDT', 'kucoin2:ETH/BTC', 'kucoin2:ETH/DAI', 'zb:ETH/USDT', 'zb:ETH/QC', 'zb:ETH/BTC', 'lbank:ETH/USDT', 'lbank:ETH/BTC', 'exx:ETH/USDT', 'exx:ETH/BTC', 'exx:ETH/CNYT', 'upbit:ETH/KRW', 'upbit:ETH/BTC', 'upbit:ETH/USDT', 'bitforex:ETH/USDT', 'bitforex:ETH/BTC', 'bitforex:ETH/TUSD', 'hitbtc2:ETH/BTC', 'hitbtc2:ETH/USDT', 'hitbtc2:ETH/TUSD', 'hitbtc2:ETH/DAI', 'hitbtc2:ETH/EURS', 'hitbtc2:ETH/GUSD', 'hitbtc2:ETH/PAX', 'hitbtc2:ETH/USDC', 'hitbtc2:ETH/EOSDT', 'hitbtc2:ETH/Bitcoin Cash', 'liquid:ETH/EUR', 'liquid:ETH/USDC', 'liquid:ETH/GUSD', 'liquid:ETH/AUD', 'liquid:ETH/BTC', 'liquid:ETH/ANCT', 'liquid:ETH/PHP', 'liquid:ETH/IDR', 'liquid:ETH/JPY', 'liquid:ETH/USD', 'liquid:ETH/HKD', 'liquid:ETH/SGD', 'liquid:ETH/DAI'], 'asset_id': 'ethereum', 'highest_volume_market': 'binance'}

One of the fields in the JSON data is the available markets, we can parse that like this:

#!/usr/bin/python3
print(response_json['markets'])                                                                                                                

for market in response_json['markets']:                                                                                                        
    if "EUR" in market:                                                                                                                        
        print(market)    

In the if statement above I filter for euros. They are trading in other currencies (USD, GBP etc), it's just a filter.

Complete code

The code below collects the markets for the coin that the user types. The modules are all installed by default, except maybe requests.

#!/usr/bin/python3
import requests
import json
import os

coin = input("Coin: ")

api_url = "https://coinmarketbook.cc/api/ticker/" + coin
response = requests.get(api_url)
response_json = response.json()   
#print(response_json)
#print(response_json['markets'])

for market in response_json['markets']:
    if "EUR" in market:
        print(market)

Related links:

Top comments (0)