Python:米主要12社の相対収益率をPlotlyでグラフ化する

このサイトを参考にして、米企業12社の相対的収益率をplotlyで視覚化する。

スポンサーリンク

データの準備

先ず、必要なライブラリをインポートする。

import numpy as np
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import plot,iplot
import datetime
import matplotlib.pyplot as plt
%matplotlib inline
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf

アップル、ジョンソン&ジョンソン、マクドナルド、ネットフリックス、ウォールマート、フェースブック、ツイッター、アマゾン、グーグル、ディズニー、インテル、AMD、マイクロソフトの株価をダウンロードする。

tickers = (['AAPL','JNJ','MCD','NFLX','WMT','FB','TWTR','AMZN','GOOGL',
            'DIS','INTC','AMD','MSFT'])
stocks_start = datetime.datetime(2010, 1, 1)
stocks_end = datetime.datetime(2019, 7, 18)

def get(tickers, startdate, enddate):
    def data(ticker):
        return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
    datas = map(data, tickers)
    return(pd.concat(datas, keys=tickers, names=['Ticker', 'Date']))
               
all_data = get(tickers, stocks_start, stocks_end)
all_data.tail()
High Low Open Close Volume Adj Close
Ticker Date
MSFT 2019-07-11 139.220001 137.869995 138.199997 138.399994 22327900.0 138.399994
2019-07-12 139.130005 138.009995 138.850006 138.899994 18936800.0 138.899994
2019-07-15 139.539993 138.460007 139.440002 138.899994 16651500.0 138.899994
2019-07-16 139.050003 136.520004 138.960007 137.080002 22726100.0 137.080002
2019-07-17 137.929993 136.220001 137.699997 136.270004 20179200.0 136.270004
close = all_data[['Close']].reset_index()
close.head()
Ticker Date Close
0 AAPL 2009-12-31 30.104286
1 AAPL 2010-01-04 30.572857
2 AAPL 2010-01-05 30.625713
3 AAPL 2010-01-06 30.138571
4 AAPL 2010-01-07 30.082857
start_of_year = datetime.datetime(2017, 1, 3)
close_start = close[close['Date']>=start_of_year]
close_start.tail()
Ticker Date Close
29638 MSFT 2019-07-11 138.399994
29639 MSFT 2019-07-12 138.899994
29640 MSFT 2019-07-15 138.899994
29641 MSFT 2019-07-16 137.080002
29642 MSFT 2019-07-17 136.270004
chart_data_eval = close_start
chart_data_eval.reset_index(inplace=True)
chart_data_eval.head()
index Ticker Date Close
0 1763 AAPL 2017-01-03 116.150002
1 1764 AAPL 2017-01-04 116.019997
2 1765 AAPL 2017-01-05 116.610001
3 1766 AAPL 2017-01-06 117.910004
4 1767 AAPL 2017-01-09 118.989998
chart_data_eval_pivot = pd.pivot_table(chart_data_eval, index='Date', columns='Ticker', values = 'Close')
chart_data_eval_pivot.reset_index(inplace=True)
chart_data_eval_pivot.head()
Ticker Date AAPL AMD AMZN DIS FB GOOGL INTC JNJ MCD MSFT NFLX TWTR WMT
0 2017-01-03 116.150002 11.43 753.669983 106.080002 116.860001 808.010010 36.599998 115.839996 119.620003 62.580002 127.489998 16.440001 68.660004
1 2017-01-04 116.019997 11.43 757.179993 107.440002 118.690002 807.770020 36.410000 115.650002 119.480003 62.299999 129.410004 16.860001 69.059998
2 2017-01-05 116.610001 11.24 780.450012 107.379997 120.669998 813.020020 36.349998 116.860001 119.699997 62.299999 131.809998 17.090000 69.209999
3 2017-01-06 117.910004 11.32 795.989990 108.980003 123.410004 825.210022 36.480000 116.300003 120.760002 62.840000 131.070007 17.170000 68.260002
4 2017-01-09 118.989998 11.49 796.919983 108.360001 124.900002 827.179993 36.610001 116.279999 120.430000 62.639999 130.949997 17.500000 68.709999
chart_data_eval_pivot_relative = pd.pivot_table(chart_data_eval, index='Date', columns='Ticker', values = 'Close')
chart_data_eval_pivot_relative.tail()
Ticker AAPL AMD AMZN DIS FB GOOGL INTC JNJ MCD MSFT NFLX TWTR WMT
Date
2019-07-11 201.750000 33.060001 2001.069946 143.559998 201.229996 1144.079956 48.599998 140.110001 212.690002 138.399994 379.500000 37.209999 113.919998
2019-07-12 203.300003 33.209999 2011.000000 144.880005 204.869995 1145.339966 49.919998 134.300003 212.990005 138.899994 373.250000 37.840000 114.599998
2019-07-15 205.210007 34.389999 2020.989990 145.059998 203.910004 1150.510010 50.119999 134.710007 214.210007 138.899994 366.600006 38.680000 114.980003
2019-07-16 204.500000 33.849998 2009.900024 144.300003 203.839996 1153.459961 49.169998 132.500000 213.720001 137.080002 365.989990 37.990002 114.760002
2019-07-17 203.350006 33.599998 1992.030029 142.570007 201.800003 1146.739990 49.389999 131.860001 213.710007 136.270004 362.440002 37.700001 114.599998
chart_data_eval_pivot_relative_first = chart_data_eval_pivot_relative.iloc[0,:]
chart_data_eval_pivot_relative_first.head()
Ticker
AAPL    116.150002
AMD      11.430000
AMZN    753.669983
DIS     106.080002
FB      116.860001
Name: 2017-01-03 00:00:00, dtype: float64
chart_data_eval_pivot_relative = (chart_data_eval_pivot_relative.divide(chart_data_eval_pivot_relative_first, axis=1))-1
chart_data_eval_pivot_relative.reset_index(inplace=True)
chart_data_eval_pivot_relative.head()
Ticker Date AAPL AMD AMZN DIS FB GOOGL INTC JNJ MCD MSFT NFLX TWTR WMT
0 2017-01-03 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
1 2017-01-04 -0.001119 0.000000 0.004657 0.012821 0.015660 -0.000297 -0.005191 -0.001640 -0.001170 -0.004474 0.015060 0.025547 0.005826
2 2017-01-05 0.003960 -0.016623 0.035533 0.012255 0.032603 0.006200 -0.006831 0.008805 0.000669 -0.004474 0.033885 0.039538 0.008010
3 2017-01-06 0.015153 -0.009624 0.056152 0.027338 0.056050 0.021287 -0.003279 0.003971 0.009530 0.004155 0.028081 0.044404 -0.005826
4 2017-01-09 0.024451 0.005249 0.057386 0.021493 0.068800 0.023725 0.000273 0.003798 0.006771 0.000959 0.027139 0.064477 0.000728
chart_data_eval_pivot_relative.tail()
Ticker Date AAPL AMD AMZN DIS FB GOOGL INTC JNJ MCD MSFT NFLX TWTR WMT
633 2019-07-11 0.736978 1.892388 1.655101 0.353318 0.721975 0.415923 0.327869 0.209513 0.778047 1.211569 1.976704 1.263382 0.659190
634 2019-07-12 0.750323 1.905512 1.668277 0.365762 0.753123 0.417482 0.363934 0.159358 0.780555 1.219559 1.927681 1.301703 0.669094
635 2019-07-15 0.766767 2.008749 1.681532 0.367458 0.744908 0.423881 0.369399 0.162897 0.790754 1.219559 1.875520 1.352798 0.674629
636 2019-07-16 0.760654 1.961505 1.666817 0.360294 0.744309 0.427532 0.343443 0.143819 0.786658 1.190476 1.870735 1.310827 0.671424
637 2019-07-17 0.750753 1.939632 1.643106 0.343986 0.726853 0.419215 0.349454 0.138294 0.786574 1.177533 1.842890 1.293187 0.669094
スポンサーリンク

相対的収益率をグラフ化する

trace1 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['FB'],
    mode = 'lines',
    name = 'FB Prices')
trace2 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['AAPL'],
    mode = 'lines',
    name = 'AAPL Returns')
trace3 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['NFLX'],
    mode = 'lines',
    name = 'NFLX Returns')
trace4 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['WMT'],
    mode = 'lines',
    name = 'WMT Returns')
trace5 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['TWTR'],
    mode = 'lines',
    name = 'TWTR Returns')
trace6 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['JNJ'],
    mode = 'lines',
    name = 'JNJ Returns')
trace7 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['AMZN'],
    mode = 'lines',
    name = 'AMZN Returns')
trace8 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['GOOGL'],
    mode = 'lines',
    name = 'GOOGL Returns')
trace9 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['DIS'],
    mode = 'lines',
    name = 'DIS Returns')
trace10 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['INTC'],
    mode = 'lines',
    name = 'INTC Returns')
trace11 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['AMD'],
    mode = 'lines',
    name = 'AMD Returns')
trace12 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['MSFT'],
    mode = 'lines',
    name = 'MSFT Returns')
trace12 = go.Scatter(
    x = chart_data_eval_pivot_relative['Date'],
    y = chart_data_eval_pivot_relative['MCD'],
    mode = 'lines',
    name = 'MCD Returns')

data = [trace1,trace2,trace3,trace4,trace5,trace6,trace7,trace8, 
        trace9,trace10,trace11,trace12]

layout = dict(autosize=False,width=1050, height=1100
    , title = 'Share Price Returns by Ticker'
    , title_font=dict(size=24, family='Courier', color='black')
    , barmode = 'group'
    , hovermode= 'x'
    , yaxis=dict(title='Relative Returns',tickformat=".1%",title_font=dict(size=22)
                ,tickfont=dict(size=20))
    , xaxis=dict(title='Ticker',title_font=dict(size=22),tickfont=dict(size=20))
    , legend=dict(x=-.001,y=1,font=dict(size=21,color='white'),bgcolor='black')
    , legend_orientation="v"
    , hoverlabel=dict(font=dict(size=31))
    , plot_bgcolor='black'
    )
fig = go.Figure(data=data, layout=layout)
plot(fig,show_link=False,filename="a.html",include_plotlyjs=False)

plotlyはmatplotlibよりも使い勝手が良いが、matplotlibの方が手軽に扱えるので、個人的にはmatplotlibを使うだろう。

スポンサーリンク
スポンサーリンク