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

使用pandas的内建函数DataReader从雅虎财经网站读取股价数据

import tushare as ts
pingan = ts.get_k_data('601318',start='2011-01-01')
pingan.head()
dateopenclosehighlowvolumecode
02011-01-0424.98425.01125.31424.830245626.0601318
12011-01-0524.87024.11024.96224.083427554.0601318
22011-01-0624.14523.11224.14522.611947078.0601318
32011-01-0723.27923.29723.84222.765659013.0601318
42011-01-1023.24822.86623.68822.809282919.0601318
pingan.tail()
dateopenclosehighlowvolumecode
19992019-04-0378.4879.8680.1578.37760219.0601318
20002019-04-0480.0880.2080.9679.25795626.0601318
20012019-04-0880.7080.5982.2580.02916122.0601318
20022019-04-0980.4081.1082.0080.37601737.0601318
20032019-04-1080.8082.0382.1179.66783620.0601318
pingan['date'] = pd.to_datetime(pingan.date)
pingan.set_index('date',inplace = True)
pingan.index
DatetimeIndex(['2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07',
               '2011-01-10', '2011-01-11', '2011-01-12', '2011-01-13',
               '2011-01-14', '2011-01-17',
               ...
               '2019-03-27', '2019-03-28', '2019-03-29', '2019-04-01',
               '2019-04-02', '2019-04-03', '2019-04-04', '2019-04-08',
               '2019-04-09', '2019-04-10'],
              dtype='datetime64[ns]', name='date', length=2004, freq=None)
pingan['close'].plot(figsize=(12,8))
<matplotlib.axes._subplots.AxesSubplot at 0x2360fa68e10>

在这里插入图片描述

g_yue = pingan['close'].resample('M').ohlc()
g_yue
openhighlowclose
date
2011-01-3125.01125.01121.24921.842
2011-02-2821.95222.98021.47721.868
2011-03-3122.08423.51221.43321.736
2011-04-3022.51424.12322.51422.954
2011-05-3123.10323.10320.97221.407
2011-06-3021.31021.31019.49521.213
2011-07-3121.15621.60919.53419.724
2011-08-3119.90619.90617.86718.053
2011-09-3018.23118.29414.93514.935
2011-10-3115.08217.22615.08217.208
2011-11-3017.44918.03615.68715.687
2011-12-3116.54116.90114.93515.322
2012-01-3115.08217.39515.06017.057
2012-02-2916.64318.55616.64318.160
2012-03-3118.04018.35216.12316.274
2012-04-3016.79918.40516.75018.111
2012-05-3118.76119.16618.08518.636
2012-06-3018.76120.51818.09420.349
2012-07-3120.50920.57219.49319.793
2012-08-3120.22720.22717.19317.381
2012-09-3017.82918.83317.56018.833
2012-10-3118.61319.01817.09117.423
2012-11-3017.66117.66115.86516.382
2012-12-3116.44920.33816.44920.338
2013-01-3121.15521.65319.95221.653
2013-02-2822.79923.70120.19421.137
2013-03-3120.63420.63418.24518.757
2013-04-3018.40719.26517.87717.877
2013-05-3117.97618.71717.37417.920
2013-06-3017.92917.92915.55815.726
...............
2016-11-3033.39335.03132.96434.602
2016-12-3134.68835.26933.01233.745
2017-01-3133.98334.77433.53634.774
2017-02-2833.92635.06933.92634.507
2017-03-3134.44035.25033.90735.250
2017-04-3035.33636.15533.82136.155
2017-05-3136.10743.32735.48842.927
2017-06-3043.78447.58442.54647.251
2017-07-3146.61352.00345.64150.087
2017-08-3152.81254.94047.73853.948
2017-09-3054.08354.68551.56752.616
2017-10-3153.19962.52553.19962.525
2017-11-3062.29276.20362.18567.926
2017-12-3166.15872.77466.15867.985
2018-01-3170.52076.51468.01472.939
2018-02-2873.32873.84362.59365.828
2018-03-3166.96571.97763.44863.448
2018-04-3062.81666.30459.15459.154
2018-05-3159.59162.27258.92160.358
2018-06-3059.88263.99056.26758.000
2018-07-3154.67361.54554.67360.990
2018-08-3159.10962.84256.02062.357
2018-09-3062.10968.50060.60068.500
2018-10-3164.78067.93061.85063.630
2018-11-3064.30067.35062.64063.490
2018-12-3164.99064.99056.10056.100
2019-01-3155.18062.98055.18062.980
2019-02-2863.21073.00063.21070.010
2019-03-3172.39077.10068.70077.100
2019-04-3078.60082.03078.60082.030

100 rows × 4 columns

pingan['close'].loc['2017'].plot(figsize=(12,8))
<matplotlib.axes._subplots.AxesSubplot at 0x23612ba5e10>

在这里插入图片描述

shouyi = (pingan['close'][-1] - pingan['close'][0])/pingan['close'][0]
shouyi
2.2797569069609374
y_num = pingan.index[-1].year - pingan.index[0].year
y_num
8
shouyi**(1/y_num)
1.1085009445718548
pingan['close'].to_period('A')
date
2011    25.011
2011    24.110
2011    23.112
2011    23.297
2011    22.866
2011    22.905
2011    23.288
2011    23.283
2011    22.844
2011    21.908
2011    21.974
2011    22.273
2011    21.438
2011    21.714
2011    21.249
2011    21.315
2011    21.504
2011    21.943
2011    21.710
2011    21.842
2011    21.952
2011    21.930
2011    22.224
2011    21.961
2011    22.980
2011    22.695
2011    22.629
2011    22.528
2011    22.492
2011    22.659
         ...  
2019    69.980
2019    70.010
2019    72.390
2019    72.580
2019    72.070
2019    72.160
2019    70.290
2019    68.700
2019    68.790
2019    69.250
2019    71.540
2019    72.800
2019    73.850
2019    75.000
2019    75.350
2019    76.550
2019    75.800
2019    74.720
2019    72.290
2019    72.690
2019    74.220
2019    73.400
2019    77.100
2019    78.600
2019    78.960
2019    79.860
2019    80.200
2019    80.590
2019    81.100
2019    82.030
Freq: A-DEC, Name: close, Length: 2004, dtype: float64
pingan['close'].to_period('A').groupby(level = 0).first().plot()
<matplotlib.axes._subplots.AxesSubplot at 0x23612bdc748>

在这里插入图片描述

pingan['42d'] = pingan['close'].rolling(window=42).mean()
pingan['250d'] = pingan['close'].rolling(window=250).mean()
pingan[['close','42d','250d']].plot(figsize=(12,8),color = ['y','b','r'])
<matplotlib.axes._subplots.AxesSubplot at 0x2360fa38550>

在这里插入图片描述

pingan['duishu'] = np.log(pingan['close']/pingan['close'].shift(1))   #shifting 指的是沿着时间轴将数据前移或后移。
pingan[['close','duishu']].plot(subplots = True,figsize=(12,12))
array([<matplotlib.axes._subplots.AxesSubplot object at 0x0000023612FB0CF8>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000002361301E978>],
      dtype=object)

在这里插入图片描述

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐