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
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 |
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