Pages

Sunday, January 22, 2017

Plot Stock Graph using quantmod & functions in R


In next few blogs, we will use quantmod and explore its uses for indian markets, test some strategies and try to build some new one.

One of the basic tool in any programming language is functions and same is the case with R Language, we will use this concept to create function which can help us to plot graphs easily.

Save the below code in text editor with name - example.R

PlotGraph <- function(StockSymbol) {

  # Ensure that you have quantmod installed
require("quantmod")

  # Choose date from Where you want to Plot Graph
startT <- "2016-01-01"

  # Retrieve data
NSExts <- getSymbols(StockSymbol, from=startT, auto.assign=FALSE)

chartSeries(NSExts,name = StockSymbol)
}


Run the function using below code, You can open this in RStudio by double clicking the text file.

source('example.R')

Print the graph by passing name of the stock as argument and below is the result


PlotGraph("SBIN.NS") 

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)



Saturday, January 7, 2017

Stat Arb India - SBI & YES BANK


Currently going through the book ' The Quants' by Scott Patterson and it has some wonderful strategies used by Quants on the Wall Street and how it almost broke the entire financial system. There are some good strategies if used with caution can help generate some returns.

In this blog we will try to study the relation between SBI & YESBANK to understand if the price anomalies between these stocks can be used to drive some meaningful trades.

Data set is from 1st Jan 2012 till 31st Dec 2012. Co-relation between both the stocks is +0.65 and as you can see from the graph below the relation is positive.


Positive co-relation of 0.65 means that any wide anomolies in the stock returns of both the companies can be exploited by shorting the stock of one and buying the stock of another. Over the time the market will stick to the long term relations.

Make sure you have enough liquidity to stay for time till the market settles to historic relations and also conduct some additional research before getting into these kind of trades since they need huge leverage and can wipe out the entire capital if not executed correctly.

statistical arbitrage india