File: 1B-BenchmarkAnalysis.R

package info (click to toggle)
fmultivar 240.10068-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,492 kB
  • ctags: 272
  • sloc: fortran: 1,128; ansic: 764; sh: 22; makefile: 1
file content (263 lines) | stat: -rw-r--r-- 7,717 bytes parent folder | download
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

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA

# Copyrights (C)
# for this R-port: 
#   1999 - 2006, Diethelm Wuertz, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   info@rmetrics.org
#   www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
#   see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
#   see Rmetrics's copyright file


################################################################################
# FUNCTION:                 BENCHMARK ANALYSIS FUNCTIONS:
#  getReturns                Computes return series given a price series
# FUNCTION:                 DRAWDOWNS:
#  maxDrawDown               Computes the maximum drawdown
# FUNCTION:                 PERFORMANCE RATIOS:
#  sharpeRatio               Calculates the Sharpe Ratio
#  sterlingRatio             Calculates the Sterling Ratio
# FUNCTION:                 OHLC PLOT:
#  ohlcPlot                  Creates a Open-High-Low-Close plot
################################################################################


getReturns = 
function(x, type = c("continuous", "discrete"), percentage = FALSE, 
trim = TRUE)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes returns given a price series, for SPlus compatibility.
    
    # Arguments:
    #   type - a character string specifying the type of returns to be 
    #       computed. Valid choices are: "continuous" and "discrete". 
    #       If type="continuous", the returns are calculated as the 
    #       logarithm differences; if type="discrete", the returns are 
    #       calculated as percentage changes. The default is "continuous" 
    #   percentage - a logical flag. If TRUE, the return series will be 
    #       expressed in percentage points. The default is FALSE. 
    #   trim - a logical flag. If TRUE, the first missing observation 
    #       in the return series will be removed. The default is TRUE. 

    # Note:
    #   Function for S-Plus Compatibility
    #   getReturns(x, type = "continuous", percentage = FALSE, trim = TRUE) 
    
    # FUNCTION:
    
    # Check Type:
    type = match.arg(type)
    
    # Return Series:
    ans = returnSeries(x = x, type = type, percentage = percentage, 
        trim = trim, digits = 12)
        
    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


maxDrawDown = 
function(x)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes the maximum drawdown
    
    # FUNCTION:
    
    # Check for timeSeries Object:
    TS = is.timeSeries(x)
    if (TS) {
        positions = x@positions
        x = seriesData(x)
        x = removeNA(x)
    }

    # Check for Univariate Series:
    if (NCOL(x) > 1) {
        stop("x is not a vector or univariate timeSeries")
    }
    
    # Check for NAs:
    if(any(is.na(x))) {
        stop("NAs in x")
    }
    
    cmaxx = cummax(x)-x
    mdd = max(cmaxx)
    to = which(mdd == cmaxx)
    from = double(NROW(to))
    for (i in 1:NROW(to))
        from[i] = max(which(cmaxx[1:to[i]] == 0))   
     
    # For time Series objects:   
    if (TS) {
        from = positions[from]
        to = positions[to]
    }
    
    # Result:
    ans = list(maxdrawdown = mdd, from = from, to = to)
        
    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


sharpeRatio = 
function(x, r = 0, scale = sqrt(250))
{   # A function implemented by Diethelm Wuertz

    # Notes:
    #   A copy from A. Traplettis "tseries" package
    
    # FUNCTION:
    
    # Check for timeSeries Object:
    if (is.timeSeries(x)) x = removeNA(seriesData(x))
    
    # Check for Univariate Series:
    if (NCOL(x) > 1) stop("x is not a vector or univariate time series")
        
    # Check for NAs:
    if(any(is.na(x))) stop("NAs in x")
     
    # Sharpe Ratio:
    if (NROW(x) == 1) {
        return(NA)
    } else {
        y = diff(x)
        return(scale * (mean(y)-r)/sd(y))
    }
    
    # Return Value:
    invsible()
}


# ------------------------------------------------------------------------------


sterlingRatio = 
function(x)
{   # A function implemented by Diethelm Wuertz

    # Notes:
    #   A copy from A. Traplettis "tseries" package
    
    # FUNCTION:
    
    # Check for timeSeries Object:
    TS = is.timeSeries(x)
    if (TS) {
        Unit = x@units
        x = seriesData(x)
        x = removeNA(x)
    }
        
    # Check for Univariate Series:
    if(NCOL(x) > 1) stop("x is not a vector or univariate time series")
        
    # Check for NAs:
    if(any(is.na(x))) stop("NAs in x")
        
    # Sterling Ratio:
    if (NROW(x) == 1) {
        return(NA)
    } else {
        ans = (x[NROW(x)]-x[1]) / maxDrawDown(x)$maxdrawdown
        if (TS) names(ans) = Unit
        return(ans)
    }
    
    # Return Value:
    invsible()
}


# ------------------------------------------------------------------------------


ohlcPlot = 
function(x, xlim = NULL, ylim = NULL, xlab = "Time", ylab, col = par("col"), 
bg = par("bg"), axes = TRUE, frame.plot = axes, ann = par("ann"), main = NULL,
date = c("calendar", "julian"), format = "%Y-%m-%d",
origin = "1899-12-30", ...)
{   # A function implemented by Diethelm Wuertz

    # Notes:
    #   A copy from A. Traplettis 'tseries' package
    
    # FUNCTION:
    
    # Checks:
    if ((!is.mts(x)) || (colnames(x)[1] != "Open") ||
        (colnames(x)[2] != "High") || (colnames(x)[3] != "Low") ||
        (colnames(x)[4] != "Close"))
        stop("x is not a open/high/low/close time series")
    xlabel = if (!missing(x)) deparse(substitute(x)) else NULL
    if (missing(ylab)) ylab = xlabel
    date = match.arg(date)
    time.x = time(x)
    dt = min(lag(time.x)-time.x)/3
    ylim = c(min(x, na.rm = TRUE), max(x, na.rm = TRUE))
    if (is.null(xlim)) xlim = range(time.x)
    if (is.null(ylim)) ylim = range(x[is.finite(x)])
    plot.new()
    plot.window(xlim, ylim, ...)
    for (i in 1:NROW(x)) {
        segments(time.x[i], x[i,"High"], time.x[i], x[i,"Low"],
            col = col[1], bg = bg)
        segments(time.x[i] - dt, x[i,"Open"], time.x[i], x[i,"Open"],
            col = col[1], bg = bg)
        segments(time.x[i], x[i,"Close"], time.x[i] + dt, x[i,"Close"],
            col = col[1], bg = bg)
    }
    if (ann) title(main = main, xlab = xlab, ylab = ylab, ...)  
    if (axes) {
        if (date == "julian") {
            axis(1, ...)
            axis(2, ...)
        } else {
            n = NROW(x)
            lab.ind = round(seq(1, n, length=5))
            D = as.vector(time.x[lab.ind]*86400) + 
                as.POSIXct(origin, tz = "GMT")
            DD = format.POSIXct(D, format = format, tz ="GMT")
            axis(1, at=time.x[lab.ind], lab=DD, ...)
            axis(2, ...)
        }
    }
    if (frame.plot) box(...)
}


################################################################################