Intro: Crypto-currency ticker with Python and Robin

You’ll need Python 3, ccxt installed, and pandas installed.

The Python script we use will access the live order book ticker for each individual asset pair.
Once the data is retrieved, it will calculate the price of each asset in terms of USDT.
(Python shameless cribbed in large part from here.)

Robin code screenshot:

Here’s the Python code:

import ccxt
import json
import pandas as pd 
# https://blog.shrimpy.io/blog/ccxt-live-crypto-exchange-price-ticker

"""
The script will access the live order book ticker for each individual asset pair.
Once the data is retrieved, it will calculate the price of each asset in terms of USDT. 
"""

hitbtc = ccxt.hitbtc()

ticker_data = []

# fetch the BTC/USDT ticker for use in converting assets to price in USDT
bitcoin_ticker = hitbtc.fetch_ticker('BTC/USDT')

# calculate the ticker price of BTC in terms of USDT by taking the midpoint of the best bid and ask
bitcoinPriceUSDT = (float(bitcoin_ticker['info']['ask']) + float(bitcoin_ticker['info']['bid'])) / 2

# fetch the tickers for each asset on HitBTC
# this will take as long as 5 minutes
for trading_pair in hitbtc.load_markets():
    base = trading_pair.split('/')[0]
    quote = trading_pair.split('/')[1]
    if quote == 'BTC':
        pair_ticker = hitbtc.fetch_ticker(trading_pair)
        pair_ticker['base'] = base
        ticker_data.append(pair_ticker)

prices = []

# create the price tickers for each asset, removing unnecessary data
for ticker in ticker_data:
    price = {}
    price['symbol'] = ticker['base']
    price['price'] = ((float(ticker['info']['ask']) + float(ticker['info']['bid'])) / 2) * bitcoinPriceUSDT
    prices.append(price)

# additional processing is required for assets without BTC pairs
# additional processing is required to calculate the 24-hour price change

jsonize = json.dumps(prices)
print(jsonize)

f = open("C:\\work\\RobinTests\\ccxt.json", "w")
f.write(jsonize)

df = pd.read_json("C:\\work\\RobinTests\\ccxt.json")
df.to_excel("C:\\work\\RobinTests\\ccxt.xlsx", index=False)

You’ll see from the code it uses ‘json’ to convert the Python list being output to JSON. Then we can use ‘pandas’ to write it straight to Excel. Note the ‘index=False’ flag used to drop the index.

Screenshot of abbreviated output (there were 374 results):

Regards,
burque505