load("c:/temp/ibmISannual.RData")
is<-.ibmISannual
load("c:/temp/ibmBSannual.RData")
bs<-.ibmBSannual
#
getValue<-function(dataSet,phrase){
  aa<- current_a<-dataSet[grep(phrase,rownames(dataSet)),] 
  f<-as.numeric(as.character(unlist(aa)))
  n<-dim(aa)
  final<-data.frame(matrix(f,n))
  colnames(final)<-colnames(aa)
  rownames(final)<-rownames(aa)
  return(final)
}
#
current_a<-getValue(bs,'Total Current Assets')
current_l<-getValue(bs,'Total Current Liabilities') 
working_capital<-current_a-current_l
NI<-getValue(is,'Net Income')
NI2<-    NI[nchar(rownames(NI))<=13,]
Assets<-getValue(bs,'Total Assets')
IBT<-getValue(is,'^Income Before Tax')
Interest<-getValue(is,'Interest Expense(Income) - Net Operating')
total_liab<-getValue(bs,'Total Liabilities') 
total_liab2<-total_liab[nchar(rownames(total_liab))<=20,] 
sales<-getValue(is,'Total Revenue')
#
market_cap<-132.835*10^9
#
x1<-working_capital/Assets
x2<-NI2/Assets
x3<-IBT/Assets
x4<-market_cap/(total_liab2*1000)
x5<-sales/Assets
z<-1.2*x1+ 1.4*x2+ 3.3*x3+0.6*x4+0.999*x5












a<-bs[grep('Total Current Assets',bs$Period.Ending),] 
current_a<-as.numeric(as.matrix(a[2:4]))

l<-bs[grep('Total Current Liabilities',bs$Period.Ending),] 
current_l<-as.numeric(as.matrix(l[2:4]))
working_capital<-current_a-current_l
NI<-is[grep('Net Income',is[,1]),]

NI2<-NI[nchar(NI[,1])<=13,]


   Assets<-bs[grep('Total Assets',rownames(bs)),] 
   IBT<-   is[grep('^Income Before Tax',rownames(is)),]
   Interest<-   is[grep('Interest Expense(Income) - Net Operating',rownames(is)),]
   total_liab<-bs[grep('Total Liabilities',rownames(bs)),] 
   total_liab2<-total_liab[nchar(rownames(total_liab))<=20,] 
   sales<-is[grep('Total Revenue',rownames(is)),]

   loc<-paste("http://finance.yahoo.com/q?s=",ticker,"&ql=1",sep="")
   market_cap<-readHTMLTable(loc)[[2]][4,2]

   market_cap2<-gsub("[^0-9\\.]","",market_cap)

   x1<-working_capital/Assets
   x2<-NI2/Assets
   x3<-IBT/Assets
   x4<-as.numeric(market_cap2)*1000/total_liab2
   x5<-sales/Assets

   #return(0.012*x1+ 0.014*x2+ 0.033*x3+0.006*x4+0.999*x5)
   return(1.2*x1+ 1.4*x2+ 3.3*x3+0.6*x4+0.999*x5)
}


