Hello All,

I have 2 problems and 1 question today.

QUESTION : Are there any book that teach modelling in R? Hopefully an
introductory book. I would like to learn how to build/backtest strategies,
so if there are any introductory books, please share.

PROBLEMS:
1) Quantstrat

I tried to use the Faber demo but changed all the symbols to just "TYC"
and I get the following errors:
   .
   .
   .
 # Process the indicators and generate trades
> start_t<-Sys.time()
> out<-try(applyStrategy(strategy=stratFaber , portfolios='faber'))
*Warning message:*
*In max(i) : no non-missing arguments to max; returning -Inf*
> end_t<-Sys.time()
   .
   .
   .
*Warning messages:*
*1: In max(i) : no non-missing arguments to max; returning -Inf*
*2: In max(i) : no non-missing arguments to max; returning -Inf*

I have no idea what they mean...

2) My code:


I know this code is wrong but I dont know where the error is. To me, it
seems right. But when I ran "XLY" through the faber demo and the code I
wrote, the end results couldnt be further from each other.
I also know that my code is highly inefficient so if there are any helpful
hints that you would like to throw my way, I am open to everything.

------------------------------------------------CODE-----------------------------------
library(quantmod)

# Get Data
getSymbols("TYC", from = "1998-01-01", to = Sys.Date())

# Convert from daily to monthly
x <- get(TYC)
x <- to.monthly(XLY, indexAt = 'lastof', drop.time= TRUE)
indexFormat(x) <- '%Y-%m-%d'
colnames(x) <- gsub("x", "XLY", colnames(x))
assign("XLY", x)

price <- as.numeric(Cl(XLY))
# Build indicators
sma10 <- SMA(price, n=10)

for(i in 1:length(price)){ buy[i] <- NA; sell[i] <- NA }
# Buy signal
buy[which(price > sma10)] <- 1
buy[which(is.na(buy))] <- 0

# Sell signal
sell[which(price < sma10)] <- -1
sell[which(is.na(sell))] <- 0

signal <- buy + sell
signal[which(is.na(signal))] <- 0

x <- NA

for(i in 1:length(signal)){
 # Start with buying not selling
if(signal[i-1] == 0 && signal[i] == -1) {
signal[i] = 0
}
}

for(i in 1:length(signal)){

if(signal[i-1] == 0 && signal[i] == 1) {
x[i] <- 1
}
if(signal[i-1] == -1 && signal[i] == 1) {
x[i] <- 1
}
if(signal[i-1] == 1 && signal[i] == -1) {
x[i] <- -1
}
}

x[which(is.na(x))] <- 0

money <- 100000 -> initEq
stock <- 0
numinP <- 0

for(i in 1:length(x)){
 if(x[i] == 1) {
p <- price[i]
b <- floor(money/p)
numinP <- numinP + b
money <- money - p*b
stock <- p*b
 }
if(x[i] == -1) {
p <- price[i]
money <- money + p*numinP
stock <- 0
numinP <- 0
}
}

total <- money + stock
print(paste("The initial equity:",initEq))
print(paste("After 10 years of trading using TAA:",total))


I appreciate all your help

-- 
Aaditya Nanduri
aaditya.nanduri@gmail.com

	[[alternative HTML version deleted]]

