Hello, Habr! I present to you the translation of the article “Historical Stock Price Data in Python” by Ishan Shah.An article on how to get daily historical stock data using yfinance and minute data using alpha vantage.As you know, stocks are a very volatile instrument and it is very important to carefully analyze the price behavior before making any trading decisions. Well, first you need to get the data and python can help with this.Exchange data can be downloaded using various packages. This article will cover yahoo finance and alpha vantage.Yahoo finance
First, try the yfianance package. It can be installed using the pip install yfinance command. The code below shows how to get data for AAPL from 2016 to 2019 and build the adjusted closing price (adjusted closing price for dividends and splits) on the chart.
import yfinance as yf
data = yf.download('AAPL','2016-01-01','2019-08-01')
import matplotlib.pyplot as plt
%matplotlib inline
data['Adj Close'].plot()
plt.show()
Well, if you need to get several shares, you need to make a small addition to the code. A DataFrame is used to store values. Using the matplotlib package and the received data, you can build a daily profitability chart.
import pandas as pd
tickers_list = ['AAPL', 'WMT', 'IBM', 'MU', 'BA', 'AXP']
data = pd.DataFrame(columns=tickers_list)
for ticker in tickers_list:
data[ticker] = yf.download(ticker,'2016-01-01','2019-08-01')['Adj Close']
data.head()

((data.pct_change()+1).cumprod()).plot(figsize=(10, 7))
plt.legend()
plt.title("Adjusted Close Price", fontsize=16)
plt.ylabel('Price', fontsize=14)
plt.xlabel('Year', fontsize=14)
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.5)
plt.show()
For values on Russian stocks, there is a slight subtlety. A dot and capital letters ME are added to the name of the stock. Thanks experts on the smartlab prompted.
Get minute data with Alpha vantage
Unfortunately, the free version of Yahoo Finance does not allow you to receive data with a frequency of less than the daily. For this, you can use the Alpha vantage package, which allows you to get intervals such as 1 min, 5 min, 15 min, 30 min, 60 min.
In the future, these data can be analyzed, created a trading strategy and evaluate the effectiveness using the pyfolio package. In it, you can evaluate the Sharpe ratio, Sortino coefficient, maximum drawdown and many other necessary indicators.I hope that my translation of the original article will be useful to you. The code has been verified and everything works. But for now, the question for me is the possibility of using Alpha vantage for the Russian market.