Python数据分析入门:比特币价格涨幅趋势分布

Posted 五包辣条!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python数据分析入门:比特币价格涨幅趋势分布相关的知识,希望对你有一定的参考价值。

大家好,我是辣条。

曾经有一个真挚的机会,摆在我面前,但是我没有珍惜,等到失去的时候才后悔莫及,尘世间最痛苦的事莫过于此,如果老天可以再给我一个再来一次机会的话,我会买下那个比特币,哪怕付出所有零花钱,如果非要在这个机会加上一个期限的话,我希望是十年前。

看着这份台词是不是很眼熟,我稍稍改了一下,曾经差一点点点就购买比特币了,肠子都悔青了现在,今天对比特币做一个简单的数据分析。

# 安装对应的第三方库
!pip install pandas  
!pip install numpy
!pip install seaborn
!pip install matplotlib
!pip install sklearn
!pip install tensorflow

使用技术点:

1. 数据处理 - pandas
2. 科学运算 - numpy
3. 数据可视化 - seaborn matplotlib

使用工具:

1. anaconda
2. notebook
3. python3.7版本

导入第三方库

#a|T + enter  notebook运行方式
import pandas as pd # 数据处理
import numpy as np # 科学运算
import seaborn as sns # 数据可视化
import matplotlib.pyplot as plt # 数据可视化
​
import warnings
import warnings
warnings.filterwarnings('ignore')

如遇到导包报错 可以看看是不是自己的第三方库的版本问题

# 设置图表与 线格式
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['lines.linewidth'] = 2
plt.style.use('ggplot')
# 读取数据集
df = pd.read_csv('./DOGE-USD.csv')
df.head() # 查看前5行
DateOpenHighLowCloseAdj CloseVolume
02014-09-170.0002930.0002990.0002600.0002680.0002681463600.0
12014-09-180.0002680.0003250.0002670.0002980.0002982215910.0
22014-09-190.0002980.0003070.0002750.0002770.000277883563.0
32014-09-200.0002760.0003100.0002670.0002920.000292993004.0
42014-09-210.0002930.0002990.0002840.0002880.000288539140.0
df.isnull().sum() # 统计缺失值的总和(sum())
Date         0
Open         5
High         5
Low          5
Close        5
Adj Close    5
Volume       5
dtype: int64
df.duplicated().sum() # 查看重复值
0

# 数据类型 分布基本情况
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2591 entries, 0 to 2590
Data columns (total 7 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   Date       2591 non-null   object 
 1   Open       2586 non-null   float64
 2   High       2586 non-null   float64
 3   Low        2586 non-null   float64
 4   Close      2586 non-null   float64
 5   Adj Close  2586 non-null   float64
 6   Volume     2586 non-null   float64
dtypes: float64(6), object(1)
memory usage: 141.8+ KB
# 转换 Date的类型
df['Date'] = pd.to_datetime(df.Date, dayfirst=True)
# 索引重置 让Date时间格式成为 索引  inplace新建对象
df.set_index('Date', inplace=True)
df
OpenHighLowCloseAdj CloseVolume
Date
2014-09-170.0002930.0002990.0002600.0002680.0002681.463600e+06
2014-09-180.0002680.0003250.0002670.0002980.0002982.215910e+06
2014-09-190.0002980.0003070.0002750.0002770.0002778.835630e+05
2014-09-200.0002760.0003100.0002670.0002920.0002929.930040e+05
2014-09-210.0002930.0002990.0002840.0002880.0002885.391400e+05
.....................
2021-10-160.2338810.2444470.2336830.2372920.2372921.541851e+09
2021-10-170.2371930.2419730.2263800.2378980.2378981.397143e+09
2021-10-180.2378060.2713940.2374880.2472810.2472815.003366e+09
2021-10-19NaNNaNNaNNaNNaNNaN
2021-10-200.2451990.2468380.2423840.2460780.2460781.187871e+09

2591 rows × 6 columns

df = df.asfreq('d') # 按照天数采集数据
df = df.fillna(method='bfill') # 缺失值填充 下一条数据填充
df
OpenHighLowCloseAdj CloseVolume
Date
2014-09-170.0002930.0002990.0002600.0002680.0002681.463600e+06
2014-09-180.0002680.0003250.0002670.0002980.0002982.215910e+06
2014-09-190.0002980.0003070.0002750.0002770.0002778.835630e+05
2014-09-200.0002760.0003100.0002670.0002920.0002929.930040e+05
2014-09-210.0002930.0002990.0002840.0002880.0002885.391400e+05
.....................
2021-10-160.2338810.2444470.2336830.2372920.2372921.541851e+09
2021-10-170.2371930.2419730.2263800.2378980.2378981.397143e+09
2021-10-180.2378060.2713940.2374880.2472810.2472815.003366e+09
2021-10-190.2451990.2468380.2423840.2460780.2460781.187871e+09
2021-10-200.2451990.2468380.2423840.2460780.2460781.187871e+09

2591 rows × 6 columns

In [14]:

# 开盘价的分布情况
df['Open'].plot(figsize=(12, 8))

结论:从上图可以看出 BTB是在2021年份开始爆发式的增长 在2015 到 2021 一直都是没有较大波动
# 成交情况
df['Volume'].plot(figsize=(12, 8))

# 投资价值
df['Total Pos'] = df.sum(axis=1)
df['Total Pos'].plot(figsize=(10, 8))

结论:开盘价高 投资价值搞 比较合适做卖出操作 实现一夜暴富(开玩笑的)
# 当前元素与先前元素的相差百分比
df['Daily Reture'] = df['Total Pos'].pct_change(1)
# 日收益率的平均
df['Daily Reture'].mean()
df['Daily Reture'].plot(kind='kde')

SR = df['Daily Reture'].mean() / df['Daily Reture'].std()
all_plot = df/df.iloc[0]
all_plot.plot(figsize=(24, 16))

df.hist(bins=100, figsize=(12, 6))


# 按照年份进行采样
df.resample(rule='A').mean()
 
OpenHighLowCloseAdj CloseVolumeTotal PosDaily Reture
Date
2014-12-310.0002490.0002590.0002400.0002480.0002488.059213e+058.059213e+051.028630
2015-12-310.0001430.0001470.0001390.0001430.0001431.685476e+051.685476e+050.139461
2016-12-310.0002350.0002420.0002290.0002350.0002352.564834e+052.564834e+050.259038
2017-12-310.0015760.0017080.0014680.0016010.0016011.118996e+071.118996e+070.225833
2018-12-310.0043680.0045770.0041250.0043500.0043502.172325e+072.172325e+070.109586
2019-12-310.0025640.0026310.0024990.0025630.0025634.463969e+074.463969e+070.027981
2020-12-310.0027360.0028220.0026600.0027440.0027441.290465e+081.290465e+080.052314
2021-12-310.2004100.2157750.1857700.2012720.2012724.620961e+094.620961e+090.260782
# 年平均收盘价
df['Open'].resample('A').mean().plot.bar(title='Yearly Mean Closing Price', color=['#b41f7d'])

# 月度
df['Open'].resample('M').mean().plot.bar(figsize=(18, 12), color='red')


# 分别获取对应时间窗口  6 12 2 均值
df['6-month-SMA'] = df['Open'].rolling(window=6).mean()
df['12-month-SMA'] = df['Open'].rolling(window=12).mean()
df['2-month-SMA'] = df['Open'].rolling(window=2).mean()
df.head(10)
 
OpenHighLowCloseAdj CloseVolumeTotal PosDaily Reture6-month-SMA12-month-SMA2-month-SMA
Date
2014-09-170.0002930.0002990.0002600.0002680.0002681463600.01.463600e+06NaNNaNNaNNaN
2014-09-180.0002680.0003250.0002670.0002980.0002982215910.02.215910e+060.514013NaNNaN0.000281
2014-09-190.0002980.0003070.0002750.0002770.000277883563.08.835630e+05-0.601264NaNNaN0.000283
2014-09-200.0002760.0003100.0002670.0002920.000292993004.09.930040e+050.123863NaNNaN0.000287
2014-09-210.0002930.0002990.0002840.0002880.000288539140.05.391400e+05-0.457062NaNNaN0.000285
2014-09-220.0002880.0003010.0002850.0002980.000298620222.06.202220e+050.1503910.000286NaN0.000291
2014-09-230.0002980.0003180.0002950.0003130.000313739197.07.391970e+050.1918260.000287NaN0.000293
2014-09-240.0003140.0003530.0003100.0003480.0003481277840.01.277840e+060.7286870.000295NaN0.000306
2014-09-250.0003470.0003830.0003320.0003750.0003752393610.02.393610e+060.8731690.000303NaN0.000331
2014-09-260.0003740.0004670.0003730.0004510.0004514722610.04.722610e+060.9730070.000319NaN0.000361

进行可视化 查看对应分布情况

df[['Open', '6-month-SMA', '12-month-SMA', '2-month-SMA']].plot(figsize=(24, 10))

df[["Open","6-month-SMA"]].plot(figsize=(18,10))

df[['Open','6-month-SMA']].iloc[:100].plot(figsize=(12,6)).autoscale(axis='x',tight=True)

df['EWMA12'] = df['Open'].ewm(span=14,adjust=True).mean()
df[['Open','EWMA12']].plot(figsize=(24,12))


df[['Open','EWMA12']].iloc[:50].plot(figsize=(12,6)).autoscale(axis='x',tight=True)
 

  👇🏻 疑难解答、学习资料、路线图可通过搜索下方 👇🏻

以上是关于Python数据分析入门:比特币价格涨幅趋势分布的主要内容,如果未能解决你的问题,请参考以下文章

比特币价格回升至52000美元上方

比特币涨幅创八年来一季度最好表现,四月能否走出历史行情?

比特币“挖矿”潮蔓延:“矿工”和游戏玩家抢显卡致价格疯涨 概念股或受益

比特币行情加密货币价格的剧烈波动,导致了爆仓金额的激增

比特币盘中暴跌9%,美股道指“V型”反弹!这一夜发生了什么?

以太坊的市值会超越比特币吗?