CryptoCurrency eXchange Trading Library | Hive & HBD Prices

4 Min Read
837 words

ccxt.png

CCXT is a cryptocurrency exchange trading library for Javascript, Python, and PHP. It is a trading API that supports more than 120 crypto exchanges and continues to add more. While my personal preference is to use in my python projects, it is nice to know that it can be used in Javascript and PHP based projects. For more details feel free to visit CCXT GitHub page.

What is really great about this library is that it provides access to many exchanges which include major exchanges like Coinbase, CoinbasePro, Binance, Bittrex, Huobi, etc. Various exchanges have their own python packages to interact with them. I think it is much better to have one interface that can connect to multiple exchanges, get necessary data, and even automate trading.

Most of the functions are available as public and do not need API access credentials. However, for projects that need access to a trading account, we will need to obtain the API access credentials for specific exchanges that we have accounts at.

This API can be used for simple things like getting data on what is being traded on exchanges, historical cryptocurrency prices. It can also be used for more complex projects like trading bots, web applications, etc.

Getting started with this API is super easy. For python we need to first pip install ccxt. Once that is done we can start using it in our python code.

To demonstrate how work with ccxt, I wanted to see what exchanges have HIVE and HBD listed, what trading pairs are available and the latest prices.

First to see what exchanges are supported we can write the following code:

import ccxt

print(ccxt.exchanges)

If we already know what exchange we would like to use in that list, we can start connecting to this exchange, and get trading pairs and latest data about this trading pairs as below:

import ccxt

binance = ccxt.binance()
print(binance.load_markets())

If we need to access our accounts we will need to pass api key and secrets as a dictionary argument like this:

import ccxt

binance = ccxt.binance({
    'apiKey': 'YOUR_PUBLIC_API_KEY',
    'secret': 'YOUR_SECRET_PRIVATE_KEY',
})

As you can see it is very easy to get started with CCXT. Exploring more available methods and properties we can write much more useful code.

As I mentioned I was curious to see if I could use CCXT to see all the exchanges that have HIVE and HBD listed, what trading pairs they have available, and latest prices. Let's do that now.

import ccxt

def create_table(prices):
    table = '<table><tr><th>Ticker Symbol</th><th>Exchange</th><th>Price</th></tr>'
    for price in prices:
        row = f'<tr><td>{price[0]}</td><td>{price[1]}</td><td>{str(price[2])+" "+price[3]}</td></tr>'
        table += row
    table += '</table>'
    return table

prices = []
for exchange_id in ccxt.exchanges:
    if exchange_id == 'cdax':
        continue

    try:
        exchange_class = getattr(ccxt, exchange_id)
        exchange = exchange_class()
        markets = exchange.load_markets()
    except:
        continue

    for market in markets:
        if 'HIVE' in market or 'HBD' in market:
            if 'HBDC' in market:
                continue
            ticker = exchange.fetch_ticker(market)
            if ticker['symbol'].split('/')[1] == 'BTC':
                prices.append((market, exchange_id, format(ticker['close'], '.8f'), ticker['symbol'].split('/')[1]))
            else:
                prices.append((market, exchange_id, ticker['close'], ticker['symbol'].split('/')[1]))

table = create_table(prices)
print(table)

What the main part of the code doing is, first we are trying to identify all the exchanges, then we are getting the method by the same name to create the instance of the exchange. Then we are looking what ticker symbols are traded in the exchange. If any of the tickers include Hive or HBD we are storing ticker symbol, exchange name, last close price in prices list. create_table is a helper function that puts together the results in a table format, so I can include them in the post. You can scroll down to see the results.

Initially when I was testing I was getting 12 exchanges that have Hive traded on. However, something went wrong when I tried to get the prices of CDAX exchange. After looking closely I was convinced Hive or HBD were not actually listed there. So I have do include a line of code to exclude CDAX.

Another interesting thing we can see is from the results is that, for some reason HBTC exchange named the ticker symbol as HIVE1/USDT instead of just HIVE without 1.

I don't know what exchange LATOKEN is, but from the results we can see while HIVE seems to be listed, prices there are all zero.

This is not a full list of exchanges that have Hive or HBD listed. We can see how some exchanges like ionomy, blocktrades, beaxy, mxc, are not there. It is still great to see that all the major exchanges like Binance, Bittrex, Huobi, Upbit are supported in CCXT.

The code above can also be used for any other coins/tokens. If you learn about a new coin/token and would like to see if where it is traded, simply change the comparison logic from HIVE and HBD to that coin/token.

Hive and HBD Prices

Ticker SymbolExchangePrice
HIVE/BNBbinance0.00429 BNB
HIVE/BTCbinance0.00000946 BTC
HIVE/USDTbinance0.3521 USDT
HIVE/KRWbithumb410.3 KRW
HBD/BTCbittrex0.00002654 BTC
HIVE/BTCbittrex0.00000950 BTC
HIVE/USDbittrex0.35458 USD
HIVE/USDTbittrex0.35712 USDT
HIVE/USDTgateio0.3524 USDT
HIVE1/USDThbtc0.358 USDT
HIVE/USDThuobipro0.3525 USDT
HIVE/HThuobipro0.025623 HT
HIVE/BTChuobipro0.00000947 BTC
HIVE/IDRindodax5063.0 IDR
HIVE/USDTlatoken0.0 USDT
HIVE/BTClatoken0.00000000 BTC
HIVE/ETHlatoken0.0 ETH
HIVE/KRWprobit410.9 KRW
HIVE/USDTprobit0.3526 USDT
HIVE/BTCprobit0.00000948 BTC
HIVE/BTCstex0.00000107 BTC
HIVE/BTCupbit0.00000955 BTC
HBD/BTCupbit0.00002515 BTC
HIVE/KRWupbit413.0 KRW

Posted Using LeoFinance Beta