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") 

No comments:

Post a Comment