1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

yahoo_finance_api2で株価を取得し、グラフに描いて、保存する

Posted at

yahoo_finance_api2で株価を取得し、グラフに描いて、保存するコードです
参考にさせていただいたページ
https://myfrankblog.com/stock_price_with_yahoo_finance_api2/

import matplotlib.pyplot as plt
import japanize_matplotlib
%matplotlib inline
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
import sys
from yahoo_finance_api2 import share
from yahoo_finance_api2.exceptions import YahooFinanceError
#銘柄コード ひとまずテレビ朝日の9409を入れてみた
company_code = 9409
#銘柄コードに.Tをつける
company_code = str(company_code) + ".T"
my_share=share.Share(company_code)
finance_data=None

#株価を取得
try:
    symbol_data=my_share.get_historical(
        #週足
        share.PERIOD_TYPE_WEEK,30,
        #1日ごと
        share.FREQUENCY_TYPE_DAY,1)
except YahooFinanceError as e:
    print(e.message)

#データフレーム形式に変換してdfに格納する
df=pd.DataFrame(symbol_data)

#timestampはマイクロ秒まで含んだ時間になっている
#       timestamp    open    high     low   close  volume
#0  1602028800000  1691.0  1692.0  1671.0  1680.0  111000

#タイムスタンプを整形する
#dfのtimestamp列をdatetime形式に変換する
df['datetime']=pd.to_datetime(df['timestamp'],unit='ms')

#1602028800000 → 2020-10-07

#横軸をdatetime、縦軸close(終値)でプロットしてみる
plt.plot(df["datetime"],df["close"],label='Close',color='#99b898')
plt.title('テレビ朝日')
#プロットしたグラフを保存する
#保存するファイル名はkabuka.jpg
plt.savefig('kabuka.jpg')
plt.show()

output_4_0.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?