DEV Community

Cover image for Comparing alt-coin trends with Python code
codesharedot
codesharedot

Posted on

Comparing alt-coin trends with Python code

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

This question can be answered with Python. 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.

Line Trend comparison

We wrap the code to get the price trend 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

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

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).

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:

Top comments (0)