Scott's Blog

学则不固, 知则不惑

0%

加密货币市值分析

自从08年比特币发布以来,数以百计类似的基于区块链技术的产品层出不穷。我们称这些为加密货币,时至今日,某些加密货币已经大幅上涨,某些在未来可能也极具上涨空间。实际上,在2017年12月6日,比特币的市值超过2000亿美元。

加密货币市值分析

席卷全球的加密货币

让我们来研究研究加密货币,第一个任务,我们将从coinmarketcap API加载当前数据,并将其显示在输出中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Importing pandas
import pandas as pd

# Importing matplotlib and setting aesthetics for plotting later.
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
plt.style.use('fivethirtyeight')

# Reading in current data from coinmarketcap.com
current = pd.read_json("https://api.coinmarketcap.com/v1/ticker/")

# Printing out the first few lines
print(current.head())

从api返回的结果只包含了100条数据,像这种开放的api基本上都是只提供试用,如果想要更多更全的数据一般需要付费,这里我们使用一份我从网站里下载的csv数据,包括了17年6月的数据,这个数据集保存在本地,叫做datasets/coinmarketcap_06122017.csv.

加载数据

1
2
3
4
5
6
7
8
# Reading datasets/coinmarketcap_06122017.csv into pandas
dec6 = pd.read_csv('datasets/coinmarketcap_06122017.csv')

# Selecting the 'id' and the 'market_cap_usd' columns
market_cap_raw = dec6[['id','market_cap_usd']]

# Counting the number of values
market_cap_raw.count()

输出:

1
2
3
id                1326
market_cap_usd 1031
dtype: int64

从输出种,我们看到这两个数据的条目数不一样,这是因为在列 market_cap_usd 种,存在一些空值(na值),而count函数是不将空值计算入内的,我们将它去掉。

1
2
3
4
5
# Filtering out rows without a market capitalization
cap = market_cap_raw.query('market_cap_usd > 0')

# Counting the number of values again
cap.count()

比较比特币与其他加密货币

现在比特币正处于与其他加密货币的激烈竞争中,但它仍然在市值中占主导地位,我们来比较一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#Declaring these now for later use in the plots
TOP_CAP_TITLE = 'Top 10 market capitalization'
TOP_CAP_YLABEL = '% of total cap'

# Selecting the first 10 rows and setting the index
cap10 = cap.head(10).set_index('id')

# Calculating market_cap_perc
cap10 = cap10.assign(market_cap_perc =
lambda x: (x.market_cap_usd / cap.market_cap_usd.sum()) * 100)

# Plotting the barplot with the title defined above
ax = cap10.market_cap_perc.plot.bar(title='Top 10 market capitalization')

# Annotating the y axis with the label defined above
ax.set_ylabel('% of total cap')

输出:

可以看到比特币在整个加密货币中所占的份额多大,但是这个图还存在一个信息,可以对其进行改进,那就是比特币所占份额太大了,其他的加密货币之间就很难区分。

可视化优化

解决上面的办法是我们需要更改坐标轴的单位,我们从百分比换成 log^10,如果你不知道如何优化你的数据可视化步骤,可以参考我的另一篇文章

另外,我们把类似的加密货币合并成同一个颜色,把x轴上的信息隐藏

1
2
3
4
5
6
7
8
9
10
11
# Colors for the bar plot
COLORS = ['orange', 'green', 'orange', 'cyan', 'cyan', 'blue', 'silver', 'orange', 'red', 'green']

# Plotting market_cap_usd as before but adding the colors and scaling the y-axis
ax = cap10.market_cap_perc.plot.bar(title='Top 10 market capitalization',color=COLORS)

# Annotating the y axis with 'USD'
ax.set_ylabel('USD')

# Final touch! Removing the xlabel as it is not very informative
ax.set_xlabel('')

隐藏X轴上的字的另一种方式:

1
2
x_axis = ax.xaxis
x_axis.label.set_visible(False)

恐怖的波动性

加密货币发布以来就以其惊人的波动性著称,让我们来探索一下这种波动性,我们会选择24小时和一周的时间短来统计波动性。

1
2
3
4
5
6
7
8
9
10
11
# Selecting the id, percent_change_24h and percent_change_7d columns
volatility = dec6[['id','percent_change_24h','percent_change_7d']]

# Setting the index to 'id' and dropping all NaN rows
volatility = volatility.set_index('id').dropna()

# Sorting the DataFrame by percent_change_24h in ascending order
volatility = volatility.sort_values(by='percent_change_24h')

# Checking the first few rows
print(volatility.head())

输出:

1
2
3
4
5
6
7
               percent_change_24h  percent_change_7d
id
flappycoin -95.85 -96.61
credence-coin -94.22 -95.31
coupecoin -93.93 -61.24
tyrocoin -79.02 -87.43
petrodollar -76.55 542.96

第一名flappycoin,24小时内的波动为95%,这意味着1万块钱的投入24小时后只剩500.

的确,波动性如此之大的产品亏损起来是很快的,但高风险也意味着高收益,我们来看一下24小时内最大的赢家与输家的对比:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#Defining a function with 2 parameters, the series to plot and the title
def top10_subplot(volatility_series, title):
# Making the subplot and the figure for two side by side plots
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 6))

# Plotting with pandas the barchart for the top 10 losers
ax = volatility_series[:10].plot.bar(color='darkred',ax=axes[0])

# Setting the figure's main title to the text passed as parameter
fig.suptitle(title)

# Setting the ylabel to '% change'
ax.set_ylabel('% change')

# Same as above, but for the top 10 winners
ax = volatility_series[-10:].plot.bar(color='darkblue',ax=axes[1])

# Returning this for good practice, might use later
return fig, ax

DTITLE = "24 hours top losers and winners"

# Calling the function above with the 24 hours period series and title DTITLE
fig, ax = top10_subplot(volatility.percent_change_24h,'24 hours top losers and winners')

输出:

24小时百分之800的收益?为什么你还在看这篇文章不赶快去买比特币!(开玩笑😝)

我们再来看看周收益吧:

1
2
3
4
5
6
7
# Sorting in ascending order
volatility7d = volatility.sort_values(by='percent_change_7d')

WTITLE = "Weekly top losers and winners"

# Calling the top10_subplot function
fig, ax = top10_subplot(volatility7d.percent_change_7d,WTITLE)

3500的收益,好了我不说什么了。

关注低市值加密货币

上面的这些加密货币感觉都没见过,事实上这些加密货币的市值都很小,而投资低市值的加密货币风险也是很高的,这也印证了之前说的高风险高收益。

对于市值要怎么划分呢?多少可以划分成高市值,多少又是低市值呢?我们一般按下表来:

1
2
3
4
5
Large cap: +10 billion
Mid cap: 2 billion - 10 billion
Small cap: 300 million - 2 billion
Micro cap: 50 million - 300 million
Nano cap: Below 50 million

我们来看一下大于一百亿市值的:

1
2
3
4
5
# Selecting everything bigger than 10 billion 
largecaps = cap.query('market_cap_usd>10000000000')

# Printing out largecaps
print(largecaps)
1
2
3
4
5
             id  market_cap_usd
0 bitcoin 2.130493e+11
1 ethereum 4.352945e+10
2 bitcoin-cash 2.529585e+10
3 iota 1.475225e+10

大多数加密货币市值很低

通过将加密货币按照市值分类,进而对其按区间统计个数,我们可以看到🧍‍♂️市值区间加密货币的个数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Making a nice function for counting different marketcaps from the
# "cap" DataFrame. Returns an int.
# INSTRUCTORS NOTE: Since you made it to the end, consider it a gift :D
def capcount(query_string):
return cap.query(query_string).count().id

# Labels for the plot
LABELS = ["biggish", "micro", "nano"]

# Using capcount count the biggish cryptos
biggish = capcount('market_cap_usd>3000000000')

# Same as above for micro ...
micro = capcount('market_cap_usd>50000000 and market_cap_usd<300000000')

# ... and for nano
nano = capcount('market_cap_usd<50000000')

# Making a list with the 3 counts
values = [biggish,micro,nano]

# Plotting them with matplotlib
plt.bar(range(len(values)),values,label=LABELS)

可以发现,大部分的加密货币市值都很小。