有可能在python中获得月度历史股票价格吗?

时间:2016-07-30 16:51:49

标签: python pandas quantitative-finance

我知道使用熊猫这就是你通常获得的每日股价报价。但我想知道是否有可能获得每月或每周报价,是否可以通过一个参数来获得月度报价?

ToString

3 个答案:

答案 0 :(得分:6)

Yahoo!每月收盘价财务......

import pandas_datareader.data as web

data = web.get_data_yahoo('IBM','01/01/2015',interval='m')

您可以根据需要替换间隔输入('d','w','m'等)。

答案 1 :(得分:1)

试试这个:

In [175]: from pandas_datareader.data import DataReader

In [176]: ibm = DataReader('IBM',  'yahoo', '2001-01-01', '2012-01-01')

更新仅显示Adj Close的平均值(月开始)

In [12]: ibm.groupby(pd.TimeGrouper(freq='MS'))['Adj Close'].mean()
Out[12]:
Date
2001-01-01     79.430605
2001-02-01     86.625519
2001-03-01     75.938913
2001-04-01     81.134375
2001-05-01     90.460754
2001-06-01     89.705042
2001-07-01     83.350254
2001-08-01     82.100543
2001-09-01     74.335789
2001-10-01     79.937451
                 ...
2011-03-01    141.628553
2011-04-01    146.530774
2011-05-01    150.298053
2011-06-01    146.844772
2011-07-01    158.716834
2011-08-01    150.690990
2011-09-01    151.627555
2011-10-01    162.365699
2011-11-01    164.596963
2011-12-01    167.924676
Freq: MS, Name: Adj Close, dtype: float64

仅显示Adj Close(月末)的平均值

In [13]: ibm.groupby(pd.TimeGrouper(freq='M'))['Adj Close'].mean()
Out[13]:
Date
2001-01-31     79.430605
2001-02-28     86.625519
2001-03-31     75.938913
2001-04-30     81.134375
2001-05-31     90.460754
2001-06-30     89.705042
2001-07-31     83.350254
2001-08-31     82.100543
2001-09-30     74.335789
2001-10-31     79.937451
                 ...
2011-03-31    141.628553
2011-04-30    146.530774
2011-05-31    150.298053
2011-06-30    146.844772
2011-07-31    158.716834
2011-08-31    150.690990
2011-09-30    151.627555
2011-10-31    162.365699
2011-11-30    164.596963
2011-12-31    167.924676
Freq: M, Name: Adj Close, dtype: float64

每月平均值(所有列):

In [179]: ibm.groupby(pd.TimeGrouper(freq='M')).mean()
Out[179]:
                  Open        High         Low       Close    Volume   Adj Close
Date
2001-01-31  100.767857  103.553571   99.428333  101.870357   9474409   79.430605
2001-02-28  111.193160  113.304210  108.967368  110.998422   8233626   86.625519
2001-03-31   97.366364   99.423637   95.252272   97.281364  11570454   75.938913
2001-04-30  103.990500  106.112500  102.229501  103.936999  11310545   81.134375
2001-05-31  115.781363  117.104091  114.349091  115.776364   7243463   90.460754
2001-06-30  114.689524  116.199048  113.739523  114.777618   6806176   89.705042
2001-07-31  106.717143  108.028095  105.332857  106.646666   7667447   83.350254
2001-08-31  105.093912  106.196521  103.856522  104.939999   6234847   82.100543
2001-09-30   95.138667   96.740000   93.471334   94.987333  12620833   74.335789
2001-10-31  101.400870  103.140000  100.327827  102.145217   9754413   79.937451
2001-11-30  113.449047  114.875715  112.510952  113.938095   6435061   89.256046
2001-12-31  120.651001  122.076000  119.790500  121.087999   6669690   94.878736
2002-01-31  116.483334  117.509524  114.613334  115.994762   9217280   90.887920
2002-02-28  103.194210  104.389474  101.646316  102.961579   9069526   80.764672
2002-03-31  105.246500  106.764499  104.312999  105.478499   7563425   82.756873
...                ...         ...         ...         ...       ...         ...
2010-10-31  138.956188  140.259048  138.427142  139.631905   6537366  122.241844
2010-11-30  144.281429  145.164762  143.385241  144.439524   4956985  126.878319
2010-12-31  145.155909  145.959545  144.567273  145.251819   4245127  127.726929
2011-01-31  152.595000  153.950499  151.861000  153.181501   5941580  134.699880
2011-02-28  163.217895  164.089474  162.510002  163.339473   4687763  144.050847
2011-03-31  160.433912  161.745652  159.154349  160.425651   5639752  141.628553
2011-04-30  165.437501  166.587500  164.760500  165.978500   5038475  146.530774
2011-05-31  169.657144  170.679046  168.442858  169.632857   5276390  150.298053
2011-06-30  165.450455  166.559093  164.691819  165.593635   4792836  146.844772
2011-07-31  178.124998  179.866502  177.574998  178.981500   5679660  158.716834
2011-08-31  169.734350  171.690435  166.749567  169.360434   8480613  150.690990
2011-09-30  169.752858  172.034761  168.109999  170.245714   6566428  151.627555
2011-10-31  181.529525  183.597145  180.172379  182.302381   6883985  162.365699
2011-11-30  184.536668  185.950952  182.780477  184.244287   4619719  164.596963
2011-12-31  188.151428  189.373809  186.421905  187.789047   4925547  167.924676

[132 rows x 6 columns]

每周平均值(所有列):

In [180]: ibm.groupby(pd.TimeGrouper(freq='W')).mean()
Out[180]:
                  Open        High         Low       Close    Volume   Adj Close
Date
2001-01-07   89.234375   94.234375   87.890625   91.656250  11060200   71.466436
2001-01-14   93.412500   95.062500   91.662500   93.412500   7470200   72.835824
2001-01-21  100.250000  103.921875   99.218750  102.250000  13851500   79.726621
2001-01-28  109.575000  111.537500  108.675000  110.600000   8056720   86.237303
2001-02-04  113.680000  115.465999  111.734000  113.582001   6538080   88.562436
2001-02-11  113.194002  115.815999  111.639999  113.884001   7269320   88.858876
2001-02-18  113.960002  116.731999  113.238000  115.106000   7225420   89.853021
2001-02-25  109.525002  111.375000  105.424999  107.977501  10722700   84.288436
2001-03-04  103.390001  106.052002  100.386000  103.228001  11982540   80.580924
2001-03-11  105.735999  106.920000  103.364002  104.844002   9226900   81.842391
2001-03-18   95.660001   97.502002   93.185997   94.899998  13863740   74.079992
2001-03-25   90.734000   92.484000   88.598000   90.518001  11382280   70.659356
2001-04-01   95.622000   97.748000   94.274000   96.106001  10467580   75.021411
2001-04-08   95.259999   97.360001   93.132001   94.642000  12312580   73.878595
2001-04-15   98.350000   99.520000   95.327502   97.170000  10218625   75.851980
...                ...         ...         ...         ...       ...         ...
2011-09-25  170.678003  173.695996  169.401996  171.766000   6358100  152.981582
2011-10-02  176.290002  178.850000  174.729999  176.762000   7373680  157.431216
2011-10-09  175.920001  179.200003  174.379999  177.792001   7623560  158.348576
2011-10-16  185.366000  187.732001  184.977997  187.017999   5244180  166.565614
2011-10-23  180.926001  182.052002  178.815997  180.351999   9359200  160.628611
2011-10-30  183.094003  184.742001  181.623996  183.582001   5743800  163.505379
2011-11-06  184.508002  186.067999  183.432004  184.716003   4583780  164.515366
2011-11-13  185.350000  186.690002  183.685999  185.508005   4180620  165.750791
2011-11-20  187.600003  189.101999  185.368002  186.738000   5104420  166.984809
2011-11-27  181.067497  181.997501  178.717499  179.449997   4089350  160.467733
2011-12-04  185.246002  187.182001  184.388000  186.052002   5168720  166.371376
2011-12-11  191.841998  194.141998  191.090002  192.794000   4828580  172.400204
2011-12-18  191.085999  191.537998  187.732001  188.619998   6037220  168.667729
2011-12-25  183.810001  184.634003  181.787997  183.678000   5433360  164.248496
2012-01-01  185.140003  185.989998  183.897499  184.750000   3029925  165.207100

[574 rows x 6 columns]

答案 2 :(得分:1)

从Quandl获取它:

import pandas as pd
import quandl
quandl.ApiConfig.api_key = 'xxxxxxxxxxxx'  # Optional
quandl.ApiConfig.api_version = '2015-04-09'  # Optional

ibm = quandl.get("WIKI/IBM", start_date="2000-01-01", end_date="2012-01-01", collapse="monthly", returns="pandas")