リバランス有無によるリターンの比較
リバランス有無による収益率の比較を行ったから、まとめていく
使用データ
以下の銘柄のヒストリカルデータ(2019/1/4~2019/12/20)
逆相関を示して欲しかったので、輸入型企業と輸出型企業を選んでみた
データは以下のリンクよりダウンロード
株価データ・株主優待情報・先物データ・ランキングデータ・CSVダウンロード無料 | 株式投資メモ・株価データベース
新しく学んだこと
pandas.read_csv
ダウンロードしたCSVファイルを読み込む際に使用。
pandas_read_csv(読み込むファイルのパス,header,index_col,usecols)
CSVファイル内に日本語あると無理
headerで指定した行より前は無視
for文関連
pandas
で作成したデータフレーム内から複数の値を取り出したい+インデックスの番号も欲しかったとき使用。
for idx,(変数1,変数2) in enumerate(zip(df.[列名1],df.[列名2])):
matplot関連
資産比率の時系列変化を可視化する際に使用。
plt.stackplot(index,rev_result[:,np.array([7,8])].T*100,colors=list('rgbm'))
結果
色合い気持ち悪いな
作成したコードと結果
今回はいかんせん明日までやんないとやばいって感じだったから、途中のエラーとかメモできなかった、、
import pandas as pd import numpy as np import matplotlib.pyplot as plt #CSVファイル読み込み df1=pd.read_csv('data/togasu_2019.csv',header=1,index_col=0,usecols=['Date','Adj Close']) df1.columns=["togasu"] df2=pd.read_csv('data/toyota_2019.csv',header=1,index_col=0,usecols=['Date','Adj Close']) df2.columns=["toyota"] #データフレーム結合 df=pd.DataFrame([]) df=df1.join(df2,how='inner') result=np.zeros((len(df),12))#出力用リスト作成リバランスなし rev_result=np.zeros((len(df),10))#出力用リスト作成リバランスあり N=10000000000 #リバランスなし for idx,(togasu,toyota) in enumerate(zip(df.togasu,df.toyota)): result[idx,0]=togasu result[idx,1]=toyota if idx==0: units1=5000000000/togasu units2=5000000000/toyota else: units1=result[idx-1,2] units2=result[idx-1,3] volume1=togasu*units1 volume2=toyota*units2 volume=volume1+volume2 weight1=volume1/volume weight2=volume2/volume #代入 result[idx,2]=units1 result[idx,3]=units2 result[idx,4]=volume1 result[idx,5]=volume2 result[idx,6]=volume result[idx,7]=weight1 result[idx,8]=weight2 result[idx,9]=(volume-N)/N result[idx,10]=volume1*2/N-1 result[idx,11]=volume2*2/N-1 #リバランスあり diff=0.01#リバランスする乖離 Cnt=0 for idx,(togasu,toyota) in enumerate(zip(df.togasu,df.toyota)): rev_result[idx,0]=togasu rev_result[idx,1]=toyota if idx==0: units1=5000000000/togasu units2=5000000000/toyota else: units1=rev_result[idx-1,2] units2=rev_result[idx-1,3] volume1=togasu*units1 volume2=toyota*units2 volume=volume1+volume2 weight1=volume1/volume weight2=volume2/volume #リバランスするか確認 if abs(weight1-0.5)>diff: Cnt+=1 volume1=volume/2 volume2=volume/2 units1=volume1/togasu units2=volume2/toyota weight1=volume1/volume weight2=volume2/volume #代入 rev_result[idx,2]=units1 rev_result[idx,3]=units2 rev_result[idx,4]=volume1 rev_result[idx,5]=volume2 rev_result[idx,6]=volume rev_result[idx,7]=weight1 rev_result[idx,8]=weight2 rev_result[idx,9]=(volume-N)/N index=np.zeros(len(df)) index=df.index.values fig1=plt.figure(1) plt.plot(index,(rev_result[:,9]-result[:,9])*100,'k-') plt.xlabel(u'date') plt.ylabel(u'return[%]') fig4=plt.figure(4) plt.plot(index,(result[:,10])*100,'k-') plt.plot(index,(result[:,11])*100,'k--') plt.legend([u'togasu',u'toyota']) plt.xlabel(u'date') plt.ylabel(u'return[%]') fig5=plt.figure(5) plt.plot(index,rev_result[:,9]*100,'k-') plt.plot(index,(result[:,9])*100,'k--') plt.legend([u'revalance',u'buyhold']) plt.xlabel(u'date') plt.ylabel(u'return[%]') fig2=plt.figure(2) plt.stackplot(index,rev_result[:,np.array([7,8])].T*100,colors=list('rgbm')) plt.axis([index.min(),index.max(),45,55]) plt.legend([u'togasu',u'toyota']) plt.xlabel(u'date') plt.ylabel(u'weight ratio[%]') fig3=plt.figure(3) plt.stackplot(index,result[:,np.array([7,8])].T*100,colors=list('rgbm')) plt.axis([index.min(),index.max(),45,55]) plt.legend([u'togasu',u'toyota']) plt.xlabel(u'date') plt.ylabel(u'weight ratio[%]') plt.show()
結果
それぞれの資産のリターン
リバランス有りとリバランスなしの収益率比較(ありーなし)
リバランス時の2資産比率変化
追記
会社の人に見せたらアドバイスとかくれたから残しておく