See Part I for instructions on how to get pandas_datareader
or yfinance
module to retrieve the data, and Part II to learn how to get stock market data for different geographies. In Part III and Part IV, review the tutorial on how to analyse the stock market data for all the stocks which make up the S&P 500.
Using Quandl to get Stock Market Data (Optional)
Quandl has many data sources to get different types of stock market data. However, some are free and some are paid. Wiki is the free data source of Quandl to get the data of the end of the day prices of 3000+ US equities. It is curated by Quandl community and also provides information about the dividends and split.
Quandl also provides paid data source of minute and lower frequencies.
To get the stock market data, you need to first install the quandl module if it is not already installed using the pip command as shown below.
!pip install quandl
Install quandl.py hosted with ❤ by GitHub
You need to get your own API Key from quandl to get the stock market data using the below code.
After you get your key, assign the variable QUANDL_API_KEY with that key. Then set the start date, end date and the ticker of the asset whose stock market data you want to fetch.
The quandl get
method takes this stock market data as input and returns the open, high, low, close, volume, adjusted values and other information.
# Import quandl package
import quandl
# 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_API_KEY = 'REPLACE-THIS-TEXT-WITH-A-REAL-API-KEY'
# This is to prompt you to change the Quandl Key
if QUANDL_API_KEY == 'REPLACE-THIS-TEXT-WITH-A-REAL-API-KEY':
raise Exception("Please provide a valid Quandl API key!")
# Set the start and end date
start_date = '1990-01-01'
end_date = '2018-03-01'
# Set the ticker name
ticker = 'AMZN'
# Feth the data
data = quandl.get('WIKI/'+ticker,
start_date=start_date,
end_date=end_date,
api_key=QUANDL_API_KEY)
# Print the first 5 rows of the dataframe
data.head()
get_data_quandl.py hosted with ❤ by GitHub
Date | Open | High | Low | Close | Volume | Ex-Dividend | Split Ratio | Adj. Open | Adj. High | Adj. Low | Adj. Close | Adj. Volume |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1997-05-16 | 22.38 | 23.75 | 20.50 | 20.75 | 1225000.0 | 0.0 | 1.0 | 1.865000 | 1.979167 | 1.708333 | 1.729167 | 14700000.0 |
1997-05-19 | 20.50 | 21.25 | 19.50 | 20.50 | 508900.0 | 0.0 | 1.0 | 1.708333 | 1.770833 | 1.625000 | 1.708333 | 6106800.0 |
1997-05-20 | 20.75 | 21.00 | 19.63 | 19.63 | 455600.0 | 0.0 | 1.0 | 1.729167 | 1.750000 | 1.635833 | 1.635833 | 5467200.0 |
1997-05-21 | 19.25 | 19.75 | 16.50 | 17.13 | 1571100.0 | 0.0 | 1.0 | 1.604167 | 1.645833 | 1.375000 | 1.427500 | 18853200.0 |
1997-05-22 | 17.25 | 17.38 | 15.75 | 16.75 | 981400.0 | 0.0 | 1.0 | 1.437500 | 1.448333 | 1.312500 | 1.395833 | 11776800.0 |
To learn more about how you can use Quandl, check out this article:
Stay tuned for next installment in which Ishan Shah will demonstrate in details how to use yfinance to get fundamental data.
See https://blog.quantinsti.com/stock-market-data-analysis-python/ for additional insight on this topic.
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 Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.