雅虎金融是获取股票数据的免费来源之一。您可以使用pandas数据读取器获取数据,也可以使用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()
量子
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=)
# 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页可能会有所帮助。