原文:Amazon Stock Analysis in Python with Yahoo Finance
範例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 pandas as pd import yfinance as yf import datetime from datetime import date, timedelta import plotly.graph_objects as go import plotly.express as px today = date.today() d1 = today.strftime("%Y-%m-%d") end_date = d1 d2 = date.today() - timedelta(days=365) d1 = d2.strftime("%Y-%m-%d") start_date = d2 data = yf.download('AMZN', start=start_date, end=end_date, progress=False) data["Date"] = data.index data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]] data.reset_index(drop=True, inplace=True) print(data.head()) |
執行結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import pandas as pd import yfinance as yf import datetime from datetime import date, timedelta import plotly.graph_objects as go import plotly.express as px today = date.today() d1 = today.strftime("%Y-%m-%d") end_date = d1 d2 = date.today() - timedelta(days=365) d1 = d2.strftime("%Y-%m-%d") start_date = d2 data = yf.download('AMZN', start=start_date, end=end_date, progress=False) data["Date"] = data.index data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]] data.reset_index(drop=True, inplace=True) print(data.head()) figure = go.Figure(data=[go.Candlestick(x=data["Date"], open=data["Open"], high=data["High"], low=data["Low"], close=data["Close"])]) figure.update_layout(title = "Amazon Stock Price Analysis", xaxis_rangeslider_visible = False) figure.show() |
執行結果:
範例3:一年長條圖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import pandas as pd import yfinance as yf import datetime from datetime import date, timedelta import plotly.graph_objects as go import plotly.express as px today = date.today() d1 = today.strftime("%Y-%m-%d") end_date = d1 d2 = date.today() - timedelta(days=365) d1 = d2.strftime("%Y-%m-%d") start_date = d2 data = yf.download('AMZN', start=start_date, end=end_date, progress=False) data["Date"] = data.index data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]] data.reset_index(drop=True, inplace=True) print(data.head()) figure = px.bar(data, x="Date", y="Close") figure.show() |
執行結果:
範例4: 使用時間區塊來瞭解某一時段的交易情形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import pandas as pd import yfinance as yf import datetime from datetime import date, timedelta import plotly.graph_objects as go import plotly.express as px today = date.today() d1 = today.strftime("%Y-%m-%d") end_date = d1 d2 = date.today() - timedelta(days=365) d1 = d2.strftime("%Y-%m-%d") start_date = d2 data = yf.download('AMZN', start=start_date, end=end_date, progress=False) data["Date"] = data.index data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]] data.reset_index(drop=True, inplace=True) print(data.head()) figure = px.line(data, x='Date', y='Close', title='Stock Market Analysis with Rangeslider') figure.update_xaxes(rangeslider_visible=True) figure.show() |
執行結果: