使用Tushare获取多只股票数据

tushare ID:393042

Tushare 简介

Tushare 是一个免费、开源的 python 财经数据接口包。Tushare 返回的绝大部分的数据格式都是pandas DataFrame 类型,非常便于用 pandas / NumPy / Matplotlib 进行数据分析和可视化。 

Tushare 包的安装

在 Anaconda prompt 中通过:

pip install tushare

命令安装,python的大多数第三包都可以通过这种方式安装。安装完成后,可以通过 conda list  检查是否按照成功。此外,也可以在 Jupyter  Notebook 中通过 import 导入检查。需要更新时,可以在 Anaconda prompt 中通过以下命令完成:

pip install tushare --upgrade

Tushare Pro

一般的 Tushare 抓取的数据有一定时限,要想抓取长时间的数据,需要注册 Tushare 会员,获取积分,如何获取Tushare积分。

第一次使用 ts.pro_api 需要设置TOKEN凭证, 如何获取TOKEN凭证 。

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import tushare as ts

token='********************************************************'
ts.set_token(token)
pro=ts.pro_api()

SSEC_pro = pro.index_daily(ts_code='000001.SH', start_date='20180101', end_date='20210915')
SSEC_pro.tail()
ts_code	trade_date	close	open	high	low	pre_close	change	pct_chg	vol	amount
898	000001.SH	20180108	3409.4795	3391.5528	3412.7307	3384.5591	3391.7501	17.7294	0.5227	236165106.0	286213219.1
899	000001.SH	20180105	3391.7501	3386.4640	3402.0694	3380.2450	3385.7102	6.0399	0.1784	213060681.0	248187840.5
900	000001.SH	20180104	3385.7102	3371.0000	3392.8264	3365.2954	3369.1084	16.6018	0.4928	206955288.0	243090768.7
901	000001.SH	20180103	3369.1084	3347.7428	3379.9152	3345.2887	3348.3259	20.7825	0.6207	213836149.0	258366523.2
902	000001.SH	20180102	3348.3259	3314.0307	3349.0530	3314.0307	3307.1721	41.1538	1.2444	202278860.0	227788461.1

SSEC_pro.describe()
close	open	high	low	pre_close	change	pct_chg	vol	amount
count	903.000000	903.000000	903.000000	903.000000	903.000000	903.000000	903.000000	9.030000e+02	9.030000e+02
mean	3097.503049	3094.822599	3115.893445	3074.233051	3097.116514	0.387218	0.018157	2.426987e+08	2.855748e+08
std	309.423173	309.152421	309.868581	307.585213	308.942065	36.162055	1.183221	1.074083e+08	1.442081e+08
min	2464.362800	2446.019300	2488.479000	2440.906600	2464.362800	-229.922500	-7.724500	8.823889e+07	8.540412e+07
25%	2874.505700	2871.951900	2888.813600	2851.706500	2874.505700	-17.994000	-0.580400	1.545743e+08	1.684175e+08
50%	3050.124000	3041.922000	3074.257100	3029.463200	3050.124000	0.974800	0.031300	2.185987e+08	2.517639e+08
75%	3379.144300	3380.980800	3407.084750	3358.585500	3378.381450	20.332900	0.632550	3.093207e+08	3.728400e+08
max	3715.372300	3721.086500	3731.687000	3692.815700	3715.372300	180.068100	5.711300	6.672763e+08	8.425290e+08

获取多只股票数据

利用 for 循环从股票代码里抓取多只股票的收盘价: 

S50 = pro.index_weight(index_code='000016.SH', start_date='20210730')
S50
index_code	con_code	trade_date	weight
0	000016.SH	603986.SH	20210730	1.675
1	000016.SH	603501.SH	20210730	2.156
2	000016.SH	603288.SH	20210730	1.985
3	000016.SH	603259.SH	20210730	3.685
4	000016.SH	601995.SH	20210730	0.186
5	000016.SH	601899.SH	20210730	1.940
6	000016.SH	601888.SH	20210730	3.223
7	000016.SH	601857.SH	20210730	0.623
8	000016.SH	601818.SH	20210730	0.748
9	000016.SH	601688.SH	20210730	1.019
10	000016.SH	601668.SH	20210730	1.282
11	000016.SH	601628.SH	20210730	0.639
12	000016.SH	601601.SH	20210730	1.220
13	000016.SH	601398.SH	20210730	2.204
14	000016.SH	601336.SH	20210730	0.463
15	000016.SH	601318.SH	20210730	7.965
16	000016.SH	601288.SH	20210730	1.153
17	000016.SH	601211.SH	20210730	1.003
18	000016.SH	601166.SH	20210730	3.520
19	000016.SH	601138.SH	20210730	0.458
20	000016.SH	601088.SH	20210730	0.768
21	000016.SH	601066.SH	20210730	0.352
22	000016.SH	601012.SH	20210730	5.097
23	000016.SH	600918.SH	20210730	0.096
24	000016.SH	600893.SH	20210730	0.801
25	000016.SH	600887.SH	20210730	2.783
26	000016.SH	600837.SH	20210730	1.460
27	000016.SH	600809.SH	20210730	1.912
28	000016.SH	600745.SH	20210730	0.932
29	000016.SH	600703.SH	20210730	1.568
30	000016.SH	600690.SH	20210730	1.295
31	000016.SH	600588.SH	20210730	0.802
32	000016.SH	600585.SH	20210730	1.164
33	000016.SH	600570.SH	20210730	0.892
34	000016.SH	600547.SH	20210730	0.435
35	000016.SH	600519.SH	20210730	14.448
36	000016.SH	600438.SH	20210730	1.599
37	000016.SH	600309.SH	20210730	2.446
38	000016.SH	600276.SH	20210730	3.336
39	000016.SH	600196.SH	20210730	1.354
40	000016.SH	600104.SH	20210730	0.881
41	000016.SH	600050.SH	20210730	1.050
42	000016.SH	600048.SH	20210730	0.985
43	000016.SH	600036.SH	20210730	7.885
44	000016.SH	600031.SH	20210730	2.020
45	000016.SH	600030.SH	20210730	2.636
46	000016.SH	600028.SH	20210730	0.731
47	000016.SH	600016.SH	20210730	1.160
48	000016.SH	600009.SH	20210730	0.509
49	000016.SH	600000.SH	20210730	1.453

S50_stock_list = S50.con_code
S50_stock_list.values
array(['603986.SH', '603501.SH', '603288.SH', '603259.SH', '601995.SH',
       '601899.SH', '601888.SH', '601857.SH', '601818.SH', '601688.SH',
       '601668.SH', '601628.SH', '601601.SH', '601398.SH', '601336.SH',
       '601318.SH', '601288.SH', '601211.SH', '601166.SH', '601138.SH',
       '601088.SH', '601066.SH', '601012.SH', '600918.SH', '600893.SH',
       '600887.SH', '600837.SH', '600809.SH', '600745.SH', '600703.SH',
       '600690.SH', '600588.SH', '600585.SH', '600570.SH', '600547.SH',
       '600519.SH', '600438.SH', '600309.SH', '600276.SH', '600196.SH',
       '600104.SH', '600050.SH', '600048.SH', '600036.SH', '600031.SH',
       '600030.SH', '600028.SH', '600016.SH', '600009.SH', '600000.SH'],
      dtype=object)

S50_data = pd.DataFrame()
for stock in S50_stock_list:
    data = pro.daily(ts_code=stock, start_date='20190101', end_date='20210915').set_index(['trade_date'])
    S50_data[stock] = data['close']
S50_data.head()
603986.SH	603501.SH	603288.SH	603259.SH	601995.SH	601899.SH	601888.SH	601857.SH	601818.SH	601688.SH	...	600104.SH	600050.SH	600048.SH	600036.SH	600031.SH	600030.SH	600028.SH	600016.SH	600009.SH	600000.SH
trade_date																					
20210915	151.55	231.99	86.32	141.73	56.76	11.52	234.97	5.99	3.45	18.09	...	20.21	4.22	12.62	51.40	26.58	26.24	4.73	4.02	42.85	9.19
20210914	154.64	234.90	88.18	141.96	57.71	11.72	246.85	5.71	3.48	18.56	...	20.43	4.23	12.99	52.10	27.01	26.48	4.65	4.03	43.32	9.21
20210913	155.30	237.35	88.96	136.32	60.05	12.39	251.25	5.50	3.56	19.15	...	20.67	4.31	13.15	53.92	27.52	27.19	4.70	4.12	44.45	9.39
20210910	159.90	243.13	90.77	140.80	58.01	11.97	255.00	5.19	3.56	18.98	...	21.00	4.32	12.46	54.12	28.31	27.27	4.55	4.16	45.78	9.41
20210909	154.25	245.81	90.00	140.78	57.30	11.43	245.07	5.27	3.54	18.34	...	20.96	4.39	12.08	52.10	28.29	26.73	4.65	4.15	45.09	9.30
5 rows × 50 columns

 

你可能感兴趣的:(python)