Pages

Showing posts with label quantmod. Show all posts
Showing posts with label quantmod. Show all posts

Saturday, September 2, 2017

R code to do Basic Stock Analysis

library(quantmod)
require(quantmod)

#! to disable warnings
options("getSymbols.yahoo.warning"=FALSE)

#! add list of stocks which you want to review
stocklist <- c("SBIN.NS","ITC.NS","MARUTI.NS")
j=1
RTable= array(c(stocklist,1:10),c(length(stocklist),2))

#! get the data for all the stocks and perform the basic calculations
for (i in stocklist)
{
test<-as.data.frame(getSymbols(Symbols = i,from ='2015-01-01',to = Sys.Date(), env = NULL))
ptest <- test[,6]
RTable[j,2] = as.numeric(ptest[length(ptest)] / ptest[1])
j = j+1
}

#! plot the output
print (RTable)
barplot(type.convert(RTable[,2]),names.arg = RTable[,1])


Sunday, August 27, 2017

Process Command Line Arguments in R


We can further extend the use of R by passing command line arguments to functions the way we do in multiple programming languages.

You will have to create a script in R which uses the arguments to process the data, we will use below script for example-

library(quantmod)
require(quantmod)



#! To Enable Command Line Arguments
args <- commandArgs(TRUE)


#! Using the Argument Value & Processing Data
symbolsHUL<-c(args[1])
getSymbols(symbolsHUL)
startT <- "2016-01-01"
endT <- "2017-01-01"
rangeT <- paste(startT,"::",endT,sep="")



#! We are still using Static Data here which will be fixed in next post
HUL <- HINDUNILVR.NS[,6][rangeT]
plot(HUL) 


  • Save the above script as Stock.R in working directory
  • Run the below command from Command line
Rscript Stock.R HINDUNILVR.NS

There should be HUL plot saved in working directory in the form of PDF.

Saturday, January 14, 2017

Pair Trading using Quantmod - HUL & ITC


In my earlier post, I have explained that very first step for stat Arb Strategies is to find out the relation between two stocks. To analyze the whole data and run the relations could be very time consuming if done using downloading data and running calculations separately.

In this blog we will discuss Quantmod which is a package in R language and can help us to get it done easily. It helps to download the data from Yahoo finance and run quick calculations since the data gets downloaded in the form of vectors.

This blog will be using example of Indian Market's stock prices to understand basic use of quantmod.

TO INSTALL PACKAGE

>install.packages("quantmod")
>library(quantmod)
>require(quantmod)

TO RETRIEVE DATA

> symbolsHUL<-c("HINDUNILVR.NS")
> getSymbols(symbolsHUL)
[1] "HINDUNILVR.NS"
> startT <- "2016-01-01"
> endT <- "2017-01-01"
> rangeT <- paste(startT,"::",endT,sep="")
> HUL <- HINDUNILVR.NS[,6][rangeT]
> symbolsITC<-c("ITC.NS")
> getSymbols(symbolsITC)
[1] "ITC.NS"
> HUL <- HINDUNILVR.NS[,6][rangeT]
> ITC <- ITC.NS[,6][rangeT]

ANALYZE DATA & RUNNING REGRESSION

> pdtHUL <- diff(HUL)[-1]
> pdtITC <- diff(ITC)[-1]
> model <- lm(pdtHUL ~ pdtITC -1)
> hr <- as.numeric(model$coefficients)
 [1] 0.1332138

PLOTTING DATA

> plot(ITC)