import yfinance as yf
import pandas as pd
import sqlite3
import matplotlib.pyplot as plt
import time
# データベースファイルのパス
db_path = 'profit_loss.db'
# 日経225銘柄リスト
ticker_symbols = ['7272','2914','4452','4911','6326','3659','4704','5108','1605','2502','3405','3436','4631','8804','2503','4004','4324','4519','5101','5201','5301','6302','6361','6723','7751','2501','4578','5214','9147','1928','6506','8233','9602','3086','3382','8267','7741','8697','4689','6861','8795','8411','9064','4568','5831','7832','2768','3099','6301','6758','6902','7733','8035','9104','3401','4005','4523','4902','5406','5801','6501','6594','8031','8253','9101','9434','9613','9984','4061','4503','4507','5019','6103','6178','6473','6701','6762','7004','7186','7270','8316','9020','9433','4042','5401','5711','5713','6471','6645','6770','6857','6988','7013','7211','7267','7269','8001','8002','8015','8053','8058','8304','8630','8766','9001','9022','9107','9432','9532','9843','2432','4043','5333','6702','7735','7752','7974','8309','8591','8725','9021','9201','2802','2871','3402','3407','4183','4188','4506','5332','5411','5541','5631','6098','6305','6504','6752','6841','6971','7205','8252','8306','8308','8750','9735','1332','1801','2002','2282','3289','4021','4502','4543','5020','5232','5802','6113','6472','6724','6753','7012','7202','7261','8331','8604','9202','1721','1802','1803','1808','1812','1925','1963','2269','2413','2531','2801','3861','3863','4041','4208','4661','5706','5714','6273','6367','6479','6503','6674','6952','6981','7011','7731','7762','7911','7912','8354','8601','8801','8802','9005','9007','9008','9009','9301','9501','9502','9503','9531','9766','4063','4901','5233','6954','6976','7201','7203','7951','8830','5803','4385','6920','9983','4751','4151']
# SQLiteデータベースに接続
conn = sqlite3.connect(db_path)
# 各銘柄について損益を計算
for ticker_symbol in ticker_symbols:
ticker = yf.Ticker(ticker_symbol + '.T')
# 過去5年間の配当データと株価データを取得
dividends = ticker.dividends.last('5YE')
for ex_date in dividends.index:
buy_date = ex_date - pd.Timedelta(days=30)
sell_date = ex_date - pd.Timedelta(days=1)
# 株価データを取得
hist = ticker.history(start=buy_date, end=sell_date)
time.sleep(1)
# 購入日と売却日の株価を取得
if not hist.empty and len(hist) > 1:
buy_price = hist.iloc[0]['Close']
sell_price = hist.iloc[-1]['Close']
profit = (sell_price - buy_price) / buy_price * 100 # 損益率(%)
# 損益率と配当支払日をデータベースに保存
pd.DataFrame({
'Ticker': [ticker_symbol],
'AvgProfit': [profit],
'Date': [ex_date.strftime('%Y-%m-%d')] # 日付を文字列形式で保存
}).to_sql('profit_loss', conn, if_exists='append', index=False)
# データベース接続を閉じる
conn.close()
# データベースから損益データを読み込むための関数
def load_data(db_path):
conn = sqlite3.connect(db_path)
df = pd.read_sql('SELECT * FROM profit_loss', conn)
conn.close()
df['Year'] = pd.to_datetime(df['Date']).dt.year
return df
# データベースから損益データを読み込む
profit_loss_df = load_data(db_path)
# 銘柄ごとにプロット
plt.figure(figsize=(12, 8))
for ticker in profit_loss_df['Ticker'].unique():
data = profit_loss_df[profit_loss_df['Ticker'] == ticker]
years = data['Year'].unique()
profits_by_year = data.groupby('Year')['AvgProfit'].mean().reindex(years).fillna(0)
# 年ごとの損益を折れ線グラフでプロット
plt.plot(profits_by_year.index, profits_by_year.values, '-o', label=f'{ticker} Yearly')
plt.xlabel('Year / Ticker Symbol')
plt.ylabel('Profit (%)')
plt.title('Yearly Profit/Loss for Each Ticker Over 5 Years')
plt.xticks(rotation=45) # X軸のラベルを45度回転
plt.legend()
plt.tight_layout()
plt.show()