Zephyrnet Logo

4 Great Cryptocurrency Libraries for Python

Date:

There is no doubt that the popularity of cryptocurrency has skyrocketed in recent years. Getting your head wrapped around just how exactly blockchain works or what Bitcoin actually is can be frustrating and confusing. There are blockchains, ledgers, contracts and even more buzzwords than the “Big Data” revolution.

If you’re just dipping your toes into the crypto world and have some development experience, I recommend checking out some publicly available libraries. This is an easy way to jump start your experience with crypto, get involved in the community and learn a little about the technical side of things.

In this article we’ll explore some excellent Python libraries that let you perform a range of tasks in the world of cryptocurrency. You’ll be able to get live price quotes for various coins, connect to different exchanges, place trades and even construct a live algorithmic trading bot of your very own. Let’s take a look.

CryptoCompare Logo. Source.

CryptoCompare is one of the leading sites for live cryptocurrency price quotes and information. This site has been around since 2015 and provides users with the latest crypto news, detailed coin information and market statistics. CryptoCompare also reviews different cryptocurrency exchanges, wallets and services to arm buyers with the most up-to-date information possible.

CryptoCompare also provides a ton of data services and a fully-functional API. This is where the cryptocompare library comes in. This Python library provides a convenient wrapper for the CryptoCompare API. You can install the library like so:

pip3 install cryptocompare

Let’s take a look at some basic usage to get the current price of Bitcoin against USD:

import cryptocompareprice = cryptocompare.get_price('BTC', 'USD')print(price)

If we run the code above we should see the current price displayed:

{'BTC': {'USD': 36176.4}}

This only scratches the surface of what the CryptoCompare API is capable of. You can also submit orders to an exchange, look at historical prices and get current market information.

Check out the official CryptoCompare website for even more live stats and data.

This next library is a little more complicated than a simple API wrapper like cryptocompare. Cryptofeed allows you to stream market data from many different exchanges directly to your application. You can setup custom functions to handle each update, store the data in one of the supported backends or just use the data to build a live trading application.

This library has a high level of complexity and can be difficult to understand in the beginning, but it is extremely powerful. To get started, install the module using the following command:

pip3 install cryptofeed

There are a multitude of examples for implementing Cryptofeed provided in the repository, but I’ve found the National Best Bid and Offer (NBBO) example to be particularly interesting. Here is a simplified version that obtains the NBBO below:

from cryptofeed import FeedHandler
from cryptofeed.exchanges import Coinbase
def nbbo_update(symbol, bid, bid_size, ask, ask_size, bid_feed, ask_feed):
print(
f'Pair: {symbol} ' +
f'Bid Price: {bid:.2f} ' +
f'Bid Size: {bid_size:.6f} ' +
f'Bid Feed: {bid_feed} ' +
f'Ask Price: {ask:.2f} ' +
f'Ask Size: {ask_size:.6f} ' +
f'Ask Feed: {ask_feed}'
)
feed = FeedHandler()
feed.add_nbbo([Coinbase], ['BTC-USD'], nbbo_update)
feed.run()

If you run the code above, the handler will stream the current NBBO information provided by the Coinbase exchange. The output will simply be printed to the console to be able to see what is going on. There is a lot of volume, so be prepared for lots of information. This code will execute in a loop, updating with the most current data, until you exit.

The Cryptofeed library is an wonderful resource to have available for building high-quality cryptocurrency trading applications. I highly recommend checking out the detailed documentation for more usage examples and updated exchange info.

Freqtrade Logo. Source.

This isn’t exactly a single Python library, but it is an excellent example of a Python trading application. This application is written entirely in Python and allows you to setup a custom trading bot, complete with a wide variety of trading strategies and the ability to respond to different commands. Freqtrade supports multiple different exchanges and provides easy integration with Telegram.

Another great thing about Freqtrade is that your trading strategies are also written in Python. You don’t have to mess around with another unfamiliar language or download huge configuration files to set up a simple strategy. If you’re using the Docker installation you can view the sample strategy in user_data/strategies/sample_strategy.py, and it is also available here.

To get started with Freqtrade, head over to the installation section of the official documentation. You can also get up an running even quicker using Docker as well.

CCXT Logo. Source.

CCXT is a truly monolithic trading library. This library has a plethora of features and a lot of community support. The library itself is offered in JavaScript, Python and PHP as straightforward, easy to use modules. I would argue that CCXT is similar to Cryptofeed, but much easier to get started with. You don’t have to contend with a run loop and a constant stream of data. You can simply make calls to CCXT to get orders, exchange information and more.

Installing the CCXT module is straightforward:

pip3 install ccxt

To outline just how easy CCXT is to use, let’s look at how we might obtain the latest trade data from the Kraken exchange:

import ccxtkraken = ccxt.kraken()for trade in kraken.fetch_trades('BTC/USD'):
print(
f"date: {trade['datetime']} | " +
f"symbol: {trade['symbol']} | " +
f"price: {trade['price']} | " +
f"amount: {trade['amount']} | " +
f"cost: {trade['cost']} | " +
f"side: {trade['side']} | " +
f"type: {trade['type']}"
)

After running the code above, you should see a long list of trade executions with details about the price, amount, timestamp, etc. The data is fetched as one list containing all of the trade information vs a stream of data. CCXT also offers asynchronous concurrency mode support as well, you can read more about implementing that in the official documentation.

CCXT provides a flexible tool with compatibility across multiple languages, exchanges and platforms. This is a well thought out, well supported platform that enables you to build pragmatic trading applications.

Yes, that’s right, you too can build your very own blockchain! Although this process sounds prohibitively difficult, it is actually not too hard to build a simple blockchain. Using Python you can create a small, functional blockchain in less than an hour or two. Diving into the exact process is beyond the scope of this article, but I’ll provide some excellent resources below for how to get started:

Source: https://medium.com/swlh/4-great-cryptocurrency-libraries-for-python-d8cecfac0310?source=rss——-8—————–cryptocurrency

spot_img

Latest Intelligence

spot_img