1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

機械学習で株価分析 10.説明変数の作成

Last updated at Posted at 2023-12-19

はじめに

本記事では、取得した情報から説明変数を作成する方法を紹介します。紹介内容はあくまで一例で、本気で株価予測を行う際は自身で様々な設計変数を試行錯誤する必要があります。なお、本記事は過去の記事 09.取得したデータの統合 の続きですので、未読の方はまずこちらをご参照ください。

手順

株価データ・経済指標データを取得し、以下のプログラムを実行することで新たな説明変数を作成できます。各変数の詳細な説明は省きますが、それぞれメジャーな指標ですので内容が気になる方は自身で検索してみてください。(名称や算出方法に誤りあればご指摘願います。)

データの列数が多いので、最後に列名のインデックスのみを表示するようにしています。

# 説明変数に株価に関する情報を追加
df_merge['Body']        = df_merge['Open']  - df_merge['Close']
df_merge['Body Ratio']  = df_merge['Body']  / df_merge['Close']
df_merge['Range']       = df_merge['High']  - df_merge['Low']
df_merge['Range Ratio'] = df_merge['Range'] / df_merge['Close']
df_merge['Gap']         = df_merge['Open']  - df_merge.shift(1)['Close']
df_merge['Gap Ratio']   = df_merge['Gap']   / df_merge.shift(1)['Close']
df_merge["Return"]      = df_merge.pct_change()["Adj Close"]

# 説明変数に各種移動平均と移動平均乖離率を追加
for dd in [5,10,15,20,25,30]:
  df_merge["Close " + str(dd) + "days Average"]  = df_merge["Close"].rolling(dd).mean()
  df_merge["Close " + str(dd) + "days Diverge"]  = (df_merge["Close"] - df_merge["Close " + str(dd) + "days Average"]) / df_merge["Close " + str(dd) + "days Average"]
  df_merge["Volume " + str(dd) + "days Average"] = df_merge["Volume"].rolling(dd).mean()
  df_merge["Volume " + str(dd) + "days Diverge"] = (df_merge["Volume"] - df_merge["Volume " + str(dd) + "days Average"]) / df_merge["Volume " + str(dd) + "days Average"]

# 説明変数のインデックスを表示
df_merge.info()

ソースコード全文

以下にソースコード全文を示します。

!pip install yfinance

# 必要なライブラリをインポート
import yfinance as yf
yf.pdr_override()
import numpy  as np
import pandas as pd
from   pandas_datareader import data
import datetime
from   IPython.display   import display


# 株価データの取得 ----------------------------------------------------------

# データの取得開始日・取得終了日を設定
day_start = datetime.date(2021,1,1)
day_end   = datetime.date.today() + datetime.timedelta(days=1)

# 銘柄名を変数に格納
stock_name  = ["4689.T"]

# 株価データを読み込み
df_stock = data.DataReader(stock_name, day_start, day_end, interval="1d")

# 取得する経済指標を設定
fred_name = ["^N225"]

# 経済指標データ取得
df_fred = data.DataReader(fred_name, day_start, day_end, interval="1d")

# 経済指標データの列名を編集
for i in range(len(fred_name)):
  df_fred = df_fred.add_prefix(fred_name[i] + " ")

# 株価データに経済指標データを統合
df_merge = pd.concat([df_stock, df_fred], axis=1)

# データのブランクを穴埋め
df_merge = df_merge.fillna(method='ffill')
df_merge = df_merge.fillna(0)


# 機械学習の試行 ----------------------------------------------------------

# 説明変数に株価に関する情報を追加
df_merge['Body']        = df_merge['Open']  - df_merge['Close']
df_merge['Body Ratio']  = df_merge['Body']  / df_merge['Close']
df_merge['Range']       = df_merge['High']  - df_merge['Low']
df_merge['Range Ratio'] = df_merge['Range'] / df_merge['Close']
df_merge['Gap']         = df_merge['Open']  - df_merge.shift(1)['Close']
df_merge['Gap Ratio']   = df_merge['Gap']   / df_merge.shift(1)['Close']
df_merge["Return"]      = df_merge.pct_change()["Adj Close"]

# 説明変数に各種移動平均と移動平均乖離率を追加
for dd in [5,10,15,20,25,30]:
  df_merge["Close " + str(dd) + "days Average"]  = df_merge["Close"].rolling(dd).mean()
  df_merge["Close " + str(dd) + "days Diverge"]  = (df_merge["Close"] - df_merge["Close " + str(dd) + "days Average"]) / df_merge["Close " + str(dd) + "days Average"]
  df_merge["Volume " + str(dd) + "days Average"] = df_merge["Volume"].rolling(dd).mean()
  df_merge["Volume " + str(dd) + "days Diverge"] = (df_merge["Volume"] - df_merge["Volume " + str(dd) + "days Average"]) / df_merge["Volume " + str(dd) + "days Average"]

# 説明変数のインデックスを表示
df_merge.info()

プログラムを実行し、コンソール上にデータの列名インデックスが表示されれば成功です。

目次へのリンク

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?