Home » Perform Currency conversions and get Bitcoin prices in Python

Perform Currency conversions and get Bitcoin prices in Python

by maxguy71

In this article we look at a library which allows you to get Foreign exchange rates, Bitcoin prices and perform currency conversions.

The CCXT library is used to connect and trade with cryptocurrency exchanges and payment processing services worldwide.

It provides quick access to market data for storage, analysis, visualization, indicator development, algorithmic trading, strategy backtesting, bot programming, and related software engineering.

Features

  • support for many cryptocurrency exchanges
  • fully implemented public and private APIs
  • optional normalized data for cross-exchange analytics and arbitrage
  • an out of the box unified API that is extremely easy to integrate
  • works in Node 10.4+, Python 3, PHP 5.4+, and web browsers

Installation

pip install ccxt
import ccxt
print(ccxt.exchanges) # print a list of all available exchange classes

The library supports concurrent asynchronous mode with asyncio and async/await in Python 3.5.3+

ccxt.async_support as ccxt # link against the asynchronous version of ccxt

Code Examples

import os
import sys

root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root + '/python')

import ccxt  # noqa: E402


print('CCXT Version:', ccxt.__version__)


country = 'US'
exchanges = []
for exchange_id in ccxt.exchanges:
    try:
        exchange = getattr(ccxt, exchange_id)()
        if country in exchange.countries:
            print(country, exchange_id, exchange.countries)
            exchanges.append(exchange)
    except Exception as e:
        print(type(e).__name__, str(e))

import os
import sys
from asciichartpy import plot

# -----------------------------------------------------------------------------

this_folder = os.path.dirname(os.path.abspath(__file__))
root_folder = os.path.dirname(os.path.dirname(this_folder))
sys.path.append(root_folder + '/python')
sys.path.append(this_folder)

# -----------------------------------------------------------------------------

import ccxt  # noqa: E402

# -----------------------------------------------------------------------------

kraken = ccxt.kraken()
coinbasepro = ccxt.coinbasepro()

symbol = 'BTC/USD'

# each ohlcv candle is a list of [ timestamp, open, high, low, close, volume ]
index = 4  # use close price from each ohlcv candle


def print_chart(exchange, symbol, timeframe):

    print("\n" + exchange.name + ' ' + symbol + ' ' + timeframe + ' chart:')

    # get a list of ohlcv candles
    ohlcv = exchange.fetch_ohlcv(symbol, timeframe)

    # get the ohlCv (closing price, index == 4)
    series = [x[index] for x in ohlcv]

    # print the chart
    print("\n" + plot(series[-120:], {'height': 20}))  # print the chart

    last = ohlcv[len(ohlcv) - 1][index]  # last closing price
    return last


last = print_chart(kraken, 'BTC/USD', '1h')
print("\n" + kraken.name + " ₿ = $" + str(last) + "\n")  # print last closing price

last = print_chart(coinbasepro, 'BTC/USD', '1h')
print("\n" + coinbasepro.name + " ₿ = $" + str(last) + "\n")  # print last closing price

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More