pandas 如何使用 Python 下载股票价格数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50800454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I download stock price data with Python?
提问by user8270077
I have installed pandas-datareader
but I'm wondering if there are alternatives.
我已经安装,pandas-datareader
但我想知道是否有替代方案。
So far, I'm using this:
到目前为止,我正在使用这个:
import pandas_datareader.data as web
start_date = '2018-01-01'
end_date = '2018-06-08'
panel_data = web.DataReader('SPY', 'yahoo', start_date, end_date)
回答by Ishan Shah
Yahoo Financeis one of the free sources to get stock data. You can get the data either using pandas datareader or can get using yfinance library. The method to get data from yfinance library is shown below.
雅虎财经是获取股票数据的免费来源之一。您可以使用 pandas datareader 或使用 yfinance 库获取数据。从 yfinance 库中获取数据的方法如下所示。
import yfinance as yf
# Get the data of the stock AAPL
data = yf.download('AAPL','2016-01-01','2019-08-01')
Wiki is one of the free source available on quandlto get the data for the 3000+ US equities. This is a community maintained data. Recently it is stopped being maintained but however, it is a good free source to backtest your strategies. To get the data, you need to get the free API key from quandl and replace the in the below code with your API key.
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>)
Note: Quandl requires NumPy (v1.8 or above) and pandas (v0.14 or above) to work. To get your API key, sign up for a free Quandl account. Then, you can find your API key on Quandl account settings page.
注意:Quandl 需要 NumPy(v1.8 或更高版本)和 pandas(v0.14 或更高版本)才能工作。要获取您的 API 密钥,请注册一个免费的 Quandl 帐户。然后,您可以在 Quandl 帐户设置页面上找到您的 API 密钥。
回答by Maik Holzhey
See below. Code is written in Python 2.7, but should work in 3.5 when you replace the print function. Make sure when you copy the spacing is correct in your editor: a tab is 4 spaces etc.
见下文。代码是用 Python 2.7 编写的,但在替换打印函数时应该可以在 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();
回答by Maik Holzhey
You could also use quandl, but you have to sign up and get your own API key. Not sure if any of the free financial APIs that have worked well with the pandas webreader still function reliably and well...
您也可以使用 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()
回答by questiondude
I found the easiest to be the new SimFin Python APIwhich lets you download stock-prices and fundamental data, save it to disk, and load it into Pandas DataFrames with only a few lines of code. They have also made several tutorialson how to use their data with other libraries such as statsmodels, scikit-learn, TensorFlow, etc. The basic example below is copied from their github page.
我发现最简单的是新的SimFin Python API,它允许您下载股票价格和基本数据,将其保存到磁盘,然后只需几行代码即可将其加载到 Pandas DataFrames 中。他们还制作了几个关于如何将他们的数据与其他库(例如 statsmodels、scikit-learn、TensorFlow 等)一起使用的教程。 下面的基本示例是从他们的 github 页面复制的。
You install the SimFin python package by typing this command in a terminal window (preferably in its own environement, see their full instructions):
您可以通过在终端窗口中键入以下命令来安装 SimFin python 包(最好在其自己的环境中,请参阅他们的完整说明):
pip install simfin
Then you copy-paste the following into a Jupyter Notebook or Python source-file:
然后将以下内容复制粘贴到 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]])
This produces the following output:
这会产生以下输出:
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
We can also load the daily share-prices and plot the closing share-price for Microsoft (ticker MSFT):
我们还可以加载每日股价并绘制 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')
This produces the following image:
这会产生以下图像: