Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Stock Market Data And Analysis In Python – Part V

Stock Market Data And Analysis In Python – Part V

Posted September 17, 2021 at 1:35 pm

Ishan Shah
QuantInsti

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
DateOpenHighLowCloseVolumeEx-DividendSplit RatioAdj. OpenAdj. HighAdj. LowAdj. CloseAdj. Volume
1997-05-1622.3823.7520.5020.751225000.00.01.01.8650001.9791671.7083331.72916714700000.0
1997-05-1920.5021.2519.5020.50508900.00.01.01.7083331.7708331.6250001.7083336106800.0
1997-05-2020.7521.0019.6319.63455600.00.01.01.7291671.7500001.6358331.6358335467200.0
1997-05-2119.2519.7516.5017.131571100.00.01.01.6041671.6458331.3750001.42750018853200.0
1997-05-2217.2517.3815.7516.75981400.00.01.01.4375001.4483331.3125001.39583311776800.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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.