use*_*077 9 python stocks pandas pandas-datareader
我已经安装了pandas-daatreader但是已经弃用了用于下载历史股票价格数据的Google和Yahoo API.
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)
ImmediateDeprecationError:
Yahoo Daily has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.
See https://github.com/pydata/pandas-datareader/issues
Run Code Online (Sandbox Code Playgroud)
你能告诉我如何使用Python访问历史股票价格吗?事实上,我有兴趣在研究时尽可能地提价.
谢谢.
雅虎财经是获取股票数据的免费资源之一。您可以使用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()
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
注意: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()
Run Code Online (Sandbox Code Playgroud)
如果您正在查看分钟级别或基本数据,那么此页面可能会有所帮助。
小智 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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9041 次 |
| 最近记录: |