1#
2#  This program is free software; you can redistribute it and/or modify
3#  it under the terms of the GNU General Public License as published by
4#  the Free Software Foundation; either version 2 of the License, or
5#  (at your option) any later version.
6#
7#  This program is distributed in the hope that it will be useful,
8#  but WITHOUT ANY WARRANTY; without even the implied warranty of
9#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10#  GNU General Public License for more details.
11#
12#  A copy of the GNU General Public License is available at
13#  ../../COPYING
14
15
16################################################################################
17# FUNCTION:                 DESCRIPTION:
18#  spreads                   Computes spreads from a 'timeSeries' object
19#  midquotes                 Computes mid quotes from a 'timeSeries' object
20################################################################################
21
22
23# DW:
24# Setting bid and ask for column names is maybe the best choice. Examples
25# are the TED spread or the Libo OIS spread. The spread between High and Low
26# is the range.
27
28
29# ------------------------------------------------------------------------------
30
31
32spreads <-
33    function(x, which = c("Bid", "Ask"), tickSize = NULL)
34{
35    # A function implemented by Diethelm Wuertz
36
37    # Description:
38    #   Computes spreads from a 'timeSeries' object
39
40    # FUNCTION:
41
42    # Check arguments:
43    stopifnot(is.timeSeries(x))
44
45    # Extract Title and Documentation:
46    Title <- x@title
47    Documentation <- x@documentation
48
49    # Compute Spread:
50    spread <- x[, which[2]] - x[, which[1]]
51    if (!is.null(tickSize)) series(spread) <- round(series(spread)/tickSize)
52
53    # Preserve Title and Documentation:
54    spread@title <- Title
55    spread@documentation <- Documentation
56
57    # Return Value:
58    spread
59}
60
61
62# ------------------------------------------------------------------------------
63
64
65midquotes =
66function(x, which = c("Bid", "Ask"))
67{
68    # A function implemented by Diethelm Wuertz
69
70    # Description:
71    #   Computes mid quotes from a 'timeSeries' object
72
73    # FUNCTION:
74
75    # Check arguments:
76    stopifnot(is.timeSeries(x))
77
78    # Extract Title and Documentation:
79    Title <- x@title
80    Documentation <- x@documentation
81
82    # Compute Mid Quotes:
83    midquotes = 0.5 * ( x[, which[1]] + x[, which[2]] )
84
85    # Preserve Title and Documentation:
86    midquotes@title <- Title
87    midquotes@documentation <- Documentation
88
89    # Return Value:
90    midquotes
91}
92
93
94################################################################################
95
96