見出し画像

親子上場(その2): GPTにPythonコードを書いてもらい投資戦略分析

前回:親子上場している銘柄リストアップ(記事はこちら
今回:同リストの中からTOBされた銘柄があるか検証→株価ベースに突出している銘柄を特定してTOBなのか確認。
更に:同ポートフォリオにEWで投資した際のリターンを検証
次回:勝率を上げるためのポートフォリオフィルター検証

必要なもの:
・ChatGPT(無料版でよいです)
・Google Colab(無料です。多分Google のアカウントが必要)
・Google Spreadsheet(無料)

方法: OpenAIを使い直近6ヶ月の株価パフォーマンスを出力するパイソンを書いてもらう。順番に並べて上昇率が高い銘柄チェック。
プロンプト1:「パイソンで指定した株式銘柄の過去6か月の株価パフォーマンスをダウンロードするコードを書いてください。ダウンロードした株価の最初の値を100としてその後どれぐらいのパフォーマンスなのかを見せるコードを書いてください。」
プロンプト2:「上記でダウンロードした株価をパフォーマンスが高い順番に横棒グラフで出力するコードを書いてください。その際にパフォーマンスのデータが横棒グラフの横に記載されるようにしてください。」
コード:(ColabのNotebookに張り付けて実行)

import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt

# Define the array of bank stock tickers
stocks =["3085.T","3326.T","9325.T","4997.T","3686.T","9055.T","2653.T","8570.T","9787.T","4343.T","7512.T","8905.T","3141.T","2698.T","9876.T","7450.T","2686.T","8278.T","8198.T","9946.T","3222.T","7315.T","8133.T","2692.T","4739.T","4215.T","2281.T","3604.T","6191.T","4059.T","8254.T","4107.T","8242.T","9919.T","3271.T","8303.T","7326.T","4765.T","7198.T","7614.T","5727.T","4178.T","3254.T","7624.T","5395.T","6662.T","8096.T","7961.T","1777.T","7739.T","8060.T","3802.T","2830.T","9369.T","4151.T","8244.T","9726.T","9636.T","6192.T","3198.T","3484.T","9436.T","5476.T","7829.T","2705.T","7412.T","7421.T","4479.T","3280.T","9967.T","1739.T","9425.T","4784.T","3788.T","6026.T","7177.T","3769.T","4051.T","3633.T","6180.T","3695.T","5753.T","1884.T","8209.T","2388.T","7970.T","7849.T","4506.T","9719.T","3738.T","1949.T","5191.T","6641.T","8249.T","9057.T","6744.T","9232.T","6185.T","4394.T","1712.T","3275.T","5659.T","7299.T","4847.T","3159.T","2573.T","3847.T","3459.T","8844.T","4974.T","6295.T","7079.T","1946.T","8892.T","6141.T","6164.T","3461.T","3628.T","4963.T","4348.T","7774.T","3223.T","7215.T","4270.T","3688.T","4812.T","7678.T","7102.T","8700.T","4816.T","6588.T","6565.T","6368.T","9632.T","1934.T","2806.T","6403.T","8014.T","7205.T","6345.T","2715.T","2737.T","1850.T","7222.T","3316.T","2226.T","5449.T","5481.T","2327.T","4551.T","6040.T","1973.T","6807.T","9613.T","3850.T","4762.T","3622.T","7181.T","7182.T","4262.T","7630.T","2497.T","9216.T","2412.T","3546.T","2588.T","2763.T","7513.T","9414.T","8891.T","3347.T","9423.T","9445.T","4312.T","3852.T","3784.T","6188.T","6955.T","6967.T","1775.T","3830.T","6937.T","1930.T","1832.T","7298.T","7229.T","1776.T","6391.T","9691.T","7942.T","4091.T","7105.T","6366.T","2003.T","2393.T","2892.T","7451.T","2651.T","1948.T","8283.T","2211.T","1429.T","3178.T","3140.T","4650.T","3185.T","9980.T","8105.T","9275.T"]

# Download the stock prices for the past month for each bank
prices = yf.download(stocks, period="6mo")['Adj Close']

# Calculate the historical stock prices with starting stock price as 100
normalized_prices = prices / prices.iloc[0] * 100-100
# Get the most recent normalized prices and sort in ascending order
recent_normalized_prices = normalized_prices.iloc[-1].sort_values()

# Create a horizontal bar chart for the most recent normalized prices
plt.figure(figsize=(10,30))
plt.title('My Stocks')
plt.xlabel('Past 1 Month Performance %')
bars = plt.barh(recent_normalized_prices.index, recent_normalized_prices.values)
for bar in bars:
    plt.text(bar.get_width() -2.5, bar.get_y() + bar.get_height() / 2, str(round(bar.get_width(), 1)))
plt.show()

出力:下記のテキストとその下の図表がアウトプット

ERROR:yfinance:['8096.T', '1777.T', '1832.T', '9967.T', '6295.T', '7961.T', '8249.T', '6641.T', '3830.T']: Exception('%ticker%: No data found, symbol may be delisted') 

これらの銘柄がなぜエラーなのか(あり得るのは、非上場化=TOB完了した?可能性もあり)これは後で確認するとして、データのチャートをチェック(予想通りめちゃ長い図表です)図表を見てみましょう・・・
一番パフォーマンスが高いのが、174.6%(2.746倍)増している銘柄3316.TなのでTOB発表された確率高そう・・・・

確認:とりあえずヤフーファイナンスで確認。企業情報にいくとTOBしていると確認。株価の上がり方からしてTOBっぽいですね。「企業情報」を見てみると「キャノン子会社がTOB」と記載あり。発見⁉

出所:ヤフーファイナンス
出所:ヤフーファイナンス

仮にこのポートフォリオにEqual Weightで投資するとするリターンは?
イコールウェイト(各銘柄に同じ額)で投資した際のリターンを算出

import numpy as np

# recent_normalized_pricesの平均値を計算し、NaNのデータを除外
average_performance = np.nanmean(recent_normalized_prices)

# 結果を表示
print(f'平均パフォーマンス: {average_performance:.2f}')

出力:「平均パフォーマンス: 6.84」・・・
なのでポートフォリオとして+6.84%増加ということになります。過去6か月日本株のパフォーマンスと比較するベンチマークとしてTOPIXを見てみると、10.7%増ぐらいなので、絶対値ではプラスですが、対TOPIXだと4ポイント程度パフォーマンスが劣るということになります。

しかし、六か月の期間でTOB関連の発表がそう多くあるわけではないと思います。なので1年間、2年間のスパンで見た場合パフォーマンスはどうなのかを確認しましょう。

ここから先は

2,869字 / 3画像

¥ 300

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