2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

yfinanceライブラリを使って、株価などを取得してみよう

Last updated at Posted at 2023-05-16

ライブラリをインポート

import yfinance as yf

今回はサンプルとして、トヨタを例にあげて試してみます。

T = "7203.T"
ticker = yf.Ticker(T)

株価履歴を取得

特定の期間の開始価格、終了価格、高値、安値、調整後終了価格、出来高を取得できます。

data = yf.download(T, start='2020-01-01', end='2020-12-31')
print(data)

Open High ... Adj Close Volume
Date ...
2020-01-06 1519.800049 1525.199951 ... 1382.728027 33361500
2020-01-07 1530.800049 1544.400024 ... 1410.145020 24803500
2020-01-08 1513.000000 1528.400024 ... 1392.232910 28061000
2020-01-09 1534.400024 1538.000000 ... 1401.920044 21265000
2020-01-10 1539.000000 1539.599976 ... 1400.275024 17417000
... ... ... ... ... ...
2020-12-24 1561.199951 1563.000000 ... 1440.847778 13676000
2020-12-25 1552.000000 1559.400024 ... 1449.407593 9966500
2020-12-28 1560.400024 1579.599976 ... 1469.690796 18226500
2020-12-29 1589.000000 1603.400024 ... 1491.276611 25593500
2020-12-30 1588.599976 1606.000000 ... 1480.669800 23459000

[242 rows x 6 columns]

期間指定

yf.download 関数の period パラメータを使用すると、特定の期間(例: 1d、5d、1mo、3mo、1y、2y、5y、10y、ytd、max)のデータを取得することができます。

data = yf.download(T, period='1mo')
print(data)

Open High Low Close Adj Close Volume
Date
2023-04-17 1823.0 1841.5 1822.5 1834.0 1834.0 18967700
2023-04-18 1844.0 1846.0 1827.5 1833.5 1833.5 23312400
2023-04-19 1833.0 1836.0 1816.5 1823.0 1823.0 16859000
2023-04-20 1810.0 1818.0 1796.0 1813.5 1813.5 21975500
2023-04-21 1790.0 1808.5 1784.0 1802.0 1802.0 21559900
2023-04-24 1815.0 1817.0 1798.0 1798.0 1798.0 14674600
2023-04-25 1803.0 1807.0 1795.0 1800.0 1800.0 20492700
2023-04-26 1793.5 1807.5 1791.0 1803.5 1803.5 17228200
2023-04-27 1804.5 1826.5 1799.0 1825.0 1825.0 21175700
2023-04-28 1839.0 1857.0 1833.5 1857.0 1857.0 29984600
2023-05-01 1870.5 1879.5 1865.0 1874.0 1874.0 18668200
2023-05-02 1877.0 1877.5 1853.0 1864.5 1864.5 17441500
2023-05-08 1860.0 1861.5 1848.0 1856.0 1856.0 17294400
2023-05-09 1861.0 1920.0 1855.0 1916.5 1916.5 39880100
2023-05-10 1920.0 1963.5 1894.0 1931.5 1931.5 55815000
2023-05-11 1950.0 1959.0 1902.0 1916.0 1916.0 34669900
2023-05-12 1945.5 1949.0 1913.5 1942.0 1942.0 28097200
2023-05-15 1939.5 1942.0 1926.0 1933.0 1933.0 16791400
2023-05-16 1933.0 1939.5 1919.0 1923.5 1923.5 18728200

データの間隔指定

interval パラメータを使用すると、データの間隔(例: 1m、2m、5m、15m、30m、60m、90m、1h、1d、5d、1wk、1mo、3mo)を指定できます。

data = yf.download(T, start='2020-01-01', end='2020-12-31', interval='1h')
print(data)

1 Failed download:

  • 7203.T: 1h data not available for startTime=1577804400 and endTime=1609340400. The requested range must be within the last 730 days.
    print(data)
    Empty DataFrame
    Columns: [Open, High, Low, Close, Adj Close, Volume]
    Index: []
    なぜかうまく取得できませんでした。

株価取得

株価を取得する関数を用いて取得してみます。

import yfinance as yf

def get_stock_price(stock_code):
    stock = yf.Ticker(stock_code)
    return stock.history().tail(1)['Close'].iloc[0]

stock_code = "7203.T"  # TOYOTAの株式コード
print(get_stock_price(stock_code))

配当情報を取得

import yfinance as yf

# トヨタの銘柄コード
ticker = "7203.T"

# Tickerオブジェクトを生成
toyota = yf.Ticker(ticker)

# 配当データを取得
dividends = toyota.dividends
print(dividends)

Date
1999-09-27 00:00:00+09:00 2.2
2000-03-28 00:00:00+09:00 2.6
2000-09-26 00:00:00+09:00 2.2
2001-03-27 00:00:00+09:00 2.8
2001-09-25 00:00:00+09:00 2.6
2002-03-26 00:00:00+09:00 3.0
2002-09-25 00:00:00+09:00 3.2
2003-03-26 00:00:00+09:00 4.0
2003-09-25 00:00:00+09:00 4.0
2004-03-26 00:00:00+09:00 5.0
2004-09-27 00:00:00+09:00 5.0
2005-03-28 00:00:00+09:00 8.0
2005-09-27 00:00:00+09:00 7.0
2006-03-28 00:00:00+09:00 11.0
2006-09-26 00:00:00+09:00 10.0
2007-03-27 00:00:00+09:00 14.0
2007-09-25 00:00:00+09:00 13.0
2008-03-26 00:00:00+09:00 15.0
2008-09-25 00:00:00+09:00 13.0
2009-03-26 00:00:00+09:00 7.0
2009-09-25 00:00:00+09:00 4.0
2010-03-29 00:00:00+09:00 5.0
2010-09-28 00:00:00+09:00 4.0
2011-03-29 00:00:00+09:00 6.0
2011-09-28 00:00:00+09:00 4.0
2012-03-28 00:00:00+09:00 6.0
2012-09-26 00:00:00+09:00 6.0
2013-03-27 00:00:00+09:00 12.0
2013-09-26 00:00:00+09:00 13.0
2014-03-27 00:00:00+09:00 20.0
2014-09-26 00:00:00+09:00 15.0
2015-03-27 00:00:00+09:00 25.0
2015-09-28 00:00:00+09:00 20.0
2016-03-29 00:00:00+09:00 22.0
2016-09-28 00:00:00+09:00 20.0
2017-03-29 00:00:00+09:00 22.0
2017-09-27 00:00:00+09:00 20.0
2018-03-28 00:00:00+09:00 24.0
2018-03-29 00:00:00+09:00 110.0
2018-09-26 00:00:00+09:00 20.0
2019-03-27 00:00:00+09:00 24.0
2019-09-27 00:00:00+09:00 20.0
2020-03-30 00:00:00+09:00 24.0
2020-09-29 00:00:00+09:00 1.0
2021-03-30 00:00:00+09:00 27.0
2021-09-29 00:00:00+09:00 24.0
2022-03-30 00:00:00+09:00 28.0
2022-09-29 00:00:00+09:00 25.0
2023-03-30 00:00:00+09:00 35.0

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?