Stock price data acquisition tips

Introduction

This time, I will summarize how to obtain stock price data.

On familiar sites, time series data of Yahoo! Finance seems to be easy, but Yahoo! Finance seems to be prohibited from scraping, that is, extracting stock price data from Web pages, so <a href="https:: //pandas-datareader.readthedocs.io/en/latest/ "target="_blank" rel="noopener noreferrer"> Pandas DataReader is recommended.

I referred to the following article. https://ntk-lab.com/import_stock_data/ How to get stock price data in Python

Pandas DataReader

Here Stock price data used by Pandas Data Reader Where to get it and how to access it.

IEX

For stocks listed on the NY market, You can easily find the sample code that comes from IEX, but to get stock price data from IEX, you need to create an account and get an API key. You can get a free API key, but there is a limit to the amount of data you can get in a day. Paid API keys aren't too expensive, but I couldn't find the cancellation method page, so I'll stop.

iex

import os
import pandas_datareader as pdr
from datetime import datetime

 os.environ ['IEX_API_KEY'] ='The value of the acquired API key'
start_date = datetime(2020,1,1)
end_date = datetime(2020,7,10)

df = pdr.data.DataReader(name='AAPL', data_source="iex", start=start_date, end=end_date)
df.to_csv("AAPL.csv")

Stooq

It was easy to use Stooq . No need to get API key. The acquisition example is as follows.

import pandas_datareader.stooq as web
from datetime import datetime

start_date = datetime(2020,1,1)
end_date = datetime(2020,7,10)

dr = web.StooqDailyReader('^DJI', start=start_date, end=end_date)
df = dr.read()
df.to_csv('DOW30.csv')

You can also get indexes from each country and stocks in Japan. For overseas stocks, specify the ticker code, and for Japanese domestic stocks, specify the 4-digit securities code with the extension ".JP".

Acquired brand Designation code
Apple AAPL
DOW30 ^DJI
NIKKEI225 ^NKX
Polar Ocean 1301.JP
NF Nikkei Double Inverse 1357.JP
Listed Nikkei double 1358.JP

Summary

Pandas DataReader's stooq is easy to use because it doesn't require API key acquisition. You don't have to deal with forbidden scraping because Japanese listed stocks are available.

Recommended Posts

Stock price data acquisition tips
Automatic acquisition of stock price data with docker-compose
Stock price forecast using deep learning [Data acquisition]
Stock price acquisition code by scraping (Selenium)
Download Japanese stock price data with python
Acquisition of time series data (daily) of stock prices
Get stock price data with Quandl API [Python]
Stock Price Forecast 2 Chapter 2
Stock Price Forecast 1 Chapter 1
Data is missing when getting stock price data with Pandas-datareader
python-fitbit data acquisition query summary
Python: Stock Price Forecast Part 2
Stock Price Forecasting Using LSTM_1
Get stock price with Python
Data processing tips with Pandas
Tips for data analysis ・ Notes
Python: Stock Price Forecast Part 1
[Stock price analysis] Learn pandas with Nikkei 225 (004: Change read data to Nikkei 225)
I tried using GLM (generalized linear model) for stock price data
[Stock price analysis] Learning pandas with fictitious data (001: environment preparation-file reading)
[Stock price analysis] Learning pandas with fictitious data (003: Type organization-candlestick chart)
[Python] My stock price forecast [HFT]
Data acquisition memo using Backlog API
Preprocessing in machine learning 2 Data acquisition
[Data science basics] Data acquisition from API
Continuous acquisition by Twitter API (Tips)