5つの例が、J-Quantas API クライアントの使用サンプルに追加された。
これが先日 main
にマージされたので、早速使ってみる。スコープを狭めるために、財務情報関連を1つのclass
に押し込めることにした。
from datetime import datetime from dateutil import tz import jquantsapi import pandas as pd import os import numpy as np class Statements: def __init__(self, storage_dir_path: str, jqapi: jquantsapi.Client): self.storage_dir_path = storage_dir_path self._jqapi = jqapi def fetch_from_api_with_cache(self): ''' Original: J-Quants/jquants-api-client-python https://github.com/J-Quants/jquants-api-client-python/blob/da16a23d85c80a0106673f0a0deaec3437016418/examples/20220825-003-dividend.ipynb Arguments ------------------- storage_dir_path: str DataFrameをpickle化したものを保存先の親ディレクトリへのパス ''' # 過去3ヶ月に発表された財務情報を取得します now = pd.Timestamp.now(tz="Asia/Tokyo") start_dt = now - pd.Timedelta(90, unit="D") end_dt = now if end_dt.hour < 1: # データ更新時間前の場合は日付を1日ずらします。 end_dt -= pd.Timedelta(1, unit="D") statements_file = f"{self.storage_dir_path}/statements.pkl" if not os.path.isfile(statements_file): cache_dir = f"{self.storage_dir_path}/raw_statements" os.makedirs(cache_dir, exist_ok=True) df_s = self._jqapi.get_statements_range( start_dt=start_dt, end_dt=end_dt, cache_dir=cache_dir ) df_s.to_pickle(self.statements_file) print(f"save file: {self.statements_file}") else: df_s = self.load_df(statements_file) df = self._format_dt(df_s) return df def _format_dt(self, df_s: pd.DataFrame): ''' Original: J-Quants/jquants-api-client-python https://github.com/J-Quants/jquants-api-client-python/blob/da16a23d85c80a0106673f0a0deaec3437016418/examples/20220825-003-dividend.ipynb ''' # float64にするために"-"をnp.nanに置き換えます df_s.replace({"-": np.nan}, inplace=True) df_s["ResultDividendPerShareFiscalYearEnd"] = df_s["ResultDividendPerShareFiscalYearEnd"].astype(np.float64) df_s["EarningsPerShare"] = df_s["EarningsPerShare"].astype(np.float64) df_s["ForecastDividendPerShareAnnual"] = df_s["ForecastDividendPerShareAnnual"].astype(np.float64) df_s["ForecastEarningsPerShare"] = df_s["ForecastEarningsPerShare"].astype(np.float64) # 日付型に変換します df_s.loc[:, "DisclosedDate"] = pd.to_datetime( df_s["DisclosedDate"], format="%Y-%m-%d" ) df_s.loc[:, "CurrentPeriodEndDate"] = pd.to_datetime( df_s["CurrentPeriodEndDate"], format="%Y-%m-%d" ) df_s.loc[:, "CurrentFiscalYearStartDate"] = pd.to_datetime( df_s["CurrentFiscalYearStartDate"], format="%Y-%m-%d" ) df_s.loc[:, "CurrentFiscalYearEndDate"] = pd.to_datetime( df_s["CurrentFiscalYearEndDate"], format="%Y-%m-%d" ) df_s.sort_values("DisclosedUnixTime", inplace=True) return df_s def load_df(self, statements_file: str): ''' Original: J-Quants/jquants-api-client-python https://github.com/J-Quants/jquants-api-client-python/blob/da16a23d85c80a0106673f0a0deaec3437016418/examples/20220825-003-dividend.ipynb ''' print(f"file exists: {statements_file}, loading") df_s = pd.read_pickle(statements_file) return df_s my_refresh_token: str = '(token)' cli = jquantsapi.Client(refresh_token=my_refresh_token) parent_path = '/content/drive/MyDrive/drive_ws/marketdata' statements = Statements(parent_path, cli) df = statements.fetch_from_api_with_cache() df[df.LocalCode == 79740]
これを実行すると、下記のような表を得られる。
index | DisclosureNumber | DisclosedDate | ApplyingOfSpecificAccountingOfTheQuarterlyFinancialStatements | AverageNumberOfShares | BookValuePerShare | ChangesBasedOnRevisionsOfAccountingStandard | ChangesInAccountingEstimates | ChangesOtherThanOnesBasedOnRevisionsOfAccountingStandard | CurrentFiscalYearEndDate | CurrentFiscalYearStartDate | CurrentPeriodEndDate | DisclosedTime | DisclosedUnixTime | EarningsPerShare | Equity | EquityToAssetRatio | ForecastDividendPerShare1stQuarter | ForecastDividendPerShare2ndQuarter | ForecastDividendPerShare3rdQuarter | ForecastDividendPerShareAnnual |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
92 | 20220726504912 | 2022-08-03 00:00:00 | NaN | 116817xxx.0 | NaN | false | false | false | 2023-03-31 00:00:00 | 2022-04-01 00:00:00 | 2022-06-30 00:00:00 | 15:30:00 | 1xxx540600.0 | xxxx.55 | 203xxxx000000.0 | 0.xxx | NaN | NaN | NaN | NaN |
これで次は過去5年間の配当性向がどのようになっているかを見られるようにしたい。