from datetime import timedelta,date,datetimeimport pandas as pdimport requests, json, os, re, timeindex_list =['SH000016','SH000300','SH000905','SZ399006','SZ399005','SH000012',#宽指数#'SH000922','SH000827','SH000978','SH000922',#策略指数'SH000990','SH000991','SH000993','SZ399396','SH000986',#全指医药消费# 'SZ399967','SZ399975','SH000827',#主题指数:中证军工、证券'HKHSCEI','HKHSI', #恒生指数,恒生国企'QQQ','SP500','''XOP','GLD']def init_xueqiu():xueqiu_s = requests.session()user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 \(KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'xueqiu_s.headers.update({'User-Agent':user_agent})xueqiu_s.headers.update({'X-Requested-With':'XMLHttpRequest'})xueqiu_s.get('https://xueqiu.com/')return xueqiu_sdef get_hist(code, begin='', end='', days=0, timeout=5): xueqiu_s = init_xueqiu()#code: sh000300, begin/end: 20160901, days: 指定最近天数#http://xueqiu.com/S/SH000902/historical.csvif len(begin) > 0 and days > 0: #!!!不能同时指定begin和days!!!log.error('begin: %s, days: %d', begin, days)return Noneurl = 'https://xueqiu.com/stock/forchartk/stocklist.json?symbol=%s&period=1month&type=normal'%(code)if len(begin) > 0:url = '%s&begin=%d'%(url, int(time.mktime(datetime.strptime(begin,'%Y%m%d').timetuple())*1000))if len(end) > 0:url = '%s&end=%s'%(url, int(time.mktime(datetime.strptime(end,'%Y%m%d').timetuple())*1000))if days > 0:url = '%s&begin=%d'%(url, int(time.mktime((datetime.today() - timedelta(days)).timetuple())*1000))url = '%s&_=%d'%(url, int(time.time()*1000))print(url)r = xueqiu_s.get(url,timeout=timeout)org_fmt = "%a %b %d %H:%M:%S +0800 %Y"if r.ok is False:return Noneret_data = r.json()stock_data = ret_data['chartlist']data_list = []day_list = []# date open close high low volfor item in stock_data:try:date = datetime.strptime(item['time'], org_fmt)except:date = datetime.strptime(item['time'], "%a %b %d %H:%M:%S +0900 %Y")data_list.append(item['percent'])day_list.append(date)return pd.DataFrame(data=data_list,index=day_list, columns=[code])start_date = '20050101'index={}pd.set_option('precision',3)df = get_hist('SH000001', start_date)for item in index_list:index = get_hist(item, start_date)df = pd.concat([df,index],axis=1)df.corr()
https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000001&period=1month&type=normal&begin=1104508800000&_=1561648511235 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000016&period=1month&type=normal&begin=1104508800000&_=1561648511616 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000300&period=1month&type=normal&begin=1104508800000&_=1561648512016 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000905&period=1month&type=normal&begin=1104508800000&_=1561648512511 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SZ399006&period=1month&type=normal&begin=1104508800000&_=1561648512925 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SZ399005&period=1month&type=normal&begin=1104508800000&_=1561648513243 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000012&period=1month&type=normal&begin=1104508800000&_=1561648513639 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000990&period=1month&type=normal&begin=1104508800000&_=1561648514006 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000991&period=1month&type=normal&begin=1104508800000&_=1561648514386 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000993&period=1month&type=normal&begin=1104508800000&_=1561648514787 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SZ399396&period=1month&type=normal&begin=1104508800000&_=1561648515188 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000986&period=1month&type=normal&begin=1104508800000&_=1561648515502 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=HKHSCEI&period=1month&type=normal&begin=1104508800000&_=1561648515892 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=HKHSI&period=1month&type=normal&begin=1104508800000&_=1561648516452 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=QQQ&period=1month&type=normal&begin=1104508800000&_=1561648517096 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SP500&period=1month&type=normal&begin=1104508800000&_=1561648517603 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=XOP&period=1month&type=normal&begin=1104508800000&_=1561648518117 https://xueqiu.com/stock/forchartk/stocklist.json?symbol=GLD&period=1month&type=normal&begin=1104508800000&_=1561648518685
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
SH000001 | SH000016 | SH000300 | SH000905 | SZ399006 | SZ399005 | SH000012 | SH000990 | SH000991 | SH000993 | SZ399396 | SH000986 | HKHSCEI | HKHSI | QQQ | SP500 | XOP | GLD | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SH000001 | 1.000 | 0.959 | 0.974 | 0.850 | 0.727 | 0.822 | -0.237 | 0.835 | 0.758 | 0.774 | 0.763 | 0.900 | 0.725 | 0.633 | 0.395 | 0.432 | 0.272 | 0.163 |
SH000016 | 0.959 | 1.000 | 0.967 | 0.759 | 0.558 | 0.754 | -0.242 | 0.764 | 0.664 | 0.677 | 0.683 | 0.873 | 0.728 | 0.641 | 0.417 | 0.445 | 0.261 | 0.132 |
SH000300 | 0.974 | 0.967 | 1.000 | 0.884 | 0.701 | 0.864 | -0.226 | 0.855 | 0.796 | 0.807 | 0.772 | 0.902 | 0.682 | 0.607 | 0.385 | 0.420 | 0.269 | 0.172 |
SH000905 | 0.850 | 0.759 | 0.884 | 1.000 | 0.860 | 0.948 | -0.192 | 0.884 | 0.915 | 0.950 | 0.727 | 0.786 | 0.485 | 0.444 | 0.269 | 0.315 | 0.221 | 0.211 |
SZ399006 | 0.727 | 0.558 | 0.701 | 0.860 | 1.000 | 0.894 | -0.047 | 0.729 | 0.876 | 0.949 | 0.608 | 0.499 | 0.376 | 0.345 | 0.273 | 0.295 | 0.300 | -0.035 |
SZ399005 | 0.822 | 0.754 | 0.864 | 0.948 | 0.894 | 1.000 | -0.187 | 0.894 | 0.912 | 0.943 | 0.753 | 0.736 | 0.520 | 0.480 | 0.323 | 0.339 | 0.244 | 0.183 |
SH000012 | -0.237 | -0.242 | -0.226 | -0.192 | -0.047 | -0.187 | 1.000 | -0.197 | -0.151 | -0.154 | 0.031 | -0.258 | -0.194 | -0.220 | -0.212 | -0.206 | -0.273 | -0.038 |
SH000990 | 0.835 | 0.764 | 0.855 | 0.884 | 0.729 | 0.894 | -0.197 | 1.000 | 0.881 | 0.823 | 0.960 | 0.731 | 0.531 | 0.490 | 0.268 | 0.312 | 0.259 | 0.258 |
SH000991 | 0.758 | 0.664 | 0.796 | 0.915 | 0.876 | 0.912 | -0.151 | 0.881 | 1.000 | 0.896 | 0.750 | 0.666 | 0.442 | 0.424 | 0.241 | 0.291 | 0.242 | 0.201 |
SH000993 | 0.774 | 0.677 | 0.807 | 0.950 | 0.949 | 0.943 | -0.154 | 0.823 | 0.896 | 1.000 | 0.649 | 0.686 | 0.423 | 0.398 | 0.283 | 0.329 | 0.230 | 0.170 |
SZ399396 | 0.763 | 0.683 | 0.772 | 0.727 | 0.608 | 0.753 | 0.031 | 0.960 | 0.750 | 0.649 | 1.000 | 0.660 | 0.529 | 0.529 | 0.433 | 0.421 | 0.378 | 0.053 |
SH000986 | 0.900 | 0.873 | 0.902 | 0.786 | 0.499 | 0.736 | -0.258 | 0.731 | 0.666 | 0.686 | 0.660 | 1.000 | 0.656 | 0.585 | 0.404 | 0.438 | 0.311 | 0.239 |
HKHSCEI | 0.725 | 0.728 | 0.682 | 0.485 | 0.376 | 0.520 | -0.194 | 0.531 | 0.442 | 0.423 | 0.529 | 0.656 | 1.000 | 0.905 | 0.580 | 0.598 | 0.461 | 0.207 |
HKHSI | 0.633 | 0.641 | 0.607 | 0.444 | 0.345 | 0.480 | -0.220 | 0.490 | 0.424 | 0.398 | 0.529 | 0.585 | 0.905 | 1.000 | 0.676 | 0.679 | 0.544 | 0.244 |
QQQ | 0.395 | 0.417 | 0.385 | 0.269 | 0.273 | 0.323 | -0.212 | 0.268 | 0.241 | 0.283 | 0.433 | 0.404 | 0.580 | 0.676 | 1.000 | 0.894 | 0.537 | 0.008 |
SP500 | 0.432 | 0.445 | 0.420 | 0.315 | 0.295 | 0.339 | -0.206 | 0.312 | 0.291 | 0.329 | 0.421 | 0.438 | 0.598 | 0.679 | 0.894 | 1.000 | 0.612 | 0.038 |
XOP | 0.272 | 0.261 | 0.269 | 0.221 | 0.300 | 0.244 | -0.273 | 0.259 | 0.242 | 0.230 | 0.378 | 0.311 | 0.461 | 0.544 | 0.537 | 0.612 | 1.000 | 0.165 |
GLD | 0.163 | 0.132 | 0.172 | 0.211 | -0.035 | 0.183 | -0.038 | 0.258 | 0.201 | 0.170 | 0.053 | 0.239 | 0.207 | 0.244 | 0.008 | 0.038 | 0.165 | 1.000 |