Getting stock quotes using Python

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 the yfinance. If you get module not found error the run !pip install yfianance from your Jupyter notebook
import yfinance as yf

# Get the data for the stock AAPL
data = yf.download('AAPL','2016-01-01','2019-08-01')

# Import the plotting library
import matplotlib.pyplot as plt
%matplotlib inline

# Plot the close price of the AAPL
data['Adj Close'].plot()
plt.show()

image

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.

# Define the ticker list
import pandas as pd
tickers_list = ['AAPL', 'WMT', 'IBM', 'MU', 'BA', 'AXP']

# Import pandas
data = pd.DataFrame(columns=tickers_list)

# Fetch the data

for ticker in tickers_list:
    data[ticker] = yf.download(ticker,'2016-01-01','2019-08-01')['Adj Close']

# Print first 5 rows of the data
data.head()

image

# Plot all the close prices
((data.pct_change()+1).cumprod()).plot(figsize=(10, 7))

# Show the legend
plt.legend()

# Define the label for the title of the figure
plt.title("Adjusted Close Price", fontsize=16)

# Define the labels for x-axis and y-axis
plt.ylabel('Price', fontsize=14)
plt.xlabel('Year', fontsize=14)

# Plot the grid lines
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.5)
plt.show()

image

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.

image

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.

image

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.

Source: https://habr.com/ru/post/undefined/


All Articles