Quick Start

Example 1. 获取个股历史交易数据(包括均线数据)


In [1]:
import tushare as ts

ts.get_hist_data('600848') # 一次性获取全部数据


Out[1]:
open high close low volume price_change p_change ma5 ma10 ma20 v_ma5 v_ma10 v_ma20 turnover
date
2016-09-08 24.80 24.80 24.25 23.81 136169.94 -0.24 -0.98 23.470 22.451 22.263 168695.44 125246.74 115658.48 4.66
2016-09-07 23.20 24.94 24.48 23.00 228627.59 1.49 6.48 23.192 22.201 22.093 193177.05 116716.33 112294.72 7.83
2016-09-06 22.74 24.07 22.99 22.74 136205.03 0.23 1.01 22.450 21.956 21.919 151935.44 101285.71 104605.53 4.66
2016-09-05 22.57 23.48 22.78 22.57 108498.10 -0.07 -0.31 21.992 21.811 21.855 133160.42 93292.61 103590.20 3.71
2016-09-02 22.33 23.80 22.85 22.33 233976.56 0.00 0.00 21.654 21.752 21.773 121822.20 88941.81 102333.38 8.01
2016-09-01 20.85 22.86 22.86 20.75 258577.98 2.08 10.01 21.432 21.679 21.713 81798.03 75551.31 97176.78 8.85
2016-08-31 20.92 20.94 20.77 20.65 22419.51 0.07 0.34 21.210 21.653 21.665 40255.61 65380.00 90520.76 0.77
2016-08-30 21.12 21.30 20.70 20.61 42329.96 -0.39 -1.85 21.462 21.816 21.735 50635.99 76858.93 98996.66 1.45
2016-08-29 21.87 21.87 21.09 21.00 51807.00 -0.67 -3.08 21.630 22.025 21.740 53424.79 91677.43 105053.36 1.77
2016-08-26 21.79 21.95 21.74 21.61 33855.69 -0.02 -0.09 21.850 22.115 21.708 56061.41 101178.10 107785.28 1.16
2016-08-25 21.80 22.03 21.75 21.53 50865.89 -0.26 -1.18 21.926 22.075 21.658 69304.58 106070.22 112279.59 1.74
2016-08-24 21.88 22.38 22.03 21.75 74321.39 0.49 2.27 22.096 21.985 21.627 90504.39 107873.10 119053.29 2.54
2016-08-23 22.41 22.41 21.54 21.38 56274.00 -0.66 -2.97 22.170 21.882 21.659 103081.87 107925.36 130838.66 1.93
2016-08-22 22.08 22.40 22.19 21.80 64990.10 0.00 0.00 22.420 21.898 21.663 129930.07 113887.79 137326.92 2.22
2016-08-19 22.60 22.60 22.12 21.99 100071.53 -0.57 -2.51 22.380 21.793 21.653 146294.79 115724.95 144255.63 3.43
2016-08-18 22.41 23.04 22.60 22.22 156864.94 0.23 1.03 22.224 21.747 21.545 142835.86 118802.26 146158.88 5.37
2016-08-17 22.51 22.78 22.40 21.83 137208.80 -0.38 -1.67 21.874 21.677 21.370 125241.81 115661.52 146652.25 4.70
2016-08-16 21.81 23.00 22.79 21.81 190514.98 0.80 3.64 21.594 21.653 21.173 112768.84 121134.38 142950.65 6.52
2016-08-15 21.60 22.38 21.99 21.18 146813.69 0.65 3.05 21.376 21.454 20.934 97845.51 118429.30 135559.48 5.03
2016-08-12 20.77 21.60 21.34 20.71 82776.91 0.53 2.55 21.206 21.301 20.750 85155.12 114392.45 131203.14 2.83
2016-08-11 21.09 21.25 20.85 20.77 68894.68 -0.16 -0.76 21.270 21.240 20.599 94768.66 118488.95 132369.09 2.36
2016-08-10 21.44 21.50 21.00 20.98 74843.92 -0.68 -3.14 21.480 21.269 20.440 106081.23 130233.48 131321.54 2.56
2016-08-09 21.14 21.70 21.70 20.80 115898.33 0.54 2.55 21.712 21.436 20.271 129499.93 153751.96 130282.63 3.97
2016-08-08 21.41 21.55 21.14 20.80 83361.75 -0.52 -2.40 21.532 21.428 20.065 139013.09 160766.04 128541.96 2.85
2016-08-05 22.15 22.70 21.66 21.55 130844.60 -0.26 -1.19 21.396 21.512 19.905 143629.78 172786.32 129085.94 4.48
2016-08-04 21.79 22.00 21.90 21.31 125457.57 -0.23 -1.04 21.210 21.342 19.745 142209.25 173515.50 132014.14 4.29
2016-08-03 21.00 22.45 22.16 20.51 191937.39 1.39 6.69 21.058 21.062 19.583 154385.73 177642.97 128179.09 6.57
2016-08-02 20.55 21.12 20.80 20.17 163464.12 0.38 1.86 21.160 20.693 19.323 178003.99 164766.92 118640.13 5.60
2016-08-01 20.37 21.22 20.46 20.03 106445.22 -0.23 -1.11 21.324 20.413 19.054 182519.00 152689.66 111697.72 3.64
2016-07-29 20.92 21.74 20.73 20.52 123741.96 -0.39 -1.85 21.628 20.198 18.793 201942.85 148013.83 107654.54 4.24
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2013-10-30 6.00 6.08 6.07 5.93 10351.61 0.07 1.17 6.226 6.459 6.818 14104.09 27101.91 51733.86 0.35
2013-10-29 6.34 6.36 6.00 5.80 24353.96 -0.32 -5.06 6.304 6.578 6.873 16348.68 35900.07 56028.35 0.83
2013-10-28 6.29 6.35 6.32 6.27 6932.24 0.03 0.48 6.428 6.707 6.937 17326.80 39962.91 60272.80 0.24
2013-10-25 6.45 6.51 6.29 6.26 15821.80 -0.16 -2.48 6.518 6.803 6.990 22308.18 44614.07 66388.60 0.54
2013-10-24 6.44 6.55 6.45 6.42 13060.82 -0.01 -0.15 6.608 6.902 7.028 28920.61 51651.77 71549.71 0.45
2013-10-23 6.66 6.71 6.46 6.43 21574.58 -0.16 -2.42 6.692 6.958 7.050 40099.74 58000.64 72719.07 0.74
2013-10-22 6.87 6.87 6.62 6.56 29244.56 -0.15 -2.22 6.852 7.037 7.071 55451.47 64374.92 73113.23 1.00
2013-10-21 6.72 6.83 6.77 6.64 31839.14 0.03 0.45 6.986 7.104 7.087 62599.02 72434.09 73828.71 1.09
2013-10-18 6.83 6.83 6.74 6.50 48883.97 -0.13 -1.89 7.088 7.142 7.100 66919.96 76280.45 74639.76 1.67
2013-10-17 7.26 7.35 6.87 6.86 68956.43 -0.39 -5.37 7.196 7.173 7.110 74382.92 77813.07 76205.81 2.36
2013-10-16 7.27 7.36 7.26 7.10 98333.25 -0.03 -0.41 7.224 7.176 7.114 75901.55 76365.82 76614.82 3.37
2013-10-15 7.26 7.40 7.29 7.11 64982.30 0.01 0.14 7.222 7.168 7.106 73298.36 76156.62 75471.74 2.22
2013-10-14 7.31 7.39 7.28 7.15 53443.84 0.00 0.00 7.222 7.167 7.096 82269.17 80582.69 76054.49 1.83
2013-10-11 7.00 7.29 7.28 7.00 86198.78 0.27 3.85 7.196 7.177 7.085 85640.95 88163.12 77384.53 2.95
2013-10-10 7.24 7.30 7.01 6.99 76549.58 -0.24 -3.31 7.150 7.153 7.073 81243.23 91447.65 76833.64 2.62
2013-10-09 7.29 7.40 7.25 7.16 85317.30 -0.04 -0.55 7.128 7.141 7.077 76830.08 87437.49 76852.57 2.92
2013-10-08 7.14 7.50 7.29 7.11 109836.34 0.14 1.96 7.114 7.104 7.064 79014.88 81851.54 76247.95 3.76
2013-09-30 7.01 7.25 7.15 6.91 70302.73 0.10 1.42 7.112 7.069 7.047 78896.22 75223.32 73664.23 2.41
2013-09-27 6.81 7.15 7.05 6.69 64210.19 0.15 2.17 7.158 7.057 7.038 90685.30 72999.06 73944.35 2.20
2013-09-26 7.17 7.24 6.90 6.78 54483.85 -0.28 -3.90 7.156 7.046 7.037 101652.07 74598.55 74829.28 1.86
2013-09-25 7.28 7.43 7.18 7.12 96241.27 -0.10 -1.37 7.154 7.051 7.051 98044.90 76863.82 76863.82 3.29
2013-09-24 7.34 7.65 7.28 7.21 109243.07 -0.10 -1.35 7.094 7.037 7.037 84688.21 74710.77 74710.77 3.74
2013-09-23 6.92 7.47 7.38 6.92 129248.11 0.34 4.83 7.026 7.006 7.006 71550.41 70394.23 70394.23 4.42
2013-09-18 6.82 7.25 7.04 6.81 119044.05 0.15 2.18 6.956 6.953 6.953 55312.83 61986.54 61986.54 4.07
2013-09-17 6.89 7.00 6.89 6.72 36447.99 0.01 0.14 6.936 6.938 6.938 47545.03 52476.95 52476.95 1.25
2013-09-16 6.92 6.99 6.88 6.78 29457.82 -0.06 -0.86 6.948 6.948 6.948 55682.74 55682.74 55682.74 1.01
2013-09-13 6.99 7.07 6.94 6.85 43554.09 -0.09 -1.28 6.965 6.965 6.965 62238.97 62238.97 62238.97 1.49
2013-09-12 6.94 7.05 7.03 6.80 48060.21 0.09 1.30 6.973 6.973 6.973 68467.27 68467.27 68467.27 1.65
2013-09-11 6.91 7.09 6.94 6.65 80205.05 -0.01 -0.14 6.945 6.945 6.945 78670.80 78670.80 78670.80 2.75
2013-09-10 6.67 7.01 6.95 6.52 77136.54 0.34 5.14 6.950 6.950 6.950 77136.54 77136.54 77136.54 2.64

503 rows × 14 columns

设定历史数据的时间:


In [2]:
ts.get_hist_data('600848', start='2015-01-05', end='2015-01-09')


Out[2]:
open high close low volume price_change p_change ma5 ma10 ma20 v_ma5 v_ma10 v_ma20 turnover
date
2015-01-09 11.68 11.71 11.23 11.19 44851.56 -0.44 -3.77 11.538 11.363 11.682 58792.43 60665.93 107924.27 1.54
2015-01-08 11.70 11.92 11.67 11.64 56845.71 -0.25 -2.10 11.516 11.349 11.647 57268.99 61376.00 105823.50 1.95
2015-01-07 11.58 11.99 11.92 11.48 86681.38 0.31 2.67 11.366 11.251 11.543 55049.74 61628.07 103010.58 2.97
2015-01-06 11.13 11.66 11.61 11.03 59199.93 0.35 3.11 11.182 11.155 11.382 54854.38 63401.05 98686.98 2.03
2015-01-05 11.16 11.39 11.26 10.89 46383.57 0.14 1.26 11.156 11.212 11.198 58648.75 68429.87 97141.81 1.59

Example 2. 获取实时交易数据(Realtime Quotes Data)


In [3]:
df = ts.get_realtime_quotes('000581') # Single stock symbol
df[['code', 'name', 'price', 'bid', 'ask', 'volume', 'amount', 'time']]


Out[3]:
code name price bid ask volume amount time
0 000581 威孚高科 22.420 22.430 22.430 11144969 256467490.010 14:59:51

Example 3. 获取历史分笔数据


In [4]:
df = ts.get_tick_data('600848', date='2014-01-09')
df.head(10)


Out[4]:
time price change volume amount type
0 15:00:00 6.05 -- 8 4840 卖盘
1 14:59:55 6.05 -- 50 30250 卖盘
2 14:59:35 6.05 -- 20 12100 卖盘
3 14:59:30 6.05 -0.01 165 99825 卖盘
4 14:59:20 6.06 0.01 4 2424 买盘
5 14:59:05 6.05 -0.01 2 1210 卖盘
6 14:58:55 6.06 -- 4 2424 买盘
7 14:58:45 6.06 -- 2 1212 买盘
8 14:58:35 6.06 0.01 2 1212 买盘
9 14:58:25 6.05 -0.01 20 12100 卖盘

Example 4. 获取实时交易数据(Realtime Quotes Data)


In [5]:
df = ts.get_realtime_quotes('000581') #Single stock symbol
df[['code', 'name', 'price', 'bid', 'ask', 'volume', 'amount', 'time']]


Out[5]:
code name price bid ask volume amount time
0 000581 威孚高科 22.430 22.430 22.440 11259169 259028996.010 15:02:03