Finance, Data Science

Cryptocurrency Network Analysis

graph
In [11]:
fig.show()

Objective

To illustrate a network graph of cryptocurrencies.

Data

60 cryptocurrencies' whitepapers are used for the analysis.

Data is manually corrected in 2018.

The coins are all listed on Binance as of 2018.

List of coins: Adacoin, Adex, Aelf, Aeron, Augur, Basicattentiontoken, Binancecoin, Bitcoin, Bytecoin, Chatcoin, Cindicator, Dash, Decentralizedmachinelearning, Dentcoin, Digixdao, Dock, Enigma, Enjincoin, Eos, Ethereum, Etherparty, Ethos, Foenixredpulse, Genesisvision, Gifto, Gochain, Holo, Icon, Iota, Lunyr, Mco, Metal, Moedaloyalitypoints, Monero, Nanocoin, Neocoin, Nucleusvison, Omisego, Ontology, Ost, Populus, Pundix, Qtum, Selfkey, Siacoin, Stellar, Substratum, Timenewbank, Tron, Vechain, Verge, Wabi, Waltonchain, Wanchain, Waves, Wepower, Yoyow, Zerocash, Zerox, Zilliqa

In [1]:
import plotly.graph_objects as go
import networkx as nx
import glob
import re
import os
import nltk

import plotly.offline as pyo
pyo.init_notebook_mode()
In [2]:
def find_case_insensitve(dirname):
    f = {}
    for filename in glob.glob(dirname):
        _, ext = os.path.splitext(filename)
        basename = os.path.basename(filename)
        if ext.lower() in '.txt':
            f[basename] = filename
    return f
In [3]:
data = []
files = find_case_insensitve('./listed/*')
for key, value in files.items():
    with open(value) as j:
        tex = j.read().lower()
        tex = re.sub(r'\d+', '000', tex)
        lis = set([vocab for vocab in nltk.word_tokenize(tex) if len(vocab) > 2])
        data.append((value[9:].replace('.txt', ''), lis))
In [4]:
all_coin_name = [x.replace('.txt', '') for x in os.listdir('./listed') if not x.startswith('.')]
In [5]:
G = nx.Graph()
for coinname in all_coin_name:
    G.add_node(coinname)
In [6]:
for coin, text in data:
    for cc, tt in data:
        if cc == coin:
            continue
        if coin.lower() in tt:
            G.add_edge(cc, coin)
In [7]:
pos = nx.spring_layout(G, k=0.5, iterations=50)
for n, p in pos.items():
    G.nodes[n]['pos'] = p
In [8]:
edge_trace = go.Scatter(
    x=[],
    y=[],
    line=dict(width=0.5,color='#888'),
    hoverinfo='none',
    mode='lines')
for edge in G.edges():
    x0, y0 = G.nodes[edge[0]]['pos']
    x1, y1 = G.nodes[edge[1]]['pos']
    edge_trace['x'] += tuple([x0, x1, None])
    edge_trace['y'] += tuple([y0, y1, None])
node_trace = go.Scatter(
    x=[],
    y=[],
    text=[],
    mode='markers',
    hoverinfo='text',
    marker=dict(
        showscale=True,
        colorscale='RdBu',
        reversescale=True,
        color=[],
        size=15,
        colorbar=dict(
            thickness=10,
            title='Node Connections',
            xanchor='left',
            titleside='right'
        ),
        line=dict(width=0)))
for node in G.nodes():
    x, y = G.nodes[node]['pos']
    node_trace['x'] += tuple([x])
    node_trace['y'] += tuple([y])
In [9]:
for node, adjacencies in enumerate(G.adjacency()):
    node_trace['marker']['color']+=tuple([len(adjacencies[1])])
    node_info = adjacencies[0]
    node_trace['text']+=tuple([node_info])
In [10]:
fig = go.Figure(data=[edge_trace, node_trace],
             layout=go.Layout(
                title='<br>Cryptocurrency network connections',
                titlefont=dict(size=16),
                showlegend=False,
                hovermode='closest',
                margin=dict(b=20,l=5,r=5,t=40),
                annotations=[ dict(
                    text=" ",
                    showarrow=False,
                    xref="paper", yref="paper") ],
                xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
                yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)))
fig.show()