# 币种的涨跌与比特币的相关性关系

Author: 小草, Created: 2023-11-16 16:53:56, Updated: 2023-11-17 21:36:53

### 相关性的公式和计算方法

$r = \frac{\sum_{i=1}^{n} (X_i - \bar{X})(Y_i - \bar{Y})}{\sqrt{\sum_{i=1}^{n} (X_i - \bar{X})^2} \sqrt{\sum_{i=1}^{n} (Y_i - \bar{Y})^2}}$

### 数据收集

import requests
from datetime import date,datetime
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

ticker = requests.get('https://fapi.binance.com/fapi/v1/ticker/24hr')
ticker = ticker.json()
sort_symbols = [k['symbol'][:-4] for k in sorted(ticker, key=lambda x :-float(x['quoteVolume'])) if k['symbol'][-4:] == 'USDT']

def GetKlines(symbol='BTCUSDT',start='2020-8-10',end='2023-8-10',period='1h',base='fapi',v = 'v1'):
Klines = []
start_time = int(time.mktime(datetime.strptime(start, "%Y-%m-%d").timetuple()))*1000 + 8*60*60*1000
end_time =  min(int(time.mktime(datetime.strptime(end, "%Y-%m-%d").timetuple()))*1000 + 8*60*60*1000,time.time()*1000)
intervel_map = {'m':60*1000,'h':60*60*1000,'d':24*60*60*1000}
while start_time < end_time:
time.sleep(0.5)
mid_time = start_time+1000*int(period[:-1])*intervel_map[period[-1]]
url = 'https://'+base+'.binance.com/'+base+'/'+v+'/klines?symbol=%s&interval=%s&startTime=%s&endTime=%s&limit=1000'%(symbol,period,start_time,mid_time)
res = requests.get(url)
res_list = res.json()
if type(res_list) == list and len(res_list) > 0:
start_time = res_list[-1][0]+int(period[:-1])*intervel_map[period[-1]]
Klines += res_list
if type(res_list) == list and len(res_list) == 0:
start_time = start_time+1000*int(period[:-1])*intervel_map[period[-1]]
if mid_time >= end_time:
break
df.index = pd.to_datetime(df.time,unit='ms')
return df

start_date = '2023-01-01'
end_date   = '2023-11-16'
period = '4h'
df_dict = {}

for symbol in sort_symbols:
print(symbol)
df_s = GetKlines(symbol=symbol+'USDT',start=start_date,end=end_date,period=period)
if not df_s.empty:
df_dict[symbol] = df_s

df_close = pd.DataFrame(index=pd.date_range(start=start_date, end=end_date, freq=period),columns=df_dict.keys())
for symbol in symbols:
df_s = df_dict[symbol]
df_close[symbol] = df_s.close
df_close = df_close.dropna(how='any',axis=1)


### 行情回顾

df_norm = df_close/df_close.fillna(method='bfill').iloc[0] #归一化
total_index = df_norm.mean(axis=1)
total_index.plot(figsize=(15,6),grid=True);


### 相关性分析

pandas自带相关性计算，和BTC价格相关性和最弱的如下图，大部分币种的相关性为正，意味着它们跟随BTC的价格，而还有部分币种相关性为负，这在数字货币行情中，算是一种异常。

corr_symbols = df_norm.corrwith(df_norm.BTC).sort_values().index


### 相关性与价格涨幅

(df_norm[corr_symbols[-40:]].mean(axis=1)-df_norm[corr_symbols[:40]].mean(axis=1)).plot(figsize=(15,6),grid=True);


corr_symbols = (df_norm.iloc[:1500].corrwith(df_norm.BTC.iloc[:1500])-df_norm.iloc[:1500].corrwith(total_index[:1500])).sort_values().index


### 总结

More

mztcoin 好，相关性分析可以与之前的做空超涨做多超跌策略结合一下