1#
2#   TTR: Technical Trading Rules
3#
4#   Copyright (C) 2007-2013  Joshua M. Ulrich
5#
6#   This program is free software: you can redistribute it and/or modify
7#   it under the terms of the GNU General Public License as published by
8#   the Free Software Foundation, either version 2 of the License, or
9#   (at your option) any later version.
10#
11#   This program is distributed in the hope that it will be useful,
12#   but WITHOUT ANY WARRANTY; without even the implied warranty of
13#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#   GNU General Public License for more details.
15#
16#   You should have received a copy of the GNU General Public License
17#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19
20#'Vertical Horizontal Filter
21#'
22#'The Vertical Horizontal Filter (VHF) attempts to identify starting and ending
23#'trends.  Developed by Adam White.
24#'
25#'The VHF is calculated by subtracting the \code{n}-period lowest low from the
26#'\code{n}-period highest high and dividing that result by the \code{n}-period
27#'rolling sum of the close price changes.
28#'
29#'@param price Object that is coercible to xts or matrix and contains a Close
30#'price series, or a High-Low-Close price series.
31#'@param n Number of periods to use.
32#'@return A object of the same class as \code{price} or a vector (if
33#'\code{try.xts} fails) containing the VHF values.
34#'@note If Close prices are given, the function calculates the max/min using
35#'only those prices (the default).  If HLC prices are given, the function
36#'calculates the max/min using the high/low prices (added for flexibility).
37#'@author Joshua Ulrich
38#'@seealso See \code{\link{aroon}}, \code{\link{CCI}}, \code{\link{ADX}},
39#'\code{\link{TDI}}, \code{\link{GMMA}} for other indicators that measure trend
40#'direction/strength.
41#'@references The following site(s) were used to code/document this
42#'indicator:\cr
43#'\url{https://www.metastock.com/Customer/Resources/TAAZ/#119}\cr
44#'@keywords ts
45#'@examples
46#'
47#' data(ttrc)
48#' vhf.close <- VHF(ttrc[,"Close"])
49#' vhf.hilow <- VHF(ttrc[,c("High","Low","Close")])
50#'
51"VHF" <-
52function(price, n=28) {
53
54  # Vertical Horizontal Filter
55
56  price <- try.xts(price, error=as.matrix)
57
58  # Calculation if price series is given
59  if(NCOL(price)==1) {
60    high  <- price
61    low   <- price
62    close <- price
63  } else
64
65  # Calculation if HLC series is given
66  if(NCOL(price)==3) {
67    high  <- price[,1]
68    low   <- price[,2]
69    close <- price[,3]
70  } else
71
72  stop("Price series must be either Close, or High-Low-Close")
73
74  # Find highest max, and lowest min of price series
75  hmax  <- runMax( high, n)
76  lmin  <- runMin(  low, n)
77  denom <- abs( momentum(close, n=1, na.pad=TRUE) )
78
79  VHF <- ( hmax - lmin ) / runSum(denom, n)
80
81  reclass(VHF, price)
82}
83