R code for stock analysis