見出し画像

親子上場(その3): 投資して、パフォーマンスをLINE通知してくれる機能を作ってみよう編

実験としてTOBされるのでは、というフィルターをいくつかかけ、5つ銘柄を選別。
一旦、これら5銘柄を選別:
1 住友理工(親:住友電気工業)
2 日本食品化工(三菱商事)
3 伊藤忠食品(伊藤忠)
4 フジオーゼックス(大同特殊鋼)
5 三井住建道路(三井住友建設)

フィルター:親会社が大企業で業績もよさそうで子会社持ち比率4割以上、PBR1倍以下、営業利益増益予想、過去のVAL水準に対しての今のVAL(これはバフェットコードを参考)、信用倍率、直近6カ月パフォーマンス(モメンタム的要素)

買い時のレッスン:買うと決めた時はすぐに買う(なので指値で見た時より安めで買いたいけどそれやると全然変えなくて結局翌日繰り越しで買えないみたいなこともあるから、一旦その時点でアップサイドをかなり見込んでいるなら成り行きでも買ってOKな気がする。

課題感:今回買った時から市場に対してアウトパフォーム(市場より良い成績)かアンダーパフォームしているかを確認したい! 既存で他の銘柄を保有しているため、この5つの銘柄の合計パフォーマンスと対マーケットというのは、既存の証券会社アプリ・マイページでは確認できないという課題あり。

GPTで5つの銘柄の購入金額、保有株数を指定して、そこから合計のパフォーマンスと対市場(今回は日経225)に対して毎日パフォーマンスを比較するコードを書いてもらう。

import yfinance as yf
import pandas as pd

# Define the list of stock tickers, purchase prices, and quantities
tickers = ["1776.T", "5191.T", "2892.T", "7299.T", "2692.T"] ##ここに購入銘柄のTickerを記入
purchase_prices = [xxxx, xxx, xxxx, xxxx, xxxx] ##ここに購入時の価格を記入
quantities = [xxx, xxx, xxx, xxx, xxx] ##ここに購入株式数を記入

# Create an empty list to store stock data
stock_data = []

# Fetch current stock prices and calculate the performance
total_investment = 0
current_portfolio_value = 0

for i, ticker in enumerate(tickers):
    stock = yf.Ticker(ticker)
    stock_history = stock.history(period="1d")
    current_price = stock_history["Close"].iloc[0]
    purchase_price = purchase_prices[i]
    quantity = quantities[i]
    stock_data.append({
        "Stock": ticker,
        "Purchase Price": purchase_price,
        "Current Price": current_price,
        "Quantity": quantity,
    })
    total_investment += purchase_price * quantity
    current_portfolio_value += current_price * quantity

# Calculate the total performance
total_performance = (current_portfolio_value - total_investment) / total_investment * 100

# Create a DataFrame for stock data
df_stock_data = pd.DataFrame(stock_data)

# Calculate the performance of Nikkei 225
nikkei_ticker = '^N225'
nikkei_purchase_price = 30991.69

nikkei_stock = yf.Ticker(nikkei_ticker)
nikkei_history = nikkei_stock.history(period="1d")
nikkei_current_price = nikkei_history["Close"].iloc[0]

nikkei_performance = ((nikkei_current_price - nikkei_purchase_price) / nikkei_purchase_price) * 100

# Add a row for Nikkei in df_stock_data
df_stock_data = df_stock_data.append({
    "Stock": nikkei_ticker,
    "Purchase Price": nikkei_purchase_price,
    "Current Price": nikkei_current_price,
    "Quantity": 1,  # Assuming 1 unit for Nikkei
}, ignore_index=True)

# Calculate the performance (percentage) of each stock
df_stock_data["Performance (%)"] = ((df_stock_data["Current Price"] - df_stock_data["Purchase Price"]) / df_stock_data["Purchase Price"]) * 100

# Display the stock data DataFrame
print("Stock Data:")
display(df_stock_data)

# Create a DataFrame for performance metrics
df_performance = pd.DataFrame({
    "Metric": ["Total Performance", "Nikkei Performance", "Relative Performance"],
    "Value": [total_performance, nikkei_performance, total_performance - nikkei_performance]
})

# Display the performance metrics DataFrame
print("\nPerformance Metrics:")
display(df_performance)

更に通知してもらいたいので次のコードを作成:Colabでこのコードを実行すると、Google Spreadsheetの数字を更新してくれて、このSpreadsheetに変更があると自動でGASが作動するトリガーを作成して、LINEで通知してくれる機能を追加。LINE通知してくれるGASもGPTが作成(一部ググってLINE通知のGASについての記事を確認して、どのようなコードなのかをレファレンスしてGPTのコード理解)

10/27に購入して11/7時点で+4.77%、ほぼ市場並みのパフォーマンス(0.64ポイントアウトパフォーム)

次:毎日市場クローズ後の3:15PMにColabのコードを実装するという機能を付けたいが未だやり方がわからず。。。模索中
次回続く。


この記事が参加している募集

この記事が気に入ったらサポートをしてみませんか?