Keeping track of your cryptocurrencies with Python

in #python7 years ago (edited)

As many of you are aware of, is it not good practice to store big amounts of cryptocurrencies on exchanges or online wallets. Although you have to be careless and have a portion of bad luck to lose your funds or get them stolen, it is advice to store them somewhere offline.  Personally, I have created paper wallets for all my currencies, but a disadvantage is that is harder to ‘follow’ the performances of the individual currencies, especially when you have invested in dozens of cryptocurrencies.  

 

In this article I will give some tips and tricks to keep track of your crypto’s in Python. I admit it is easier to use an API in Excel (for example: https://api.coinmarketcap.com/v1/ticker/) and in Excel it is also easier to create nice tables, which is not surprising given that Python is a programming language. However, a disadvantage of the API is that the price is not always up-to-date. The CoinMarketCap API is updated every 5 minutes, however I would prefer to base my calculations on the price shown on the individual pages of the currencies. Regarding Ethereum, for example, I would like to obtain $227.60 and by using my initial trade price and amount, I can easily calculate my profit (or loss) and return. 

Python actually has a great tool to read specific contents from the underlying HTML, called BeautifulSoup (the name actually explains what it does). The only thing you have to do, is to tell the method where to look in the HTML code. Usually the structure of the HTML code is the same within a webpage, so once you have found the price of one currency, you can do it for all of them. First, you have to dig into the HTML code by doing the following steps.

  1. Go to https://coinmarketcap.com/ and choose a currency (here: Ethereum).
  2. ‘Add’ the HTML source to the webpage (in this example I use Google Chrome, where you should select ‘Inspect element’ after a right-mouse click).
  3. Find out where the price is mentioned. Simply search (Ctrl + F) for 227.60.
  4. As you can see, the class of the ‘span’ that contains the price is called “text-large”, which will be your input for BeautifulSoup. Probably it is also possible to get this value focussing on the ‘div’ or something else, but I find this the easiest option. Although for large HTML files, it might not be the fastest option.
  5. Finally, we move to Python. I import the BeautifulSoup library from the bs4 package and we need the urllib package too. I wrote a simple method to obtain the price for a given currency; just give a string of the name of the currency as input. In line 19 you ask BeautifulSoup to find a ‘span’ with class ‘text-large’. Adding .contents will return a list of which you need the first item (i.e. [0]). However, we remove the dollar sign by [1:] and turn the string into a floating number.  

Personally, I do not make a lot of transactions, but when the amount of a specific cryptocurrency changes a lot, you might want to automate that. As far as I know, the current balance of a Steem address is not publicly available, but for many other cryptocurrencies it is. For example, on blockchain.info you can simply enter a public key and check the balance of Bitcoins.

We can obtain the ‘Final balance’ of this Bitcoin address by selecting the correct cell of the table with header ‘Transactions’. For this purpose, it is recommended to use lambda. In fact, I should have used it in the method above too because it is much easier to read. As you can see the ‘id’ in that case was ‘quote_price’ and you don’t need the class name ‘text-large’ anymore. The td means that we are looking for a table cell, which should have an attribute id named final_balance. The extension .text returns the text in the cell and by [:-4] we are left with a number (in fact, I could have used '.text' instead of  '.contents[0]' in the first example).

This was sort of an introduction to BeautifulSoup in Python. I have no experience with HTML coding, so I hope my explanation is correct. There is always room for extensions and improvements. For example, non-American holders of crypto currencies might also be interested in the real-time exchange rate from US dollars to their local currency. Nevertheless, I hope this article was useful for you all and feel free to ask questions!

Sort:  

Nice information

This is cool, I'm just trying to learn bs4.
Could you please post your whole code?

Thanks! Well, I'm also quite new to it. Right now, I have only tried the things I mentioned in the story, so my code won't be very helpful

Congratulations @theonlywayisup! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You got a First Vote

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.24
TRX 0.24
JST 0.038
BTC 95135.46
ETH 3281.35
USDT 1.00
SBD 3.37