How to get Stock Market Data in Python?
One of the first sources from which you can get historical daily price-volume stock market data is Yahoo! Finance. You can use
yfinance module to get the data and then can download or store in a csv file by using pandas.to_csv method.
If yfinance is not installed on your computer, then run the below line of code from your Jupyter Notebook to install yfinance.
!pip install yfinance
# Import yfinance package
import yfinance as yf
# Set the start and end date
start_date = ‘1990-01-01’
end_date = ‘2021-07-12’
# Set the ticker
ticker = ‘AMZN’
# Get the data
data = yf.download(ticker, start_date, end_date)
# Print 5 rows
To visualize the adjusted close price data, you can use the matplotlib library and plot method as shown below.
# Import matplotlib for plotting
import matplotlib.pyplot as plt
# Plot adjusted close price data
Let us improve the plot by resizing, giving appropriate labels and adding grid lines for better readability.
# Plot the adjusted close price
data[‘Adj Close’].plot(figsize=(10, 7))
# Define the label for the title of the figure
plt.title(“Adjusted Close Price of %s” % ticker, fontsize=16)
# Define the labels for x-axis and y-axis
# Plot the grid lines
plt.grid(which=”major”, color=’k’, linestyle=’-.’, linewidth=0.5)
# Show the plot
- Adjusted close price stock market data is available
- Most recent stock market data is available
- Doesn’t require API key to fetch the stock market data
See https://blog.quantinsti.com/stock-market-data-analysis-python/ for additional insight on this topic and for a tutorial from Nitesh Khandelwal, Co-Founder and CEO, QuantInsti, that answers questions related to getting Data for Algo Trading.
Past performance is not indicative of future results.
Any stock, options or futures symbols displayed are for illustrative purposes only and are not intended to portray recommendations.
Disclosure: Interactive Brokers
Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.
Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.