Closed aju1987b closed 8 years ago
Please provide a reproducible example and the output from sessionInfo()
.
The code is below, where we are doing a sort of parameter sweep between n_short1
(n_long1
) and n_short2
(n_long2
).
HMAB <- function(f,n_short1,n_short2,n_long1,n_long2,n_band) {
z = read.csv(f) [,1:12]
require("TTR")
require("quantmod")
require("WriteXLS")
z_data = z[,"Last"]
nob = length(z_data)
ret = (z_data[2:nob])-(z_data[1:(nob-1)])
v_short = vector()
v_long = vector()
v_band = vector()
v_no.buys = vector()
v_no.sells = vector()
for (n_short in n_short1:n_short2)
{
for (n_long in n_long1:n_long2)
{
short = HMA(z_data,n_short,500)
long = HMA(z_data,n_long,500)
signup = ifelse(short>=long*n_band,1,0)
signdn = ifelse(long>short*n_band,-1,0)
sign = signup+signdn
sign = Lag(sign)
sign = sign[-1]
sign[is.na(sign)]=0
returns = sign*ret
returns[is.na(returns)] = 0
up = ifelse(sign==1,1,0)
dn = ifelse(sign==-1,1,0)
no.buys = sum(up)
no.sells = sum(dn)
v_short = append(v_short, n_short)
v_long = append(v_long, n_long)
v_band = append(v_band,n_band)
v_no.buys = append(v_no.buys, no.buys)
v_no.sells = append(v_no.sells, no.sells)
}
}
df = data.frame(
"short" = v_short,
"long" = v_long,
"band" = v_band,
"n(buys)" = v_no.buys,
"n(sells)" = v_no.sells)
file_name = paste("HMA1",f,n_short1,n_short2,n_long1,n_long2,n_band,".xls")
WriteXLS("df", ExcelFileName = file_name, SheetNames = NULL,
row.names = FALSE, col.names = TRUE)
}
The session output is;
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] WriteXLS_4.0.0 quantmod_0.4-5 xts_0.9-7 zoo_1.7-12
[5] TTR_0.23-1
loaded via a namespace (and not attached):
[1] tools_3.2.0 grid_3.2.0 lattice_0.20-31
I should have emphasized minimal when I asked for a reproducible example...
Regardless, you're passing 3 arguments to HMA
, which only has 2 arguments (x
and n
). What do you expect the third argument to do?
What happens is that third arguments is passed to the WMA
calls inside HMA
, and it's interpreted as the value for wts
. Since 500
is a vector of length = 1, and your data have more than one observation, you get the error.
Hi,
Firstly, thanks for the TTR package, it is very useful. I written a code to compute various TTRs and output the results into a nice excel file. For the
SMA
,EMA
,WMA
, the code works fine. However, when I try to use theHMA
, I keep getting the following error:My code is:
Where
z_data
is a price series whilen_short
andn_long
are parameters between two values specified in the code. So to confirm, when theHMA
is changed toSMA
/WMA
/EMA
, the code works fine. I have added other inputs to the code such as:short = HMA(z_data,n_short,500)
andshort = HMA(z_data,n_short,"length of data")
, but the error remains.I think I am missing something pretty simple. Any ideas most welcome.
Many thanks.