#!/usr/bin/env python # coding: utf-8 # In[1]: import pandas as pd import pandas_datareader.data as web import pandas_datareader as pdr import os import numpy as np from bokeh.io import output_notebook from bokeh.plotting import figure, show from bokeh.palettes import Spectral8 # 色を考えなくても良い 地味に素敵 https://bokeh.pydata.org/en/latest/docs/reference/palettes.html from datetime import datetime from datetime import timedelta get_ipython().run_line_magic('matplotlib', 'inline') output_notebook() # In[2]: start = datetime(2008, 9, 11) end = datetime(2018, 2, 9) mstar_spx = web.DataReader('SPX', 'morningstar', start, end) # In[3]: mstar_spx.head() # In[4]: mstar_spx.tail() # In[5]: mstar_spx2 = web.DataReader('SPX', 'morningstar', '1900-01-01') # In[6]: mstar_spx2.head() # ### 株価以外のデータ・ソース # * Enigma / https://public.enigma.com/ # API KEYがいるみたいなのでパス # * FRED / https://fred.stlouisfed.org/ # セントルイス連銀が出しているデータ # * WORLD BANK / https://data.worldbank.org/ # * OECD / http://stats.oecd.org/ # * Eurostats / http://ec.europa.eu/eurostat/ # # 色々とあります。データありすぎ問題・・・ # In[7]: # FRED のデータを取得してみる。注目されている米国金利 FF、2年、10年、30年 start_f = datetime(1980,1,1) end_f = datetime(2018,2,9) # FF https://fred.stlouisfed.org/series/FF 2年 https://fred.stlouisfed.org/series/GS2 # 10年 https://fred.stlouisfed.org/series/DGS10 30年 https://fred.stlouisfed.org/series/DGS30 # Dがつくとdailyのよう。つけないとmonthly f_ff = web.DataReader('FF', 'fred', start_f, end_f) f_2 = web.DataReader('GS2', 'fred', start_f, end_f) f_10 = web.DataReader('GS10', 'fred', start_f, end_f) f_30 = web.DataReader('GS30', 'fred', start_f, end_f) # In[8]: f_2.plot() # In[9]: f_10.plot() # In[10]: f_30.plot() # In[11]: f_ff.plot() # In[12]: # 負債の状況 自動車ローン https://fred.stlouisfed.org/series/MVLOAS f_car = web.DataReader('MVLOAS', 'fred', start_f, end_f) # In[13]: f_car.plot() # In[14]: # 不動産ローン f_re = web.DataReader('RHEACBW027SBOG', 'fred', start_f, end_f) f_re.plot() # In[15]: # 全産業ローン f_loan = web.DataReader('BUSLOANS', 'fred', start_f, end_f) f_loan.plot() # In[16]: # FED TOTAL ASSET f_fed_asset = web.DataReader('WALCL', 'fred', start_f, end_f) f_fed_asset.plot() # In[17]: f_fed_asset['2014':].plot() # In[18]: # BOJ TOTAL ASSET f_boj = web.DataReader('JPNASSETS', 'fred', start_f, end_f) f_boj.plot() # In[19]: # World Bank from pandas_datareader import wb # In[20]: mat = wb.search('elect') # In[21]: mat # In[22]: dat = wb.download(indicator='EG.ELC.ACCS.UR.ZS') # In[23]: dat # In[24]: eu = web.DataReader('teimf060', 'eurostat', start_f) # In[25]: eu # In[26]: eu2 = web.DataReader('teimf050', 'eurostat', '1900') # In[27]: eu2 # In[28]: eu2.columns # ## pandas_datareaderを使って分析を作ってみる # * FRB議長が交代する時に、新議長を試すために市場が荒れることがよくあるそうな。 # * 幾つかの指標を使ってみてみます! # * 参考  wikipedia 連邦準備制度 https://ja.wikipedia.org/wiki/%E9%80%A3%E9%82%A6%E6%BA%96%E5%82%99%E5%88%B6%E5%BA%A6 # In[29]: chair_p = {} chair_p = pd.DataFrame(chair_p) chair_p['name'] = ['Charles Hamlin', 'William Harding', 'Daniel Crissinger', 'Roy Young', 'Eugene Meyer', \ 'Eugene Black', 'Marriner Eccles', 'Thomas McCabe', 'William Martin', 'Authur Burns', \ 'William Miller', 'Paul Volker', 'Alan Greenspan', 'Ben Bernanke', 'Janet Yellen', 'Jerome Powell'] chair_p['start'] = [datetime(1914,8,10), datetime(1916,8,10), datetime(1923,5,1), datetime(1927,10,4), \ datetime(1930,9,16), datetime(1933,5,19), datetime(1934,11,15), datetime(1948,4,15), \ datetime(1951,4,2), datetime(1970,2,2), datetime(1978,3,8), datetime(1979,8,6), \ datetime(1987,8,11), datetime(2006,2,1), datetime(2014,2,3), datetime(2018,2,5)] chair_p['end'] = [datetime(1916,8,10), datetime(1922,8,9), datetime(1927,9,15), datetime(1930,8,30), \ datetime(1933,5,10),datetime(1934,8,15), datetime(1948,1,31), datetime(1951,3,31), \ datetime(1970,1,31), datetime(1978,1,31), datetime(1979,8,6), datetime(1987,8,11), \ datetime(2006,1,31), datetime(2014,1,31), datetime(2018,2,5), '-'] # In[30]: chair_p # In[31]: chair_p['before_365'] = 0 chair_p['after_365'] = 0 # In[32]: for i in range(len(chair_p)): a = chair_p.iloc[i, 1] - timedelta(days=365) b = chair_p.iloc[i, 1] + timedelta(days=365) chair_p.iloc[i, 3] = '{}-{}'.format(a.year, a.month) chair_p.iloc[i, 4] = '{}-{}'.format(b.year, b.month) # In[33]: chair_p # In[34]: spx = web.DataReader('^SPX', 'stooq') spx = spx.sort_index() # In[35]: spx.loc['2014-02-03'] # In[36]: spx.head() # ## S&P500指数の1941年からの算出なのになんてこった!! # https://ja.wikipedia.org/wiki/S%26P_500 # In[37]: spx = spx['1941':] # In[38]: spx.head() # In[39]: spx7 = spx[chair_p.iloc[7,3]:chair_p.iloc[7,4]] / spx.loc[chair_p.iloc[7,1], 'Close'] * 100 spx8 = spx[chair_p.iloc[8,3]:chair_p.iloc[8,4]] / spx.loc[chair_p.iloc[8,1], 'Close'] * 100 spx9 = spx[chair_p.iloc[9,3]:chair_p.iloc[9,4]] / spx.loc[chair_p.iloc[9,1], 'Close'] * 100 spx10 = spx[chair_p.iloc[10,3]:chair_p.iloc[10,4]] / spx.loc[chair_p.iloc[10,1], 'Close'] * 100 spx11 = spx[chair_p.iloc[11,3]:chair_p.iloc[11,4]] / spx.loc[chair_p.iloc[11,1], 'Close'] * 100 spx12 = spx[chair_p.iloc[12,3]:chair_p.iloc[12,4]] / spx.loc[chair_p.iloc[12,1], 'Close'] * 100 spx13 = spx[chair_p.iloc[13,3]:chair_p.iloc[13,4]] / spx.loc[chair_p.iloc[13,1], 'Close'] * 100 spx14 = spx[chair_p.iloc[14,3]:chair_p.iloc[14,4]] / spx.loc[chair_p.iloc[14,1], 'Close'] * 100 spx15 = spx[chair_p.iloc[15,3]:chair_p.iloc[15,4]] / spx.loc[chair_p.iloc[15,1], 'Close'] * 100 # In[40]: spx7.index = spx7.index - chair_p.iloc[7,1] spx8.index = spx8.index - chair_p.iloc[8,1] spx9.index = spx9.index - chair_p.iloc[9,1] spx10.index = spx10.index - chair_p.iloc[10,1] spx11.index = spx11.index - chair_p.iloc[11,1] spx12.index = spx12.index - chair_p.iloc[12,1] spx13.index = spx13.index - chair_p.iloc[13,1] spx14.index = spx14.index - chair_p.iloc[14,1] spx15.index = spx15.index - chair_p.iloc[15,1] # In[41]: p = figure(width=700, height = 500, title='FRB議長交代1年前後の株価の動き') p.line(spx7.index, spx7.Close, legend='McCabe', line_color=Spectral8[0]) p.line(spx8.index, spx8.Close, legend='Martin', line_color=Spectral8[1]) p.line(spx9.index, spx9.Close, legend='Burns', line_color=Spectral8[2]) p.line(spx10.index, spx10.Close, legend='Miller', line_color=Spectral8[3]) p.line(spx11.index, spx11.Close, legend='Volker', line_color=Spectral8[4]) p.line(spx12.index, spx12.Close, legend='Greenspan', line_color=Spectral8[5]) p.line(spx13.index, spx13.Close, legend='Bernanke',line_color=Spectral8[6]) p.line(spx14.index, spx14.Close, legend='Yellen', line_color=Spectral8[7]) p.line(spx15.index, spx15.Close, legend='Powell', line_color='black', line_width=3) p.legend.location = 'bottom_left' show(p) # In[ ]: