WTIの価格を得る
1 | pip install yfinance |
でインストール後
1 2 3 4 5 6 | import yfinance as yf # リアルタイムのWTI価格を取得 wti = yf.Ticker( "CL=F" ) price = wti. history (period= "1d" )[ 'Close' ].iloc[-1] # 最新の終値 print(f "Current WTI Price: ${price}" ) |
とすると
1 2 3 4 5 6 7 8 | [*********************100%***********************] 1 of 1 completed Open High Low Close Adj Close Volume Date 2023-01-03 80.570000 81.500000 76.599998 76.930000 76.930000 338520 2023-01-04 77.250000 77.419998 72.730003 72.839996 72.839996 352434 2023-01-05 73.250000 74.919998 72.459999 73.669998 73.669998 300731 2023-01-06 73.970001 75.470001 73.239998 73.769997 73.769997 258128 2023-01-09 73.470001 76.739998 73.470001 74.629997 74.629997 329290 |
と去年のものが出る
wti.history(period=”1d”) が1日分のデータではなく、
日次の複数日分のデータを取得しています。
これは、yfinanceが日次の過去データを返しているためです。
リアルタイムの価格を取得したい場合は、
次のように interval=”1m” を指定して、
最新の1分ごとのデータを取得することができます。
また、tail(1)で最新の行だけを取得するようにすると良い
とのことなのでコード変更
1 2 3 4 5 6 7 | import yfinance as yf # リアルタイムのWTI価格を取得(1分間隔の最新データ) wti = yf.Ticker( "CL=F" ) data = wti. history (period= "1d" , interval= "1m" ). tail (1) # 最新の1分データ price = data[ 'Close' ].iloc[-1] # 最新の終値 print(f "Current WTI Price: ${price}" ) |
これなら
1 | Current WTI Price: $75.55999755859375 |
となる
1ヶ月間のデータを取得し折線グラフで表示
yfinance を使用して過去1ヶ月間の日次データを取得し、
matplotlib を使用して折れ線グラフを描画
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import yfinance as yf import matplotlib.pyplot as plt # WTIデータを取得(1ヶ月間の日次データ) wti = yf.Ticker( "CL=F" ) data = wti. history (period= "1mo" , interval= "1d" ) # 折れ線グラフの描画 plt.figure(figsize=(12, 6)) plt.plot(data.index, data[ 'Close' ], marker= 'o' , linestyle= '-' , color= 'b' ) plt.title( 'WTI Price Trend Over Last Month' ) plt.xlabel( 'Date' ) plt.ylabel( 'WTI Price ($)' ) plt.xticks(rotation=45) plt.grid(True) plt.tight_layout() plt.show() |
これで表示される
次にスクロール機能をつける
1 | pip install plotly |
の後
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import yfinance as yf import plotly.graph_objs as go # WTIデータを取得(3年間の月次データ) wti = yf.Ticker( "CL=F" ) data_3yr = wti. history (period= "3y" , interval= "1mo" ) # Plotlyグラフの作成 fig = go.Figure() # 折れ線グラフを追加 fig.add_trace(go.Scatter(x=data_3yr.index, y=data_3yr[ 'Close' ], mode= 'lines+markers' , name= 'WTI Price' )) # グラフのレイアウト設定 fig.update_layout( title= 'WTI Price Trend Over Last 3 Years' , xaxis_title= 'Date' , yaxis_title= 'WTI Price ($)' , xaxis_rangeslider_visible=True, # スクロールバーを追加 ) # グラフを表示 fig.show() |
これだと出ない
1年にしてみる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import yfinance as yf import plotly.graph_objs as go # WTIデータを取得(1年間の月次データ) wti = yf.Ticker( "CL=F" ) data_1yr = wti. history (period= "1y" , interval= "1mo" ) # Plotlyグラフの作成 fig = go.Figure() # 折れ線グラフを追加 fig.add_trace(go.Scatter(x=data_1yr.index, y=data_1yr[ 'Close' ], mode= 'lines+markers' , name= 'WTI Price' )) # グラフのレイアウト設定 fig.update_layout( title= 'WTI Price Trend Over Last 1 Year' , xaxis_title= 'Date' , yaxis_title= 'WTI Price ($)' , xaxis_rangeslider_visible=True, # スクロールバーを追加 ) # グラフを表示 fig.show() |
これならOK