Monday, June 15, 2015

[7] User-Defined Functions in Portfolio Management

Before you read this section, please (1) create a csv (AAPL.csv) or tab-separated file (AAPL.txt) for monthly total returns of Apple and then (2) load the file into "aapl" by using read.table(), as you can see in the previous section.

Total Return

Create the following r code.

TR_F.r
# TR_F(FILE, column_date, column_total_return, separator)
#
# arguments:
#   FILE  tab-separated or csv iput data file
#   dt    data column number for yyyymmdd
#   tr    monthly total return number (%) (e.g., 1.23 in input data file or output means 1.23%)
#   sp    separator
#           ""    tab-separated
#           "csv" csv (comma separated value)


TR_F <- function(FILE,dt,tr,sp){

  i <- 1
  ctr <- 0
  OUT <- NULL


  if(sp=="csv"){
    IN = read.table(FILE, header=T, sep=",")
  } else {
    IN = read.table(FILE, header=T, sep="\t")
  }

  
  while(i<=nrow(IN)){
    ctr <- (1 + ctr) * (1 + (IN[i,tr]/100)) - 1
    i <- i+1
  }

  OUT <- ctr*100
  return(OUT)
  
}


sample code(s)
# Total return is 1,044.323%.

source("TR_F.r") TR_F("AAPL.txt",1,3,"")

[1] 1044.323


sample code(s)
TR_F("AAPL.csv",1,3,"csv")

[1] 1044.323


Monthly and Cumulative Total Return

Create the following r code.

MCTR_F.r
# MCTR_F(FILE, column_date, column_total_return, separator)
#
# arguments:
#   FILE  tab-separated or csv iput data file
#   dt    data column number for yyyymmdd
#   tr    monthly total return number (%) (e.g., 1.23 in input data file or output means 1.23%)
#   sp    separator
#           ""    tab-separated
#           "csv" csv (comma separated value)


MCTR_F <- function(FILE,dt,tr,sp){

  i <- 1
  ctr <- 0
  df <- NULL


  if(sp=="csv"){
    IN = read.table(FILE, header=T, sep=",")
  } else {
    IN = read.table(FILE, header=T, sep="\t")
  }

  
  while(i<=nrow(IN)){
    ctr <- (1 + ctr) * (1 + (IN[i,tr]/100)) - 1
    
    # cumulative monthly returns
    df <- rbind(df, data.frame(IN[i,dt],IN[i,tr],ctr*100))
   
    i <- i+1
    
  }

  return(df)
  
}




sample code(s)
source("MCTR_F.r")

MCTR_F("AAPL.txt",1,3,"")

     IN.i..dt. IN.i..tr.    ctr...100
1   2007/01/31    1.0490    1.0490000
2   2007/02/28   -1.3064   -0.2711041
3   2007/03/30    9.8097    9.5120014
4   2007/04/30    7.4158   17.6331924
5   2007/05/31   21.4339   42.8465732
6   2007/06/29    0.7005   43.8472134
7   2007/07/31    7.9646   55.3040686
8   2007/08/31    5.1002   63.2248867
9   2007/09/28   10.8247   80.8934910
10  2007/10/31   23.7701  123.8920547
11  2007/11/30   -4.0695  114.7807675
12  2007/12/31    8.7038  133.4748560
13  2008/01/31  -31.6640   59.5473776
14  2008/02/29   -7.6389   47.3597130
15  2008/03/31   14.7816   69.1418363
16  2008/04/30   21.2195  105.0328882
17  2008/05/30    8.5082  122.4774964
18  2008/06/30  -11.2901   97.3595646
19  2008/07/31   -5.0705   87.3524479
20  2008/08/29    6.6562   99.8230015
21  2008/09/30  -32.9558   33.9697328
22  2008/10/31   -5.3405   26.8150792
23  2008/11/28  -13.8675    9.2289981
24  2008/12/31   -7.8990    0.6009995
25  2009/01/30    5.6005    6.2351585
26  2009/02/27   -0.9098    5.2686310
27  2009/03/31   17.7024   23.9037052
28  2009/04/30   19.7013   48.3143459
29  2009/05/29    7.9313   60.0776016
30  2009/06/30    4.8745   67.8805843
31  2009/07/31   14.7160   92.5858910
32  2009/08/31    2.9500   98.2671748
33  2009/09/30   10.1896  118.4698069
34  2009/10/30    1.6995  122.1827012
35  2009/11/30    6.0531  135.6316423
36  2009/12/31    5.4134  148.3873256
37  2010/01/29   -8.8591  126.3824441
38  2010/02/26    6.5380  141.1833283
39  2010/03/31   14.8470  176.9918170
40  2010/04/30   11.1021  207.7437255
41  2010/05/28   -1.6125  202.7813580
42  2010/06/30   -2.0827  196.4753306
43  2010/07/30    2.2741  203.2174761
44  2010/08/31   -5.5005  186.5389988
45  2010/09/30   16.7215  234.4526175
46  2010/10/29    6.0722  254.7612494
47  2010/11/30    3.3790  266.7486320
48  2010/12/31    3.6670  280.1973043
49  2011/01/31    5.1959  299.9519761
50  2011/02/28    4.0935  316.3240102
51  2011/03/31   -1.3314  310.7810723
52  2011/04/29    0.4656  312.6936690
53  2011/05/31   -0.6569  309.9826843
54  2011/06/30   -3.4960  295.6496897
55  2011/07/29   16.3285  360.2533492
56  2011/08/31   -1.4469  353.5939435
57  2011/09/30   -0.9121  349.4567132
58  2011/10/31    6.1523  377.1086385
59  2011/11/30   -5.5783  350.4940873
60  2011/12/30    5.9655  377.3683121
61  2012/01/31   12.7111  438.0470756
62  2012/02/29   18.8311  539.3672585
63  2012/03/30   10.5284  606.6824010
64  2012/04/30   -2.5969  588.3305657
65  2012/05/31   -1.0702  580.9640520
66  2012/06/29    1.0853  588.3545548
67  2012/07/31    4.5822  619.8963372
68  2012/08/31    9.3850  687.4586085
69  2012/09/28    0.2803  689.6658550
70  2012/10/31  -10.7607  604.6922813
71  2012/11/30   -1.2196  596.0978542
72  2012/12/31   -9.0738  532.9353271
73  2013/01/31  -14.4094  441.7331441
74  2013/02/28   -2.5449  427.9465773
75  2013/03/28    0.2855  429.4538648
76  2013/04/30    0.0271  429.5973468
77  2013/05/31    2.2490  441.5079911
78  2013/06/28  -11.8303  377.4459713
79  2013/07/31   14.1225  444.8732786
80  2013/08/30    8.3772  490.5184028
81  2013/09/30   -2.1481  477.8334770
82  2013/10/31    9.6386  533.5285346
83  2013/11/29    7.0066  577.9173449
84  2013/12/31    0.8902  583.9521651
85  2014/01/31  -10.7697  510.2925687
86  2014/02/28    5.7474  545.3685238
87  2014/03/31    1.9953  558.2455620
88  2014/04/30    9.9396  623.6725379
89  2014/05/30    7.8720  680.6400400
90  2014/06/30    2.7662  702.2341048
91  2014/07/31    2.8731  725.2830929
92  2014/08/29    7.7509  789.2499602
93  2014/09/30   -1.7073  774.0677956
94  2014/10/31    7.1960  836.9657142
95  2014/11/28   10.5965  936.2512861
96  2014/12/31   -7.1891  861.7541448
97  2015/01/30    6.1424  920.8289314
98  2015/02/27   10.0746 1023.6733630
99  2015/03/31   -3.1372  988.4214822
100 2015/04/30    0.5786  994.7190889
101 2015/05/29    4.5312 1044.3230003

No comments:

Post a Comment