-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRiskParityBenchmark.R
1489 lines (1422 loc) · 59.1 KB
/
RiskParityBenchmark.R
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(BB)
library(biotools)
library(xts)
library(stringr)
#library(futile.logger) #all print() should be replaced with logging.
#DATA.ROOT <- 'D:/MyProject/R/my-investment-project/history data'
#DATA.ROOT <- 'E:/projects/rp/R/my-investment-project/history data'
DATA.ROOT <- 'D:/Nutstore/my/history data'
OUTPUT.ROOT <- 'D:/MyProject/R/my-investment-project/output'
#OUTPUT.ROOT <- 'E:/projects/rp/R/my-investment-project/output'
#OUTPUT.ROOT <- 'D:/Nutstore/my/output'
CONFIG.ROOT <- 'D:/MyProject/R/my-investment-project/cfg'
BIG.ASSET.TIME.WINDOW=5 #year
SUB.ASSET.TIME.WINDOW=3 #year
TRADING.DAYS=252
COV.COMP.THRESHOLD=1-0.95 #95%
INIT.PORTFOLIO.MONEY=100000
INIT.INDEX=10000
MIN.WINDOW.TO.START=100 #weeks. if the ts data is less than this threashold, then the data should not be included.
TRANSACTION.COST = TRUE
LOAN.COST = TRUE
REMOVE.NA = TRUE #in ts, if a date has na price, then remove it. or interpolate.
findCovMat <- function(return_matrix) {
meanv <- apply(return_matrix,2,mean)
cov_mat <- cov(return_matrix)
diag_cov_mat <- diag(cov_mat)
sdevv <- sqrt(diag(cov_mat))
list(meanv,cov_mat,diag_cov_mat,sdevv)
}
#get.log.rt <- function(prices_matrix) {#Find R: logrets:
# len <- dim(prices_matrix)[1]
# D <- dim(prices_matrix)[2]
# R = matrix(nrow=(len-1),ncol=D)
# for(i in 1:D) {
# R[,i] = 100*diff(log(prices_matrix[,i])) ###log rets
# }
# R
#}
get.log.rt <- function(prices) {#Find R: logrets:
mtx <- 100*diff(log(prices))
mtx <- na.omit(mtx)
}
###
load.all.prices <- function (label = 'all', root.path = DATA.ROOT) {
#load all the prices from the data source folder.
files <- NULL
names <- NULL
if ('all' %in% label) {
files <- list.files(path = root.path, full.names = TRUE,
recursive = FALSE, include.dirs = FALSE, pattern = "*.csv")
names <- list.files(path = root.path, full.names = FALSE,
recursive = FALSE, include.dirs = FALSE, pattern = "*.csv")
} else {
for(tmplab in label) {
tmplab <- remove.label.level(tmplab)
files <- append(files, paste(root.path, '/', tmplab, '.csv', sep=''))
names <- append(names, paste(label, '.csv', sep=''))
}
}
ts <- NULL
labs <- NULL
for (i in 1:length(files)) {
out <- tryCatch({
tmp.zoo <- read.csv.zoo(files[i], format="%Y/%m/%d", tz='GMT')
tmp.zoo
},
error=function(cond){
# try another format
tmp.zoo <- read.csv.zoo(files[i], format="%Y-%m-%d", tz='GMT')
tmp.zoo
}
)
tmp.xts <- as.xts(out)
if (i == 1) {
ts <- tmp.xts
}else {
ts <- cbind(ts, tmp.xts)
ts <- na.trim(ts)
if(REMOVE.NA) {
ts <- na.omit(ts)
} else {
ts <- na.approx(ts)
}
}
#remove the surfix -- '*.csv'
labs <- append(labs, substr(names[i], 1, (nchar(names[i])-4)))
}
colnames(ts) <- labs
#trim and interpolation NA data
#ts <- na.trim(ts)
#ts <- na.approx(ts)
#ts <- na.omit(ts)
ts
}
remove.label.level <- function(leveled.label) {
# remove the level. e.g. comm_gold.usd --> gold.usd
# the purpose for removing level is because, in prices file, no level is presented in the file name.
search.str <- '_{1}[A-Za-z0-9\\.]+$'
m <- regexpr(search.str, leveled.label)
if (m == -1) {
#no match, so no leveled information
return (leveled.label)
}
lab <- regmatches(leveled.label, m)
substr(lab, 2, nchar(lab))
}
###
get.pre.n.years.rt <- function(ts, end, n=SUB.ASSET.TIME.WINDOW, return.period='weeks') {
# default n=3, 3 years of data
# period can be:"seconds", "minutes", "hours", "days", "weeks", "months", "quarters", and "years".
# end= matrix_xts['2006-01-07/2007-01-07']
tmp.ts <- n.year.window.by.end.date(ts, end, n.years = n, to.period = return.period)
rt <- get.normal.rt(tmp.ts)
# get cor(), cov()
cov <- cov(rt)
list(rt=rt, cov=cov)
}
# cov2cor(log_rt_cov); if the correlation will be needed.
# how to simulate the excel solver to get the asset weight
# First, the cov matrix must be ready
#aa <- c(0.0025,0.0015,-0.0025,0.0015,0.0225,0.0075,-0.0025,0.0075,0.0625)
#a3cov <- matrix(aa, nrow=3, ncol=3, byrow = TRUE)
#colnames(a3cov) <- c('aa','bb','cc')
#rownames(a3cov) <- c('aa','bb','cc')
# target function:
# first calcuate the 3 assets' risk contribution
# then compute the variance of the 3 risk contribution
# the target should be that variance of the 3 risk contribution equals to ZERO
#risk.target <- function (w, cov_mtx) { # inputs are the 3 assets weights
# risk_bond <- (w[1]*w[1]*cov_mtx[1,1]+w[1]*w[2]*cov_mtx[1,2]+w[1]*w[3]*cov_mtx[1,3])*10000
# risk_stock <- (w[2]*w[2]*cov_mtx[2,2]+w[1]*w[2]*cov_mtx[1,2]+w[2]*w[3]*cov_mtx[2,3])*10000
# risk_comm <- (w[3]*w[3]*cov_mtx[3,3]+w[1]*w[3]*cov_mtx[1,3]+w[2]*w[3]*cov_mtx[2,3])*10000
# var(c(risk_bond, risk_stock, risk_comm))
#}
# set boundry: w1+w2+w3 = 1
#Amat <- matrix(c(1,1,1,1,0,0,0,1,0,0,0,1), nrow = 4, ncol = 3, byrow = TRUE)
#[,1] [,2] [,3]
#[1,] 1 1 1
#[2,] 1 0 0
#[3,] 0 1 0
#[4,] 0 0 1
#b <- c(1,0,0,0)
#meq <- 1 # the first line is equal condition: w1+w2+w3 = 1
#p0 <- c(1/3, 1/3, 1/3) # the init value for w1,w2,w3
#opt_rs <- spg(par=p0, fn=risk.target, cov_mtx= a3cov, project="projectLinear",
# projectArgs=list(A=Amat, b=b, meq=meq))
#opt_rs <- spg(par=p0, fn=optim.target, cov.mtx= a3cov, project="projectLinear",
# projectArgs=list(A=Amat, b=b, meq=meq))
exe.optim <- function (cov.mtx) {
num.asset <- nrow(cov.mtx)
labs <- rownames(cov.mtx)
# set the equal condition: w1+w2+w3 = 1
Amat.seq <- rep(1, num.asset)
b <- 1
# set the unequal condition, w1,w2,w3 > 0
for (i in 1:num.asset) {
for (j in 1:num.asset) {
if (i == j) {
Amat.seq <- append(Amat.seq, 1)
} else {
Amat.seq <- append(Amat.seq, 0)
}
}
#[,1] [,2] [,3]
#[1,] 1 1 1
#[2,] 1 0 0
#[3,] 0 1 0
#[4,] 0 0 1
b <- append(b, 0)
}
Amat <- matrix(Amat.seq, nrow = (num.asset + 1), ncol = num.asset, byrow = TRUE)
meq <- 1 # the first line is equal condition: w1+w2+w3 = 1
p0 <- rep(1/num.asset, num.asset)# the init value for w1,w2,w3...
#control <- list(M=10, maxit=50000, ftol= 1.e-10, gtol= 1.e-05, maxfeval = 10000,
# maximize = FALSE, trace = TRUE, triter=10, eps=1.e-07, checkGrad=NULL,
# checkGrad.tol= 1.e-06)
#target <- rep(1/nrow(cov.mtx), nrow(cov.mtx))
optim.rs <- BBoptim(par=p0, fn=optim.target, cov.mtx= cov.mtx, project="projectLinear",
projectArgs=list(A=Amat, b=b, meq=meq))#, control=control
print(optim.rs)
weights <- optim.rs$par
names(weights) <- labs
return(weights)
}
# this is for unequal risk parity. e.g. stock contribution != Bond contribution
optim.target1 <- function (w, cov.mtx, optim.target.values) {
num.asset <- nrow(cov.mtx)
risk.contrib <- rep(NA, num.asset)
if (missing(optim.target.values)) {
optim.target.values <- rep(1/num.asset, num.asset)
}
for(i in 1:num.asset) {
tmpsum <- 0
for (j in 1:num.asset) {
tmpsum <- tmpsum + w[i]*w[j]*cov.mtx[i,j]
}
risk.contrib[i] <- tmpsum
}
risk.contrib <- risk.contrib/sum(risk.contrib)
gap <- risk.contrib - optim.target.values
gap
}
optim.target <- function (w, cov.mtx) {
risk.contrib <- c()
num.asset <- nrow(cov.mtx)
for(i in 1:num.asset) {
tmpsum <- 0
for (j in 1:num.asset) {
tmpsum <- tmpsum + w[i]*w[j]*cov.mtx[i,j]
}
#risk.contrib[i] <- tmpsum*10000
risk.contrib[i] <- tmpsum
}
risk.var <- sd(risk.contrib/sum(risk.contrib))*num.asset*10
risk.var
}
LoadSubPortfolioCfg <- function(label) {
path <- paste(OUTPUT.ROOT, 'sub_portfolio', label, sep = '/')
path <- paste(path, 'csv', sep = '.')
pf <- read.csv(path, row.names = 1, sep = ',')
}
get.fx.lab <- function (lab) {
currency <- grep('\\.[a-z]{3}$', lab, value=TRUE)
if(length(currency) == 0) {
'cny'
} else {
tolower(substr(lab, (nchar(lab)-2), nchar(lab)))
}
}
get.exchange.rate <- function (lab, prices) {
#last 4 characters stands for foreign currency e.g. .usd
currency <- tolower(substr(lab, (nchar(lab)-2), nchar(lab)))
# hard code, since as expect there should be only at most 3~5 currency.
if (currency == 'usd') {
return(prices$usdcny)
}
if (currency == 'eur') {
return(prices$eurcny)
}
}
get.pf.value <- function(pf, latest.prices){
# pf -- volumn, it's a data.frame
# latest.prices -- should include currency pair
# return a vector with the following item:
# rmb_total, sub_usd, sub_rmb, sub_other.fx.....
labs <- rownames(pf)
currency_class <- c('rmb_total', 'sub_usd', 'sub_rmb', 'sub_eur')
total <- c(0,0,0,0)
names(total) <- currency_class
for(i in 1:length(labs)) {
lab <- labs[i]
vol.num <- pf[lab,]
price <- as.numeric(latest.prices[, lab])
which.fx <- get.fx.lab(lab)
cur_key <- paste('sub', which.fx, sep = '_')
if (which.fx != 'cny') { # it's a foreign asset, so need exchange rate
exrate <- get.exchange.rate(lab, latest.prices)
exrate <- as.numeric(exrate) #convert from ts to numeric
total[cur_key] <- total[cur_key] + price*vol.num # subtotal for fx assets
price <- price*exrate # convert to cny price
} else { # it's a cny asset
total['sub_rmb'] <- total['sub_rmb'] + price*vol.num # sub total cny assets
}
total['rmb_total'] <- total['rmb_total'] + price*vol.num # sub total cny assets
}
total
}
get.normal.rt <- function(prices) {
lag.prices <- lag(prices, 1)
normal.rt <- prices/lag.prices - 1
normal.rt <- na.omit(normal.rt)
}
LoadCSVWithLabelAsRowName <- function(bchmrk.w.file) {
# load these value from file, without lever. the content format should be:
# return a matrix with rownames equal to the first column
rec <- NULL
tryCatch({
rec <- read.csv(bchmrk.w.file, row.names = 1, stringsAsFactors = FALSE)
},
error=function(cond){
print(cond)
},
warning=function(cond){
print(cond)
}
)
return(rec)
}
is.leaf.lable <- function(lab) {
# search in the structure.csv file to check if current lable has any '_' appended,
# if so, then it is not a leaf lable.
# e.g. the input is 'comm_gold.usd'
if(lab == 'RPROOT') {
return(FALSE)
}
weights.file <- LoadCSVWithLabelAsRowName(paste(CONFIG.ROOT, 'structure.csv', sep = '/'))
available.weights.labels <- rownames(weights.file)
search.str <- paste('^',escape.weight.lable(lab), '_{1}', sep = '')
#print(search.str) # log
is.match = grepl(search.str, available.weights.labels)
if(length(which(is.match == TRUE)) > 0) {
# has matched succesfully, so still has sub labels
# thus return FALSE since it it not a leaf
return(FALSE)
}
return(TRUE)
}
escape.weight.lable <- function(lab) {
#escpae '\\|' to '\\\\|'
#str_replace_all(lab, '_', '_')
# do NOTHING now. originally, '|' is used as the level separator, but, later it turns out
# the '|' cannot be used as file name!!!
lab
}
get.sub.lables <- function(lab) {
# the input tag should contains the hirarachy information
# e.g. 'test_test1_test2'
weights.file <- LoadCSVWithLabelAsRowName(paste(CONFIG.ROOT, 'structure.csv', sep = '/'))
available.weights.labels <- rownames(weights.file)
if(lab=='RPROOT') {
return(grep('^[A-Za-z0-9\\.]+$', available.weights.labels, value = TRUE))
} else {
# e.g. when input is stock, it should search for the direct descends
# stock_sp500, stock_hs300
search.str <- paste('^',escape.weight.lable(lab), '_{1}[A-Za-z0-9\\.]+$', sep = '')
#print(search.str)
return(grep(search.str, available.weights.labels, value = TRUE))
}
return(NULL)
}
# should be deleted
#override.sub.weights <- function (w.target, w.source) {
#both are named vector
# for(lab in names(w.source)) {
# w.target[lab] <- w.source[lab]
# }
# w.target
#}
ConvertToTargetCurrency <- function(from, to, ts, currency.pair.path = DATA.ROOT){
#from/to -- 3 characters for currency e.g. CNH, USD
lab <- paste(from, to, sep = '')
currency <- NULL
is.reverse <- FALSE
tryCatch({
currency <- load.all.prices(lab, currency.pair.path)
},
error=function(cond){
print (paste('currency', lab, 'does not exist', sep=' '))
},
warning=function(cond) {
print (paste('currency', lab, 'does not exist', sep=' '))
}
)
if (is.null(currency)) {
lab <- paste(to, from, sep = '')
tryCatch(
{
currency <- load.all.prices(lab, currency.pair.path)
is.reverse <- TRUE
},
error=function(cond){
print (paste('currency', lab, 'does not exist', sep=' '))
},
warning=function(cond) {
print (paste('currency', lab, 'does not exist', sep=' '))
}
)
}
if(is.null(currency)) {
e <- simpleError(paste('currency pair', lab, 'is not available', sep = ' '))
stop(e)
}
#to compare whether ts & currency has same end date, or at least, the currency data
#should be latest enough to convert the ts
idx.cur <- index(currency)
idx.asset <- index(ts)
if(idx.cur[length(idx.cur)] < idx.asset[length(idx.asset)]) {
# the currency data is not uptodate!!!
e <- simpleError(paste('currency pair', lab, 'is not up to date', sep = ' '))
stop(e)
}
if(idx.cur[1] > idx.asset[length(1)]) {
# the currency data is not uptodate!!!
e <- simpleError(paste('currency pair', lab, 'does not have enough history data', sep = ' '))
stop(e)
}
# convert to target currency
if(is.reverse) {
currency <- 1/currency
}
combined.ts <- cbind(ts, currency)
combined.ts <- na.omit(combined.ts)
trans.ts <- combined.ts[,1]*combined.ts[,2]
colnames(trans.ts) <- c('price')
trans.ts
}
n.year.window.by.end.date <- function(ts, end.date, n.years=BIG.ASSET.TIME.WINDOW, to.period='weeks') {
time.window.day <- TRADING.DAYS*n.years
window.ts <- ts
if(!missing(end.date)){
window.ts <- ts[paste('/', end.date, sep = '')]
}
total <- length(index(window.ts))
if (total > time.window.day) {
# subset to n years of window
window.ts <- window.ts[(total-time.window.day + 1):total]
}
window.ts <- to.period(window.ts, to.period, OHLC=FALSE)
}
cov.changed <- function(ts, end.date.pre, end.date.current, time.window=BIG.ASSET.TIME.WINDOW) {
#
if(end.date.pre == end.date.current) {
return(FALSE)
}
window.new <- n.year.window.by.end.date(ts, end.date.current, n.years = time.window)
rt.new <- get.normal.rt(window.new)
rt.new <- as.matrix(rt.new) # boxM does not support xts
flag.new <- rep(1, nrow(rt.new))
window.pre <- n.year.window.by.end.date(ts, end.date.pre, n.years = time.window)
rt.pre <- get.normal.rt(window.pre)
rt.pre <- as.matrix(rt.pre)
flag.pre <- rep(2, nrow(rt.pre))
data <- rbind(rt.new, rt.pre)
group <- c(flag.new, flag.pre)
fgroup <- factor(group)
result <- boxM(as.matrix(data), fgroup)
# when 2 identical matrix were compared, we will get the following results:
# Chi-Sq (approx.) = 0, df = 15, p-value = 1, so:
# 'p value is large that we cannot reject the cov is equal'
if(result$p.value < COV.COMP.THRESHOLD) {
#so we can reject that the covs are equal
#which means the cov has been changed
return (TRUE)
}
return(FALSE)
}
load.sub.pf <- function(lab) {
# the information for sub value including 1)history net value, 2) weights
# 3) volumn, 4)std, should be already stored on the disk. or it is a init run!
out <- tryCatch({
filename <- paste(lab, 'csv', sep = '.')
path <- paste(OUTPUT.ROOT, 'sub_netvalue', filename, sep = '/')
sub.pf.value <- read.csv.zoo(path, format="%Y-%m-%d", tz='GMT')
sub.pf.value <- as.xts(sub.pf.value)
path <- paste(OUTPUT.ROOT, 'sub_portfolio', filename, sep = '/')
sub.pf.cfg <- read.csv(path, row.names = 1, sep = ',')
rs <- list(sub.pf.value, sub.pf.cfg)
names(rs) <- c('value', 'cfg')
rs
},
error=function(cond){
print(paste('sub portfolio', lab, 'does not exist', sep=' '))
print(cond)
return(NULL)
},
warning=function(cond) {
print(paste('sub portfolio', lab, 'does not exist', sep=' '))
print(cond)
return(NULL)
}
)
return(out)
}
InitRPPF <- function(label, ts, end.date, period='weeks') {
hist.window = SUB.ASSET.TIME.WINDOW
if(label == 'RPROOT') {
hist.window=BIG.ASSET.TIME.WINDOW
}
rts <- get.pre.n.years.rt(ts, end.date, n = hist.window, return.period=period)
#check the ts contains enough data.
idx <- index(rts$rt)
pos1 <- idx[1]
pos2 <- idx[length(idx)]
span.weeks <- as.numeric(pos2 - pos1, units='weeks')
if(span.weeks < MIN.WINDOW.TO.START) {
e <- simpleError(paste('Not enough data to start the process :::', label, sep = ' '))
stop(e)
}
cov.mtx <- rts$cov
weight <- exe.optim(cov.mtx)
money <- INIT.PORTFOLIO.MONEY * weight
volumn <- as.numeric(money / ts[pos1])
std <- sqrt(diag(cov.mtx))
pf.name <- colnames(rts$rt)
#w.low, w.high should not be need during init, because the init cfg will be
#written to the disk immediately.
cfg <- data.frame(pf.name, weight, volumn, std, row.names = 1)
cfg <- SaveRPPFCfg(label, cfg, format(pos1)) #save weight, volumn, std to disk, back up previous cfg if any
UpdateCovChangeDate(label, end.date) #save the init cov change date.
# consturct the ts before the run day.
first.day.pf <- zoo(INIT.PORTFOLIO.MONEY, pos1) #it's the initial money invested, defined as a constant.
first.day.pf <- as.xts(first.day.pf)
init.pf.ts <- CalcuRPTS(label, ts, first.day.pf, cfg, end.date, init.run=TRUE)
results <- list(cfg, init.pf.ts)
names(results) <- c('cfg', 'ts')
return(results)
}
SaveRPPFCfg <- function(label, cfg, end.date) {
#write a dataframe/matrix to disk
#cfg is a list of a list. outter list contains w, volumn, std
#inner list contains sub assets.
#end.date is used to log, when renaming file. the format is 2010-06-08
pf.name <- rownames(cfg)
weight <- as.numeric(cfg$weight)
volumn <- as.numeric(cfg$volumn)
std <- as.numeric(cfg$std)
# calculate weight high & low.
#[standard weight1 +/- one std(return)* w1]/ total weight,
#if the above scope is broken, then rebalance
w.std <- weight*std
w.low <- weight - w.std
w.high <- weight + w.std
df.cfg <- data.frame(pf.name, weight, volumn, std, w.low, w.high, row.names = 1)
# check if file exists, if so rename it
cfg.file <- paste(OUTPUT.ROOT, 'sub_portfolio', label, sep = '/')
cfg.file <- paste(cfg.file, 'csv', sep = '.')
if(file.exists(cfg.file)) {
# rename the existing file
rename.to <- paste(cfg.file, end.date, sep = '.')
file.rename(cfg.file, rename.to)
}
#write the cfg to a new file
write.csv(df.cfg, file = cfg.file)
return(df.cfg)
}
update.sub.pf.value <- function(label, ts) {
#write the ts to disk
# check if file exists, if so rename it
ts.file <- paste(OUTPUT.ROOT, '/sub_netvalue', label, sep = '/')
ts.file <- paste(ts.file, 'csv', sep = '.')
colnames(ts) <- c('value')
#just wirte the ts value directly to the disk. the ts contains the full values.
#so every time just overwrite is required.
write.zoo(ts, ts.file, sep = ',')
}
need.rebalance <- function(cfg, current.w){
# cfg is a data.frame
# current.w is a named vector
labs <- rownames(cfg)
for(lab in labs){
rec <- cfg[lab,]
w.low <- rec[,'w.low']
w.high <- rec[,'w.high']
w <- current.w[lab]
if(w>w.high || w<w.low){
return(TRUE)
}
}
return(FALSE)
}
rebalance <- function(cfg, sub.pf.value, current.date.ts) {
#cfg -- was loaded from file
#sub.pf.value -- current money value of the sub portfolio
#current.date.ts -- the market prices of different assets on A specifc date.
labs <- rownames(cfg)
for(lab in labs) {
w <- cfg[lab, 'weight']
money <- sub.pf.value * w
vol <- money/as.numeric(current.date.ts[, lab])
#update the cfg
print(paste('rebalance on', format(index(current.date.ts)), '--', lab, 'volumn changed by [', vol-cfg[lab, 'volumn'], ']'))
cfg[lab, 'volumn'] <- vol
}
cfg
}
CalcuRPTS <- function (parent.lab, current.sub.ts, hist.pf.ts, cfg, end.date, init.run=FALSE) {
# current.sub.ts -- contains the ts of each sub asset which consist hist.pf.ts.
# hist.pf.ts -- history net value of current level (a level higher then current.sub.ts) ts.
# cfg -- current level portfolio config info including: weight, volumn, std
# end.date -- the end date for construting the current level ts.
## First check the validities of the input data:
# 1. the last date of hist.pf.ts should be in current.sub.ts, or the current.sub.ts
# was not correctly constructed.
end.date.obj <- as.POSIXct(end.date, tz='GMT')
hist.days <- index(hist.pf.ts)
start.pos <- hist.days[length(hist.days)] #it's a date
ts.days <- index(current.sub.ts)
if (!(start.pos %in% ts.days)) {
e <- simpleError(paste(hist.days[length(hist.days)], 'is not in local ts file, please update local ts file first and then try again.', sep = ' '))
stop(e)
}
# 2. if end.date is not the last day of current.sub.ts, then an error should be raised. I use this
# strategy for simplify the process. So each time I run the procedure, the end.date should be set
# explicitly so that to make sure the ts data are uptodate.
if(end.date.obj > ts.days[length(ts.days)]) {
e <- simpleError(paste('The end date (', end.date, ') exceeds the ts range(', ts.days[length(ts.days)], ')', sep = ' '))
stop(e)
}
# 3. hist.pf.ts end date should not be later then the end.date
if(start.pos > end.date.obj) {
e <- simpleError('The end date of the history sub portfolio ts (', start.pos, ') is later than current end date (', end.date, ')')
stop(e)
}
# in this case, if rerun the same date multiple times:
if(start.pos == end.date.obj) {
return(hist.pf.ts)
}
## loop from hist.pf.ts end to end.date to construct the ts.
one.day <- as.difftime(1, units = "days")
start.pos <- start.pos+one.day # should start the process at least one day after the previous run.
ts.in.range <- current.sub.ts[paste(start.pos, end.date, sep = '/')]
current.pf.ts = NULL
labs <- rownames(cfg) # get the assets' names in this sub portfolio
for(i in 1:nrow(ts.in.range)) {
p <- ts.in.range[i]
date.obj <- index(p) # get the date obj for current date.
sub.total <- c() #store the value of each asset with names set by labs
#calculate the total value of each asset in the sub portfolio
for(lab in labs) {
#get volumn
vol <- cfg[lab, 'volumn']
#get price
p.asset <- as.numeric(p[,lab])
value <- vol*p.asset
sub.total <- append(sub.total, value)
}
names(sub.total) <- labs
sub.sum <- sum(sub.total)
# Normal Rebalance:::::::::
# [standard weight1 +/- one std(return)* w1]/ total weight, if the above scope is broken,
# then rebalance.
if(need.rebalance(cfg, sub.total/sub.sum)){
print(paste(parent.lab, ' --- rebalance happened while processing portfolio :::'))
# recalculate the volumn
cfg <- rebalance(cfg, sub.sum, p)
#update the cfg, and write the update to disk
cfg <- SaveRPPFCfg(parent.lab, cfg, date.obj)
}
# COV Change Rebalance:::::::::
# during the init run, the cov will not be changed in the init ts construction.
if(!init.run) {
# check if the cov has been changed, if so, produced the new cfg file
# for the use of next time
pre.date <- GetPreCovChangeDate(parent.lab)
# for big category such as bond, stock and commodity, use long term cycle to check the change
# of cov. for sub category such as sp500 and nasdaq, use short term cycle to check the change
window <- SUB.ASSET.TIME.WINDOW
if (parent.lab == 'RPROOT') {
window=BIG.ASSET.TIME.WINDOW
}
if(cov.changed(current.sub.ts, pre.date, format(date.obj), time.window = window)) {
print(paste(parent.lab, ' --- covariance matrix has been changed since last time'))
# need a rebalance, since the weights of each asset has been changed.
rts <- get.pre.n.years.rt(current.sub.ts, format(date.obj))
# run the excel solver like program to get the new weights.
new.weights <- exe.optim(rts$cov)
#update the new weights in cfg
for(lab in labs) {
cfg[lab, 'weight'] <- new.weights[lab]
}
cfg <- rebalance(cfg, sub.sum, p)
SaveRPPFCfg(parent.lab, cfg, date.obj) #save weight, volumn, std to disk, back up previous cfg if any
UpdateCovChangeDate(parent.lab, format(date.obj))
}
}
#store the net value of the sub portfolio into a ts
tmp.zoo <- zoo(sub.sum, index(p))
current.pf.ts = rbind(current.pf.ts, as.xts(tmp.zoo)) #convert to xts to combine.
}
#combine the history ts and new ts and return.
total.pf.ts <- rbind(hist.pf.ts, current.pf.ts)
total.pf.ts
}
UpdateCovChangeDate <- function(lab, date.str){
cov.date.file <- paste(OUTPUT.ROOT, 'sub_portfolio', 'cov_change.csv', sep = '/')
if(!file.exists(cov.date.file)){
# it is the first record in the file, so just create the file and write the single record.
rec <- data.frame(date = date.str, row.names = lab, stringsAsFactors = FALSE)
write.csv(rec, cov.date.file)
} else {
#rename file to keep the cov change records.
cov.date <- read.csv(cov.date.file, row.names = 1, stringsAsFactors = FALSE)
#bug fix. is.null not correct;;; when only one record in df, the rowname is very wired.better to us subset subset(rec, rownames(rec)=='stock')
rec.date <- subset(cov.date, rownames(cov.date)==lab)
if (nrow(rec.date)==0) {
# this record is new, so just append it into the data.frame.
rec <- data.frame(date = date.str, row.names = lab, stringsAsFactors = FALSE)
cov.date <- rbind(cov.date, rec)
write.csv(cov.date, cov.date.file)
} else {
# update the record
cov.date[lab, 'date'] <- date.str
write.csv(cov.date, cov.date.file)
}
}
}
GetPreCovChangeDate <- function(lab){
cov.date.file <- paste(OUTPUT.ROOT, 'sub_portfolio', 'cov_change.csv', sep = '/')
rec <- tryCatch(
{
cov.date <- read.csv(cov.date.file, row.names = 1, stringsAsFactors = FALSE)
cov.date
},
error=function(cond){
stop(cond)
},
warning=function(cond) {
stop(cond)
}
)
# return a date str
rec[lab, 'date']
}
CalcuRPAllocation <- function(lab = 'RPROOT', weight = 1, rec = NULL) {
if (is.leaf.lable(lab)) {
# update w.low & w.high before return.
alloc.cfg <- rec[,c('weight', 'std', 'w.low', 'w.high')]
std <- alloc.cfg[,'std']
w <- alloc.cfg[,'weight']
alloc.cfg[,'w.low'] <- w - w*std
alloc.cfg[,'w.high'] <- w + w*std
} else {
alloc.cfg <- NULL
# load sub pf file
sub.pf <- LoadSubPortfolioCfg(lab)
labs <- rownames(sub.pf)
for (sublab in labs) {
# get the sub pf weight and pass into the next lower level.
rec <- sub.pf[sublab, ]
relative.weight <- sub.pf[sublab, 'weight']
abs.weight <- weight * relative.weight
rec[, 'weight'] <- abs.weight
abs.cfg <- CalcuRPAllocation(sublab, abs.weight, rec) #recursive call.
# rbind the returns into a data.frame and return to next upper level
alloc.cfg <- rbind(alloc.cfg, abs.cfg)
}
}
return(alloc.cfg)
}
AllocateRPAssetWeight <- function (end.date, lab='RPROOT', period='weeks') {
# bottom up strategy. first run risk parity in sub category,
# then up to higher level of the category.
# !!! the hist.prices should be windowed !!!
# return type:
# - net value of a sub portfolio e.g. stock which contains china, us etc.
# end.date format : 2007-01-07
if (is.leaf.lable(lab)) {
# load prices according to the lab.
sub.pf.ts <- load.all.prices(remove.label.level(lab))
print(paste(' getting leaf asset :::', lab))
} else {
print(paste('constructing the middle/root layer portfolio for :::', lab))
convert.to <- get.fx.lab(lab)
# the current lab descendents
labs <- get.sub.lables(lab)
ts <- NULL
### construct the ts matrix from sub portfolios. e.g. stock including china stock, us stock etc.
for (sublab in labs) {
convert.from <- get.fx.lab(sublab)
############## recursive call
tmp.ts <- AllocateRPAssetWeight(end.date, sublab)
#convert to target currency
if(convert.from != convert.to) {
tmp.ts <- ConvertToTargetCurrency(convert.from, convert.to, tmp.ts)
}
# combine the different assets' ts in the same category, e.g. stock: sp500, hs300
if (is.null(ts)) {
ts <- tmp.ts
} else {
ts <- cbind(ts, tmp.ts)
ts <- na.trim(ts)
if(REMOVE.NA) {
ts <- na.omit(ts)
} else {
ts <- na.approx(ts)
}
}
}
colnames(ts) <- labs
#trim and interpolation NA data
#ts <- na.trim(ts)
#ts <- na.approx(ts)
#ts <- na.omit(ts)
### use the sub portfolio ts matrix to construct the current level portfolio net value.
sub.pf <- load.sub.pf(lab)
if(is.null(sub.pf)) {
# it's the first time run for this sub portfolio, so should initialized this portfolio
init.pf <- InitRPPF(lab, ts, end.date, period=period)
sub.pf.ts <- init.pf$ts
} else {
# cfg and net value already saved on disk
# 1. load sub portfolio ts (previous created).
# sub.pf$value sub.pf$cfg
# 2. calculate the net value start from previous end [including rebalance], and save to disk
# actually, the sub pf ts is full ts, so just need to overwrite the file on the disk
sub.pf.ts <- CalcuRPTS(lab, ts, sub.pf$value, sub.pf$cfg, end.date)
}
update.sub.pf.value(lab, sub.pf.ts) #save sub portfolio net value to disk
if(lab == 'RPROOT') {
#If it's the root, then output the standard asset (leaf) allocation information to disk.
pf.alloc <- CalcuRPAllocation()
# compare pf.alloc with the one stored on disk. use::: all.equal()
# if changed, then rename the old one and save the new one.
pre.pf.alloc.file <- paste(OUTPUT.ROOT, 'portfolio.csv', sep = '/')
pre.pf.alloc <- LoadCSVWithLabelAsRowName(pre.pf.alloc.file)
if(is.null(pre.pf.alloc) || !identical(round(pre.pf.alloc, digits = 4), round(pf.alloc, digits = 4))) {
#rename the previous created csv and save a new CSV.
if(file.exists(pre.pf.alloc.file)) {
# rename the existing file
rename.to <- paste(pre.pf.alloc.file, end.date, sep = '.')
file.rename(pre.pf.alloc.file, rename.to)
}
#write the cfg to a new file
write.csv(pf.alloc, file = pre.pf.alloc.file)
}
}
}
return(sub.pf.ts)
}
GetRPAllocForIndex <- function(end.date, lever, std.range=2) {
# in the 1st phase to get the standard asset allocation, std=1.
# in the 2nd phase to get index asset allocation, the std will be set to 2,
# which allow a broader range of fluctuation. 2 means sqrt(4) -- about a monthly std
AllocateRPAssetWeight(end.date) #calculate the reference weights allocation
path <- paste(OUTPUT.ROOT, 'portfolio.csv', sep = '/')
w <- read.csv(path, row.names = 1, sep = ',')
w[, 'weight'] <- w[, 'weight'] * lever
# std.range=2 equivalent to monthly std 67% monthes in the range?!
w[, 'w.low'] <- w[, 'weight'] - std.range * w[, 'std'] * w[, 'weight']
w[, 'w.high'] <- w[, 'weight'] + std.range * w[, 'std'] * w[, 'weight']
w
}
GetUnleveledRowName <- function(leveled.rn) {
unleveled.rn <- sapply(leveled.rn, remove.label.level)
#return a named vector: key is leveled, value is unleveled. that's very cool!!!
unleveled.rn
}
#constant lever (2X) benchmark coding.
CalcuPRIndex <- function(end.date, lever=2){
#load init data and parameters
end.date.obj <- as.POSIXct(end.date, tz='GMT')
index.cfg.file <- paste(CONFIG.ROOT, 'index_cfg.csv', sep = '/')
index.cfg <- read.csv(index.cfg.file, row.names = 1)
# get previous risk parity history ts from file. If the file does not exist,
# the it is the first time the RP Index is created.
path.index <- paste(OUTPUT.ROOT, 'rp_index', sep = '/')
rp.index.file <- paste(path.index, lever, sep = '/')
rp.index.file <- paste(rp.index.file, 'X.csv', sep = '') #..../2X.csv
vol.file <- paste(path.index, 'index_alloc.csv', sep = '/')
ts.all <- load.all.prices()
if(!file.exists(rp.index.file)) {
# init the index. end.date is the first day the index is created.
print('First time to create Risk Parity index...')
index.w <- GetRPAllocForIndex(end.date, lever) # get the weights according to lever, momentum etc...
unleveled.labs <- GetUnleveledRowName(rownames(index.w))
ts <- ts.all[, unleveled.labs]
equity <- index.cfg['init.equity',]
loan <- equity*lever - equity # loan for 2X lever
price <- ts[end.date,]
market.value <- equity * index.w[,'weight']
market.value.sum <- sum(market.value)
if(TRANSACTION.COST) {
#buy fee.
buy.fee <- market.value.sum * index.cfg['buy.commission',]
equity <- equity - buy.fee
}
if(LOAN.COST) {
#TODO:::: minus the loan interest, should be ZERO right?
}
volumn <- as.numeric(market.value / price)
#save the volumn info to disk
alloc.vol.info <- data.frame(index.w, volumn=volumn)
write.csv(alloc.vol.info, vol.file)
#save the ts -- index(equity) value, market value, loan.
equity.ts <- zoo(equity, end.date.obj)
equity.ts <- as.xts(equity.ts)
market.value.ts <- zoo(market.value.sum, end.date.obj)
market.value.ts <- as.xts(market.value.ts)
loan.ts <- zoo(loan, end.date.obj)
loan.ts <- as.xts(loan.ts)
index.ts <- cbind(equity.ts, market.value.ts, loan.ts)
colnames(index.ts) <- c('equity.value', 'market.value', 'loan')
write.zoo(index.ts, rp.index.file, sep = ',')
print('initailize RP index... DONE!!!')
} else {
# load previous created index value ts.
index.ts <- read.csv.zoo(rp.index.file, format="%Y-%m-%d",tz='GMT')
index.ts <- as.xts(index.ts)
# get the intervals (days or weeks) between the last date in the index file and end.date
hist.days <- index(index.ts)
print(paste('The RP Index ended last time at --- ', hist.days[length(hist.days)]))
start.pos <- hist.days[length(hist.days)] + as.difftime(1, units = "days")
ts.in.range <- ts.all[paste(start.pos, end.date, sep = '/')]
#index.ts.to.append <- NULL
# loop through the interval. on each day/week:
for(i in 1:nrow(ts.in.range)){
is.ref.w.changed <- FALSE
is.rebalanced <- FALSE
# 1. get the uptodate weight allocation
p <- ts.in.range[i]
date.obj <- index(p)
print(paste('calculating index on :::::::::::', date.obj))
index.w <- GetRPAllocForIndex(format(date.obj), lever) # get the weights according to lever, momentum etc...
unleveled.labs <- GetUnleveledRowName(rownames(index.w)) #here the labs may contains some new assets, e.g. some assets are removed and others are added.
p <- p[, unleveled.labs] # filter out those unused prices.
# 2. rebalance according to the lever(2X), detailed rebalance information will be recorded.
# the total portfolio should be around 200% +/- 10% (it's redundant).
# load volumn information
alloc.vol.info <- read.csv(vol.file, row.names = 1)
unleveled.labs.pre <- GetUnleveledRowName(rownames(alloc.vol.info)) # the labs from last time.
leveled.labs.pre <- names(unleveled.labs.pre)
p.with.labs.pre <- ts.in.range[i][, unleveled.labs.pre] # asset prices before adjustment (if any)
# check if the weight has been changed this time. if so the weight/volumn file needs to be updated.
if(!identical(round(index.w[, 'weight'], digits = 4),
round(alloc.vol.info[, 'weight'], digits = 4))) {
print('-- reference weights have been changed compared with last time!')
is.ref.w.changed <- TRUE
}
# -- get current market value
market.value.before.rb <- as.numeric(p.with.labs.pre * alloc.vol.info[, 'volumn'])
names(market.value.before.rb) <- names(unleveled.labs.pre) # need leveled asset name for each asset's weight
market.value.before.rb.sum <- sum(market.value.before.rb)
print(paste(' calculating current market value (before rebalance) ::: ', market.value.before.rb.sum))
# -- get current equity value
pre.index <- index.ts[nrow(index.ts)]
pre.equity <- as.numeric(pre.index[,'equity.value'])
pre.market.value.sum <- as.numeric(pre.index[,'market.value'])
pre.loan <- as.numeric(pre.index[,'loan'])
if(LOAN.COST) {
#TODO:::: minus the loan interest
#charge the interest from last time to current time, with the loan number as the principle.
}
current.equity <- pre.equity + (market.value.before.rb.sum - pre.market.value.sum)
print(paste(' calculating current market Equity ::: ', current.equity))
# -- rebalance, calculating current weight.
w.before.rebalance <- market.value.before.rb/current.equity
#print(paste('current allocation (weights) is ::::: ', w.before.rebalance))
# -- check with asset has exceeded the upper/lower limit.
loan.rebalance <- 0
commission <- 0
for(lab in names(unleveled.labs)) { # names(lab) returns the leveled lab.
rec <- index.w[lab,]
ref.w.high <- rec$w.high
ref.w.low <- rec$w.low
ref.w <- rec$weight
asset.price <- as.numeric(p[, unleveled.labs[lab]])
money.for.gap <- 0
# here the lab is uptodate, may be the lab (asset) was not included last time run (because
# the risk parity assets were adjusted)
asset.w <- as.numeric(w.before.rebalance[lab])
if(is.na(asset.w) || length(asset.w)==0) {
#this asset is newly added, so no rebalance need for this asset. just buy in directly
money.for.gap <- ref.w * current.equity
volumn.change <- money.for.gap/asset.price
print(paste('----', lab, 'is newly added in this time! weight gap :::', ref.w, '| money gap :::', money.for.gap,
'| volumn change :::', volumn.change))
#log info
log.info <- list(date=date.obj, lab=lab, current_w=0, target_w=ref.w,
w_change=(ref.w), money=money.for.gap, volumn=volumn.change, comments='newly added')
LogTradingInfo(log.info)
loan.rebalance <- loan.rebalance + money.for.gap
#update volumn info
alloc.vol.info <- rbind(alloc.vol.info, data.frame(rec, volumn=volumn.change))
# is.ref.w.changed <- TRUE ---MUST be ref weight changed!!!
} else {
# this asset is in the portfolio last time, rebalance maybe needed.
if(asset.w > ref.w.high || asset.w < ref.w.low) {
# exceed the upper/lower bond, rebalance required.
gap <- ref.w - asset.w # POSITIVE = buy asset required; NEGATIVE = sell asset required.