In this article I will show you how to get the price of cryptocurrencies like Bitcoin in real-time using Python.
What is bitcoin and who created it ?
Bitcoin is a digital currency and a payment system invented by an unknown group or person by the name Satoshi Nakamoto , who published the invention in 2008 and released it as open source software in 2009. It is the first decentralized digital currency, meaning the system works without a single administrator or central bank, you can use them in every country, your account cannot be frozen, and there are no prerequisites or limits .
Bitcoins are transferred directly from person to person, also known as peer to peer. The cryptographic transactions are verified by a network of people and recorded in a public distributed ledger called the block chain . Note that once a payment is made it can’t be reversed, and if you lose your wallet, you lose your bitcoins.
How much is one bitcoin worth ?
Bitcoin is very volatile, the price of one bitcoin is liable to change rapidly and unpredictably. Earlier in January 2017 one bitcoin was equivalent to $985 USD. If you had invested $100 USD in 2010, you would be worth about $72 million USD as of (12/21/2017).
You can read more about Bitcoin here.
The first thing that I like to do before writing logic in my code is to put a description of what the program is about.
#Description: Get the current price of Bitcoin
Next, I will import the request library.
#Import the requests library
Now let’s store the URL of the ticker, to get the .json file of the cryptocurrencies. This link is the API from coinmarket.com.
Update: The API URL is no longer working for the free version :(
TICKER_API_URL = 'https://api.coinmarketcap.com/v1/ticker/'
Let’s create a function to get the latest crypto currency price for a specific ‘crypto’ like Bitcoin, Litecoin or Ethereum.
response = requests.get(TICKER_API_URL+crypto)
response_json = response.json()
Test the function to see if it returns the current price of the crypto currency.
Create a main function to get the current price of the crypto currency, and print any price change when the API updates.
last_price = -1
crypto = 'bitcoin'
price = get_latest_crypto_price(crypto)
if price != last_price:
print('Bitcoin price: ',price)
last_price = price
Last but not least, we will run the main function.
You can easily get the price of litecoin, ethereum and some other crypto currencies simply by replacing the bitcoin string in this program with the cryptocurrencies of your choice !
Conclusion and Resources
That is it, you are done creating your program to get the current price of cryptocurrencies !
Again, if you want, you can watch and listen to me explain all of the code in my YouTube video.
Python is considered an easy high level language to learn. Considering it’s one of the fastest growing programming languages and a programming language many companies and computer science departments use, it is definitely a language you want to get familiar with. Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.
If you are interested in reading about machine learning to immediately get started with problems and examples, I recommend you read Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems.
It is a great book for helping beginners learn to write machine-learning programs and understanding machine-learning concepts.
Thanks for reading this article, I hope it’s helpful to you!