请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  数理科学 帖子:3128192 新帖:249

指数相关性分析相关代码

我就是英雄发表于:6 月 28 日 00:00回复(1)


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;    }


SH000001SH000016SH000300SH000905SZ399006SZ399005SH000012SH000990SH000991SH000993SZ399396SH000986HKHSCEIHKHSIQQQSP500XOPGLD
SH0000011.0000.9590.9740.8500.7270.822-0.2370.8350.7580.7740.7630.9000.7250.6330.3950.4320.2720.163
SH0000160.9591.0000.9670.7590.5580.754-0.2420.7640.6640.6770.6830.8730.7280.6410.4170.4450.2610.132
SH0003000.9740.9671.0000.8840.7010.864-0.2260.8550.7960.8070.7720.9020.6820.6070.3850.4200.2690.172
SH0009050.8500.7590.8841.0000.8600.948-0.1920.8840.9150.9500.7270.7860.4850.4440.2690.3150.2210.211
SZ3990060.7270.5580.7010.8601.0000.894-0.0470.7290.8760.9490.6080.4990.3760.3450.2730.2950.300-0.035
SZ3990050.8220.7540.8640.9480.8941.000-0.1870.8940.9120.9430.7530.7360.5200.4800.3230.3390.2440.183
SH000012-0.237-0.242-0.226-0.192-0.047-0.1871.000-0.197-0.151-0.1540.031-0.258-0.194-0.220-0.212-0.206-0.273-0.038
SH0009900.8350.7640.8550.8840.7290.894-0.1971.0000.8810.8230.9600.7310.5310.4900.2680.3120.2590.258
SH0009910.7580.6640.7960.9150.8760.912-0.1510.8811.0000.8960.7500.6660.4420.4240.2410.2910.2420.201
SH0009930.7740.6770.8070.9500.9490.943-0.1540.8230.8961.0000.6490.6860.4230.3980.2830.3290.2300.170
SZ3993960.7630.6830.7720.7270.6080.7530.0310.9600.7500.6491.0000.6600.5290.5290.4330.4210.3780.053
SH0009860.9000.8730.9020.7860.4990.736-0.2580.7310.6660.6860.6601.0000.6560.5850.4040.4380.3110.239
HKHSCEI0.7250.7280.6820.4850.3760.520-0.1940.5310.4420.4230.5290.6561.0000.9050.5800.5980.4610.207
HKHSI0.6330.6410.6070.4440.3450.480-0.2200.4900.4240.3980.5290.5850.9051.0000.6760.6790.5440.244
QQQ0.3950.4170.3850.2690.2730.323-0.2120.2680.2410.2830.4330.4040.5800.6761.0000.8940.5370.008
SP5000.4320.4450.4200.3150.2950.339-0.2060.3120.2910.3290.4210.4380.5980.6790.8941.0000.6120.038
XOP0.2720.2610.2690.2210.3000.244-0.2730.2590.2420.2300.3780.3110.4610.5440.5370.6121.0000.165
GLD0.1630.1320.1720.211-0.0350.183-0.0380.2580.2010.1700.0530.2390.2070.2440.0080.0380.1651.000
 

全部回复

0/140

达人推荐

量化课程

    移动端课程