Pages

Sunday, February 19, 2012

Some auto.arima magic

Red is arima based returns, they are not as good because portfolio was not good.
So, I have ten sample stocks. I used auto.arima to pick stocks to invest in for 30 days and stored my returns in dailyReturn


##########################
# loads the data ##
##########################

library(fImport)
stockNames <- c("AAPL", "BAC", "INTC", "GOOG","DIS","DB", "EBK","BHP", "POT", "RIO")
numStock <- length(stockNames)

#adjusted columns indices
adjustedIndex=NULL
for(i in 1:length(stockNames)) adjustedIndex[i]=i*6

#downloads all the stock data
stockData=yahooSeries(stockNames, from="2007-01-01",to="2011-06-30") [,adjustedIndex]


returnsData=returns(stockData)

#
# forecast the data
#

library(forecast)
#goes through each day
dailyReturn=c()
for (i in 1103:length(returnsData[,1]))
{
dailyReturn[i-1102]=0
#goes through each return data
for(returnIndex in 1:length(returnsData[1,]))
{
fr=forecast(auto.arima(returnsData[1:(i-1),returnIndex]))[[4]][[1]]
dailyReturn[i-1102]=dailyReturn[i-1102]+max(fr,0);
}
}


#
# compares again the index
#
indexRr = c(returns(yahooSeries("^IXIC",from="2007-01-01",to="2011-06-30",frequency="d"))[,6])
indexLastMonth=indexRr[1103:length(indexRr)]
save(file='stockdata.rda',stockData,returnsData,dailyReturn,indexLastMonth)
plot(indexLastMonth)
lines(indexLastMonth)
points(dailyReturn,col=2)
lines(dailyReturn,col=2)

No comments:

Post a Comment