Pythonで機械学習を勉強している時にテストデータとして株価か市場指標を使用と思い取得方法を調べてみるとpandas-datareaderライブラリが便利そうなことが判明
早速インストール
・インストールログ
pip install pandas-datareader
Defaulting to user installation because normal site-packages is not writeable
Collecting pandas-datareader
Downloading pandas_datareader-0.10.0-py3-none-any.whl (109 kB)
|████████████████████████████████| 109 kB 536 kB/s
Collecting lxml
Downloading lxml-4.6.3-cp37-cp37m-manylinux2014_aarch64.whl (6.7 MB)
|████████████████████████████████| 6.7 MB 1.4 MB/s
Requirement already satisfied: requests>=2.19.0 in ./.local/lib/python3.7/site-packages (from pandas-datareader) (2.26.0)
Requirement already satisfied: pandas>=0.23 in ./.local/lib/python3.7/site-packages (from pandas-datareader) (1.3.3)
Requirement already satisfied: numpy>=1.17.3 in ./.local/lib/python3.7/site-packages (from pandas>=0.23->pandas-datareader) (1.21.2)
Requirement already satisfied: pytz>=2017.3 in ./.local/lib/python3.7/site-packages (from pandas>=0.23->pandas-datareader) (2021.1)
Requirement already satisfied: python-dateutil>=2.7.3 in ./.local/lib/python3.7/site-packages (from pandas>=0.23->pandas-datareader) (2.8.1)
Requirement already satisfied: six>=1.5 in ./.local/lib/python3.7/site-packages (from python-dateutil>=2.7.3->pandas>=0.23->pandas-datareader) (1.15.0)
Requirement already satisfied: idna<4,>=2.5 in ./.local/lib/python3.7/site-packages (from requests>=2.19.0->pandas-datareader) (3.2)
Requirement already satisfied: certifi>=2017.4.17 in ./.local/lib/python3.7/site-packages (from requests>=2.19.0->pandas-datareader) (2021.5.30)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./.local/lib/python3.7/site-packages (from requests>=2.19.0->pandas-datareader) (1.26.6)
Requirement already satisfied: charset-normalizer~=2.0.0 in ./.local/lib/python3.7/site-packages (from requests>=2.19.0->pandas-datareader) (2.0.6)
Installing collected packages: lxml, pandas-datareader
Successfully installed lxml-4.6.3 pandas-datareader-0.10.0
・取得テスト
国内国内のトヨタ(7203.T)の株価を取得してみる
python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pandas_datareader import data
>>> df = data.DataReader('7203.T','yahoo')
>>> df.head(5)
High Low ... Volume Adj Close
Date ...
2016-10-12 1201.199951 1188.000000 ... 25115500.0 962.449158
2016-10-13 1211.599976 1190.800049 ... 39343500.0 964.387817
2016-10-14 1199.400024 1184.400024 ... 31210500.0 968.265503
2016-10-17 1212.599976 1198.199951 ... 28159500.0 974.243408
2016-10-18 1202.800049 1189.400024 ... 27223000.0 967.780762
[5 rows x 6 columns]
・複数銘柄、取得期間指定して取得してみる
df = data.DataReader(['2914.T','9513.T'],'yahoo','2021-10-01','2021-10-10')
>>> df.head(5)
Attributes Adj Close Close ... Open Volume
Symbols 2914.T 9513.T 2914.T 9513.T ... 2914.T 9513.T 2914.T 9513.T
Date ...
2021-10-01 2172.5 1597.0 2172.5 1597.0 ... 2174.0 1605.0 4596700 892700
2021-10-04 2159.5 1629.0 2159.5 1629.0 ... 2180.5 1608.0 4433800 1266300
2021-10-05 2165.0 1612.0 2165.0 1612.0 ... 2156.0 1626.0 3440000 1014500
2021-10-06 2193.0 1655.0 2193.0 1655.0 ... 2176.5 1625.0 5366200 1310600
2021-10-07 2181.0 1636.0 2181.0 1636.0 ... 2190.5 1658.0 2994300 748400
[5 rows x 12 columns]
・市場指標(日経平均)を取得してみる
df = data.DataReader(['^N225'],'yahoo','2021-10-01','2021-10-10')
>>> df.head(5)
Attributes Adj Close Close ... Open Volume
Symbols ^N225 ^N225 ... ^N225 ^N225
Date ...
2021-10-01 28771.070312 28771.070312 ... 29235.109375 82200000
2021-10-04 28444.890625 28444.890625 ... 29044.470703 76400000
2021-10-05 27822.119141 27822.119141 ... 28050.390625 88200000
2021-10-06 27528.869141 27528.869141 ... 28033.910156 100200000
2021-10-07 27678.210938 27678.210938 ... 27665.970703 79600000
[5 rows x 6 columns]
・米国株式情報を取得してみる
df = data.DataReader(['AMD'],'yahoo','2021-10-01','2021-10-10')
>>> df.head(3)
Attributes Adj Close Close High Low Open Volume
Symbols AMD AMD AMD AMD AMD AMD
Date
2021-09-30 102.900002 102.900002 104.440002 101.989998 102.080002 57588500
2021-10-01 102.449997 102.449997 103.000000 100.639999 102.599998 41491600
2021-10-04 100.339996 100.339996 101.849998 99.820000 101.739998 41967100
・その他銘柄/指標
【NASDAQ】^IXIC
【S&P500】^GSPC
【NYダウ】^DJI
・取得できるカラム情報
Adj Close:調整後終値
Close:終値
High:高値
Low:安値
Open:始値
Volume:出来高
・その他
情報の取得サイトによって取サーバに負荷がかかるため取得禁止の場合もあるので事前に確認が必要のようです。