Comparing alt-coin trends with Python code
Tags: blockchain, bitcoin, python
Published: 2019-09-19 13:36:40

How do alt coin trends compare? If you're into crypto-currency you may wonder about this.

This question can be answered with <a href="https://python.org">Python</a>. The way this works is by getting all the json data for each coin, then grabbing the years and finally plotting them into a plot.

<img src="https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FjR8EDxMbqi1QQ%2Fgiphy.gif&f=1&nofb=1">

## Line Trend comparison

We wrap the code to <a href="https://gitlab.com/codelivespeed/bitcoin-price-chart/blob/master/example.py">get the price trend</a> in a function.

Then call the functions to get the lists. As parameter you can define the currency.

    #!/usr/bin/python3
    prices = getCoin("litecoin")
    prices_btc = getCoin("bitcoin")
    prices_eth = getCoin("ethereum")
    prices_bcn = getCoin("binance-coin")
    prices_eos = getCoin("eos")

This puts the prices in lists. You can then plot them all together with matplotlib.

    #!/usr/bin/python3
    plt.title('price from 2014')
    plt.ylabel('Price in USD')
    plt.xlabel('Years from 2014')
    
    plt.plot(x, prices, label='litecoin', color='gold')
    #plt.plot(x, prices_btc, label='bitcoin', color='silver')
    plt.plot(x, prices_eth, label='ethereum', color='silver')
    plt.plot(x, prices_bcn, label='binance-coin', color='green')
    plt.plot(x, prices_eos, label='eos', color='blue')
    plt.legend()

![price trend with python](https://thepracticaldev.s3.amazonaws.com/i/ardvkcfornbasilk84xh.png)

This shows the biggest bull market is several years ago. Then there was a slight increase for some, but others like Ethereum had a dump time. This makes sense, because many early investors wanted to cash out.

We see binance coin increasing, binance is an exchange, so growth of their business likely sees their coin increase in value.

## Bar plot trend

Can we get more insights with a different plot?

Lets try a stacked bar plot. Change the code from plot to bar.

    #!/usr/bin/python3                                                                                                                                            
                                                                                           
    plt.bar(x, prices_eth, label='ethereum', color='silver')                                                                                       
    plt.bar(x, prices_bcn, label='binance-coin', color='green')                                                                                    
    plt.bar(x, prices_eos, label='eos', color='blue')

![Price comparison bar chart](https://thepracticaldev.s3.amazonaws.com/i/fxk7vwkn45ghrxfqpvhj.png)

Here we see that Ethereum has gained many times compared to other coins. But that doesn't mean the increase for others has been small. Binance is in a bust trend. If you were to buy them in 2017, they would be $0.89 but now they are $21 (this is not financial advice). 

<img src="https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia1.tenor.com%2Fimages%2Fb4cef4aff23de2712130b058194a1ce0%2Ftenor.gif%3Fitemid%3D14988921&f=1&nofb=1">

If you want to play around with the code:

    #!/usr/bin/python3
    import time
    import os
    import json
    import requests
    from bs4 import BeautifulSoup
    import csv
    import sys
    from time import sleep
    from time import gmtime, strftime
    import matplotlib.pyplot as plt
    
    
    def getCoin(coin):
        enddate = strftime("%Y%m%d", gmtime())
        r  = requests.get("https://coinmarketcap.com/currencies/" + coin + "/historical-data/?start=20140101&end={0}".format(enddate))
        data = r.text
        
        soup = BeautifulSoup(data, "html.parser")
        table = soup.find('table', attrs={ "class" : "table"})
        
        prices = []
        
        for row in table.find_all('tr'):
            addPrice = False
            tag = row.findAll('td')
            for val in tag:
                value = val.text
        
                if "Sep 10" in value:
                    print(value)
                    addPrice = True
        
            if addPrice == True:
                prices.append( tag[3].text )
        
        # flip list, months are in reverse order
        prices = prices[::-1]
        for i in range(0,len(prices)):
            prices[i] = float(prices[i])
    
        while len(prices) < 6:
            prices = [0] + prices
            
        return prices
    
    prices = getCoin("litecoin")
    prices_btc = getCoin("bitcoin")
    prices_eth = getCoin("ethereum")
    prices_bcn = getCoin("binance-coin")
    prices_eos = getCoin("eos")
    
    x = list(range(0, len(prices)))
       
    plt.title('price from 2014')
    plt.ylabel('Price in USD')
    plt.xlabel('Years from 2014')
    
    plt.plot(x, prices, label='litecoin', color='gold')
    #plt.plot(x, prices_btc, label='bitcoin', color='silver')
    plt.plot(x, prices_eth, label='ethereum', color='silver')
    plt.plot(x, prices_bcn, label='binance-coin', color='green')
    plt.plot(x, prices_eos, label='eos', color='blue')
    plt.legend()
    
    os.system("rm -rf chart.png")
    time.sleep(1)
    plt.savefig('chart.png')


Related links:

* <a href="https://coinmarketcap.com/">Data source for alt coin prices</a>
* <a href="http://pythonbasics.org">Learn Python programming</a>
* <a href="https://pythonbasics.org/matplotlib-bar-chart/">Bar chart examples</a>