Pages

Showing posts with label getSymbols. Show all posts
Showing posts with label getSymbols. Show all posts

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.

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