如何使用Python下载股票价格数据?

时间:2018-06-11 14:47:38

标签: python pandas stocks pandas-datareader

我已经安装了pandas-daatreader,但已弃用了用于下载历史股票价格数据的Google和Yahoo API。

data[reference.columns] = reference

您能告诉我如何使用Python访问历史股票价格吗?事实上,我有兴趣在我做研究时尽可能地让价格回来。

谢谢。

4 个答案:

答案 0 :(得分:5)

Yahoo Finance是获取股票数据的免费来源之一。您可以使用pandas datareader或yfinance库获取数据。从yfinance库获取数据的方法如下所示。

# Import the yfinance. If you get module not found error the run !pip install yfiannce from your Jupyter notebook
import yfinance as yf
# Get the data of the stock AAPL
data = yf.download('AAPL','2016-01-01','2019-08-01')
# Import the plotting library
import matplotlib.pyplot as plt
%matplotlib inline
# Plot the close price of the AAPL
data['Adj Close'].plot()
plt.show()

Quandl Wiki是quandl上免费的免费资源之一,可以获取3000多种美国股票的数据。这是社区维护的数据。最近,它已停止维护,但是,它是一个很好的免费资源,可以回溯测试您的策略。 要获取数据,您需要从quandl获取免费的API密钥,然后将以下代码中的替换为您的API密钥。

# Import the quandl package
import quandl
# Get the data from quandl
data = quandl.get("WIKI/KO", start_date="2016-01-01", end_date="2018-01-01", api_key=<Your_API_Key>)
# Plot the close pr
import matplotlib.pyplot as plt
data.Close.plot()
# Show the plot
plt.show()

注意: Quandl需要NumPy(v1.8或更高版本)和熊猫(v0.14或更高版本)才能工作。 要获取您的API密钥,请注册一个免费的Quandl帐户。然后,您可以在Quandl帐户设置页面上找到您的API密钥。

获取多只股票的数据

# Define the ticker list
import pandas as pd
tickers_list = [‘AAPL’, ‘WMT’, ‘IBM’, ‘MU’, ‘BA’, ‘AXP’]
# Import pandas
data = pd.DataFrame(columns=tickers_list)
# Fetch the data
import yfinance as yf
for ticker in tickers_list:
 data[ticker] = yf.download(ticker, ‘2015–1–1’, ‘2019–1–1’)[‘Adj Close’]
# Print first 5 rows of the data
data.head()

# Plot all the close prices
((data.pct_change()+1).cumprod()).plot(figsize=(10, 7))
# Show the legend
plt.legend()
# Define the label for the title of the figure
plt.title(“Adjusted Close Price”, fontsize=16)
# Define the labels for x-axis and y-axis
plt.ylabel(‘Price’, fontsize=14)
plt.xlabel(‘Year’, fontsize=14)
# Plot the grid lines
plt.grid(which=”major”, color=’k’, linestyle=’-.’, linewidth=0.5)
plt.show()

如果您正在查看分钟级别或基本数据,那么this页可能会有所帮助。

答案 1 :(得分:2)

请参阅下文。代码是用Python 2.7编写的,但是当您替换print函数时,它应该在3.5中工作。确保在复制时在编辑器中间距是正确的:制表符为4个空格,等等。

# pip install datareader
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import numpy as np

from datetime import datetime, timedelta

#stock of interest
stock=['MSFT','SAP','V','JPM']

# period of analysis
end = datetime.now()
start = end - timedelta(days=500)

for i in range(len(stock)):
    f = web.DataReader(stock[i], 'morningstar', start, end)

    # nice looking timeseries (DataFrame to panda Series)
    f = f.reset_index()
    f = pd.Series(f.Close.values,f.Date)

    print "Start: Year, Month, Day, Time"
    print str(start)
    f.plot(label=stock[i]);
    plt.legend()
    plt.ylabel('price in [USD]')

plt.show();

答案 2 :(得分:1)

我发现最简单的是新的SimFin Python API,它使您可以下载股票价格和基本数据,将其保存到磁盘,然后仅用几行代码即可将其加载到Pandas DataFrames中。他们还就如何将其数据与其他模型(例如statsmodels,scikit-learn,TensorFlow等)一起使用进行了tutorials的介绍。

您可以通过在终端窗口中键入以下命令来安装SimFin python软件包(最好在其自身的环境中,请参见其full instructions):

pip install simfin

然后,将以下内容复制粘贴到Jupyter Notebook或Python源文件中:

import simfin as sf
from simfin.names import *

# Set your API-key for downloading data.
# If the API-key is 'free' then you will get the free data,
# otherwise you will get the data you have paid for.
# See www.simfin.com for what data is free and how to buy more.
sf.set_api_key('free')

# Set the local directory where data-files are stored.
# The dir will be created if it does not already exist.
sf.set_data_dir('~/simfin_data/')

# Load the annual Income Statements for all companies in USA.
# The data is automatically downloaded if you don't have it already.
df = sf.load_income(variant='annual', market='us')

# Print all Revenue and Net Income for Microsoft (ticker MSFT).
print(df.loc['MSFT', [REVENUE, NET_INCOME]])

这将产生以下输出:

                  Revenue   Net Income
Report Date
2008-06-30   6.042000e+10  17681000000
2009-06-30   5.843700e+10  14569000000
2010-06-30   6.248400e+10  18760000000
2011-06-30   6.994300e+10  23150000000
2012-06-30   7.372300e+10  16978000000
2013-06-30   7.784900e+10  21863000000
2014-06-30   8.683300e+10  22074000000
2015-06-30   9.358000e+10  12193000000
2016-06-30   9.115400e+10  20539000000
2017-06-30   9.657100e+10  25489000000
2018-06-30   1.103600e+11  16571000000
2019-06-30   1.258430e+11  39240000000

我们还可以加载每日股价并绘制Microsoft的收盘价(股票代号MSFT):

# Load daily share-prices for all companies in USA.
# The data is automatically downloaded if you don't have it already.
df_prices = sf.load_shareprices(market='us', variant='daily')

# Plot the closing share-prices for ticker MSFT.
df_prices.loc['MSFT', CLOSE].plot(grid=True, figsize=(20,10), title='MSFT Close')

这将产生以下图像:

MSFT Share-Price Close

答案 3 :(得分:0)

您也可以使用quandl,但是您必须注册并获取自己的API密钥。不确定与pandas网络阅读器配合使用的任何免费金融API是否仍然可靠且运行良好...

# pip install datareader
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
# quandl api explore
import quandl
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
# api instructions
quandl.ApiConfig.api_key = "YOUR_API_KEY"
end = datetime.now()
start = end - timedelta(days=365)
# frankfurt stock exchange
mydata2 = quandl.get('FSE/VOW3_X', start_date = start, end_date = end)
f = mydata2.reset_index()
# timeseries
plt.figure(1)
f = pd.Series(f.Close.values,f.Date)
f.plot()
plt.show()