Stock Data and Analysis in Python

Get historical stock data for different timeframes and from different python packages

Ishan Shah
3 min readAug 8, 2019
Source: Freepik

Many of us aspire to be quant traders or want to add data analytics to our investment and/or trading process. This is where fetching the right set of data and in right format becomes most important.

You can get the stock data using popular data vendors. And then you can perform analysis on it. In python, there are many libraries which can be used to get the stock market data. The most common set of data is the price volume data. These data can be used to create quant strategies, technical strategies or very simple buy-and-hold strategies.

Yahoo Finance

Yahoo Finance is one of the free sources to get stock data. You can get the data either using pandas datareader or can get using yfinance library. The method to get data from yfinance library is shown below.

Daily Price of AAPL

Quandl

Wiki is one of the free source available on quandl to get the data for the 3000+ US equities. This is a community maintained data. Recently it is stopped being maintained but however, it is a good free source to backtest your strategies.

To get the data, you need to get the free API key from quandl and replace the <Your_API_Key> in the below code with your API key.

Daily Data for Coca-Cola

Note:

  1. Quandl requires NumPy (v1.8 or above) and pandas (v0.14 or above) to work.
  2. To get your API key, sign up for a free Quandl account. Then, you can find your API key on Quandl account settings page.
  3. To fetch data for different markets and types

Data for multiple stocks

Minute level data using the Alpha vantage

Until so far we have fetched the data with a frequency of daily. That is the end of the day data. Using the below code, you get the stock data for each minute. This is very useful to backtest intraday trading strategies and for risk management purpose.

Note:

  1. For the time interval between two consecutive data points in the time series, the following values are supported: 1min, 5min, 15min, 30min, 60min
  2. By default, outputsize=compact. Strings compact and full are accepted with the following specifications: compact returns only the latest 100 data points in the intraday time series; full returns the full-length intraday time series. The "compact" option is recommended if you would like to reduce the data size of each API call.

Once you have data the next step is to analyze the data. This can be done using pyfolio library. This helps to compute Sharpe ratio, Sortino ratio, equity curve and other analytics useful to analyze the performance of your strategy.

I hope you find this useful and happy trading and analysis.

Sources and References

  1. Stock Data and Analysis Blog
  2. Quandl
  3. yfinance

--

--

Ishan Shah

Machine learning, Data Science and quant trading enthusiast. Working as Quant Researcher in Quantra. LinkedIn: https://www.linkedin.com/in/ishan-shah-18393828/