Stock Data and Analysis in Python
Get historical stock data for different timeframes and from different python packages
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.
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.
Note:
- Quandl requires NumPy (v1.8 or above) and pandas (v0.14 or above) to work.
- To get your API key, sign up for a free Quandl account. Then, you can find your API key on Quandl account settings page.
- 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:
- For the time interval between two consecutive data points in the time series, the following values are supported:
1min
,5min
,15min
,30min
,60min
- By default,
outputsize=compact
. Stringscompact
andfull
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
- Stock Data and Analysis Blog
- Quandl
- yfinance