平均回帰戦略④
ポートフォリオ
# パッケージの呼び出し
library(quantmod)
library(urca)
# 開始日
s_date = as.Date("2011-01-01")
# 終了日
e_date = as.Date("2011-12-31")
# 終値
cl1 <- data$AUDUSD.Close
cl2 <- data$NZDUSD.Close
cl3 <- data$USDCAD.Close
cl <- window(merge(cl1,cl2,cl3),start=s_date,end=e_date)
# ヨハンセン検定
cl.ca <- ca.jo(cl)
summary(cl.ca)
######################
# Johansen-Procedure #
######################
Test type: maximal eigenvalue statistic (lambda max) , with linear trend
Eigenvalues (lambda):
[1] 0.036262039 0.014520928 0.006354046
Values of teststatistic and critical values of test:
test 10pct 5pct 1pct
r <= 2 | 1.64 6.50 8.18 11.65
r <= 1 | 3.77 12.91 14.90 19.19
r = 0 | 9.53 18.90 21.07 25.75
Eigenvectors, normalised to first column:
(These are the cointegration relations)
AUDUSD.Close.l2 NZDUSD.Close.l2 USDCAD.Close.l2
AUDUSD.Close.l2 1.0000000 1.0000000 1.000000
NZDUSD.Close.l2 -0.3569325 -0.9682793 1.079648
USDCAD.Close.l2 0.3318299 0.8359184 3.245736
Weights W:
(This is the loading matrix)
AUDUSD.Close.l2 NZDUSD.Close.l2 USDCAD.Close.l2
AUDUSD.Close.d -0.06419504 0.02332732 -0.006733601
NZDUSD.Close.d -0.01536753 0.02484012 -0.007298684
USDCAD.Close.d 0.02591722 -0.02973041 0.001493109
# ポートフォリオの市場価格
cl <- merge(cl1,cl2,cl3)
cl$NZDUSD.Close <- cl$NZDUSD.Close*(-0.3569325)
cl$USDCAD.Close <- cl$USDCAD.Close*0.3318299
port <- cl$AUDUSD.Close+cl$NZDUSD.Close+cl$USDCAD.Close
# ポートフォリオの変化率
deltaport <- diff(port,lag=1)/lag(port,k=1)*100
# 結果を格納するオブジェクト
result <- matrix(0,nrow=50,ncol=3)
colnames(result) <- c("lookback","totalret","r2")
# バックテスト
s_date = as.Date("2012-01-01")
e_date = as.Date("2012-12-31")
for(i in 1:50) {
# 計算期間
lookback <- 5*i
# 平均
avg <- rollapply(port,lookback,mean)
# 標準偏差
std <- rollapply(port,lookback,sd)
# ユニット数
unit <- -(port-avg)/std
# 損益
ret <- deltaport*lag(unit,k=1)
# 累積損益
totalret <- cumsum(window(ret,start=s_date,end=e_date))
# R2
r2 <- cor(totalret,1:length(totalret))^2
# 結果の記録
result[i,1] <- lookback
result[i,2] <- last(totalret)
result[i,3] <- r2
}
# 結果
result
lookback totalret r2
[1,] 5 4.7130055 0.385004702
[2,] 10 1.6088589 0.015589341
[3,] 15 -2.4208530 0.596292866
[4,] 20 -3.5896849 0.741894714
[5,] 25 -4.8989745 0.808140454
[6,] 30 -6.3599819 0.835377126
[7,] 35 -5.2685385 0.745345377
[8,] 40 -4.0930187 0.628684819
[9,] 45 -3.2869785 0.501720100
[10,] 50 -2.4054102 0.326525713
[11,] 55 -1.5280476 0.213036578
[12,] 60 -0.1837137 0.029416686
[13,] 65 0.6892384 0.001460319
[14,] 70 1.5804176 0.092670458
[15,] 75 2.3396546 0.231172574
[16,] 80 2.9336058 0.340102499
[17,] 85 3.5290072 0.428199852
[18,] 90 4.3217031 0.550503581
[19,] 95 4.7420622 0.644241320
[20,] 100 4.8815130 0.687816786
[21,] 105 5.2385054 0.741179755
[22,] 110 5.3084665 0.755824274
[23,] 115 5.3357395 0.773592207
[24,] 120 5.5344965 0.811070580
[25,] 125 5.8477125 0.840373099
[26,] 130 6.2216622 0.866089137
[27,] 135 6.2207503 0.867675879
[28,] 140 6.0634062 0.860704211
[29,] 145 5.8588285 0.851133480
[30,] 150 5.9012934 0.853695817
[31,] 155 6.3005218 0.879142467
[32,] 160 6.6509243 0.899998642
[33,] 165 7.0565007 0.912589342
[34,] 170 7.3726229 0.918909332
[35,] 175 7.4069970 0.920446471
[36,] 180 7.2715744 0.919181048
[37,] 185 7.1998703 0.919231209
[38,] 190 7.2815878 0.920502527
[39,] 195 7.3245762 0.922675103
[40,] 200 7.3775627 0.924826485
[41,] 205 7.3049591 0.923260350
[42,] 210 7.0839901 0.919101565
[43,] 215 6.8471007 0.913832085
[44,] 220 6.6254838 0.907550847
[45,] 225 6.5119891 0.903199990
[46,] 230 6.4418873 0.898914504
[47,] 235 6.3240526 0.894055109
[48,] 240 6.3031367 0.889887800
[49,] 245 6.2236103 0.884807896
[50,] 250 6.1417746 0.880090381
# グラフ
lookback <- 175
avg <- rollapply(port,lookback,mean)
std <- rollapply(port,lookback,sd)
unit <- -(port-avg)/std
ret <- deltaport*lag(unit,k=1)
totalret <- cumsum(window(ret,start=s_date,end=e_date))
matplot(cumret,type="l")