Wednesday, March 12, 2014

Trying an attempt to analyze the stock and commodities prices and see if I can spot any pattern using R functions, it is very important to have graphical representation of this patterns which help to determine the supply and demand forces in stocks. Graphical representation of historical stocks prices which form the repeating patterns or shape, are commonly used in stock markets.

Time Series analysis

A time series analysis which can be useful to see how security or economic variable changes over time or how it changes compared to other variables over the same time period. my intention is to analyze a time series of daily closing stock price for NSE (National Stock exchange),BSE(Bombay stock exchange),Gold and Currency conversion price over the period of years. Doing this we would obtain a list of all the closing prices for stock exchange vs commodities over each year and list them in chronological order. This would be closing price time series for specified range of year i.e from 2000 to 2013.

Forecasting

With the demand for gold being on rise, and complex set of factors influence the investment demand for gold, forecasting the price of gold is seen essential. With limited set of data that was available to me (i.e. Gold daily closing price), I tried to make an attempt to forecast the price of gold in the short run through time series modeling using the daily price of gold. One of the most widely adopted methods for time series modeling , autoregressive integrated moving averages(ARIMA) algorithm provided by R was used and tested  to forecast prices using the daily prices for the period specified in the range of 1979 to 2012.

Similarly we use ETS function available in the forecast package in R to analyze NSE and BSE data with exponential smoothing method.

For this study we will make use of R, Shiny and RHadoop functionality. For beginners who are new to Hadoop go through the below links to set up the environment before starting the code in R


R packages for Time series and forecasting

I used xts and forecast package of R for data analysis. Open terminal and use the command below to install packages

sudo su - -c "R -e \"install.packages('shiny', repos='http://cran.rstudio.com/')\""
sudo su - -c "R -e \"install.packages('xts', repos='http://cran.rstudio.com/')\""
sudo su - -c "R -e \"install.packages('forecast', repos='http://cran.rstudio.com/')\"" 

Once the configuration setup is done, packages are installed and if you’re feeling comfortable then start code in R


Shiny applications have two components: a user-interface definition and a server script. The source code for both of these components is listed below.

The user interface is defined in a source file named ui.R

 library(shiny)
 shinyUI(pageWithSidebar(
 headerPanel("Stock Analysis!"),
 sidebarPanel(wellPanel(
 uiOutput("from_day_dropdown"),
 uiOutput("from_month_dropdown"),
 uiOutput("from_year_dropdown"),

 uiOutput("to_day_dropdown"),
 uiOutput("to_month_dropdown"),
 uiOutput("to_year_dropdown")),


 div(div(
 selectInput(inputId = "stock_options",
 label = "Analysis Option",
 choices = c("Comparitive", "Detailed", "Combined","Time-Series","Forecasting-  BSE","Forecasting-NSE","Forecasting-GOLD","Forecasting-CURRENCY","BoxPlot"),  select="Comparitive")))),
 mainPanel(
 plotOutput("SelectedCombinedPlot", height="350px", width="800px"),
 plotOutput("MonthBack_SelectedCombinedPlot", height="350px", width="800px"),
 plotOutput("TwoMonthBack_SelectedCombinedPlot", height="800px", width="800px")
 )
 ))

The server-side of the application defined in server.r is shown below, lets go through it in short for better understanding, first few lines of code will load the R packages XTS, FORECAST so that we can use its  time-series , arima & ets functions. RHDFS been used to read the csv file from HDFS. We make use of merge function to create a data frame which contains combined data for NSE, BSE, GOLD & CURRENCY using DATE field. Our data frame is multi-dimensional and in order to represent data in graph it is essential to make data column in proportionate, just use integer value and divide few data frame columns to do little bit of tweaking with data (You can have your own way to do it at code or directly make the changes in CSV file). I defined 5 functions each function will accept the parameter as date range

  • Function comp_graph  & detailed_graph create the data frame for selective data range and plot the graph.
  • Function detailed_graph_timeseries create the xts object which then will be passed to ts (time series function) with start & end range of years.
  • detailed_graph_Forecast_GOLD  will create the ts object and then passed to arima function to plot the forecast.



 library(shiny)
 library(xts)
 library(forecast)
 Sys.setenv("HADOOP_CMD"="/usr/local/hadoop/bin/hadoop")
 library(rhdfs)


 hdfs.init()
 f1 = hdfs.file("/user/cs246/bse_table.csv","r",buffersize=1048500)
 m1 = hdfs.read(f1)
 c1 = rawToChar(m1)
 bse = read.table(textConnection(c1), sep = ",",header=TRUE)

 f2 = hdfs.file("/user/cs246/nse_table.csv","r",buffersize=1048500)
 m2 = hdfs.read(f2)
 c2 = rawToChar(m2)
 nse = read.table(textConnection(c2), sep = ",",header=TRUE)

 f3 = hdfs.file("/user/cs246/Gold_prices.csv","r",buffersize=1048500)
 m3 = hdfs.read(f3)
 c3 = rawToChar(m3)
 gold= read.table(textConnection(c3), sep = ",",header=TRUE)

 f4 = hdfs.file("/user/cs246/currency_data1.csv","r",buffersize=1048500)
 m4 = hdfs.read(f4)
 c4 = rawToChar(m4)
 currency= read.table(textConnection(c4), sep = ",",header=TRUE)

 bse_nse<-merge(bse,nse,by="Date",all.x=TRUE)
 bsense_curr<-merge(bse_nse,currency,by="Date",all.y=TRUE)
 bsense_cur_gld<-merge(bsense_curr,gold,by="Date")
 bsense_cur_gld[is.na(bsense_cur_gld)]<-0
 colnames(bsense_cur_gld) <- c("Date",  "Year","bse_Index","bse_high","bse_low","bse_close","bse_volume","bse_adj","nse_open","nse_  high","nse_low","nse_index","nse_volume","nse_adj","RsValue","Price","seasonal")
 bsense_cur_gld$bse_p_index<-NA
 bsense_cur_gld$bse_p_index<-bsense_cur_gld$bse_Index/350
 bsense_cur_gld$nse_p_index<-NA
 bsense_cur_gld$nse_p_index<-bsense_cur_gld$nse_index/100
 bsense_cur_gld$gold_price<-NA
 bsense_cur_gld$gold_price<-bsense_cur_gld$Price/500
 analysis<-bsense_cur_gld
 analysis[is.na(analysis)]<-0

 analysis$Date <- as.Date(analysis$Date, format="%m/%d/%Y")
 analysis<-analysis[order(as.Date(analysis$Date, format="%Y-%d-%m")),]
 gold$Date1= as.Date(gold$Date, format="%m/%d/%Y")
 TimePeriods <- unique(as.numeric(format(gold$Date1, "%Y")))

 mon <- 01:12

 month <- function() {
 mon <- factor(mon, labels = month.abb)
 }

 comp_graph <- function(option, from_year, from_day, from_month, to_year, to_day, to_month)  {

 if(option == "1") #selected period comparitive graph
 {
   
 sanalysis <- analysis[(as.Date(analysis$Date,format="%Y-%d-%m") >=  as.Date(paste(from_year, from_day , match(from_month, month.abb),sep="-"), format="%Y-  %d-%m")) & (as.Date(analysis$Date,format="%Y-%d-%m") <= as.Date(paste(to_year,  to_day,match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
 main_header="Selective Period Comparitive Graph"
 }
 else if(option == "2") # Comparitive graph of 1 month back from selected period
 {
 sanalysis <- analysis[(as.Date(analysis$Date,format="%Y-%d-%m") >=  as.Date(paste(from_year, from_day , match(from_month, month.abb),sep="-"), format="%Y- 
 %d-%m")-31) & (as.Date(analysis$Date,format="%Y-%d-%m") <= as.Date(paste(to_year,  to_day,match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
 main_header="One month back Selective Period Comparitive Graph"
 }
 else if(option == "3") # Comparitive graph of 1 year back from selected period
 {
 sanalysis <- analysis[(as.Date(analysis$Date,format="%Y-%d-%m") >=  as.Date(paste(from_year, from_day , match(from_month, month.abb),sep="-"), format="%Y-  %d-%m")-365) & (as.Date(analysis$Date,format="%Y-%d-%m") <= as.Date(paste(to_year,  to_day,match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
 main_header="1 year back Selective Period Comparitive Graph"
 }

 sanalysis$blankplot <- sanalysis$RsValue

   
 if(length(sanalysis$Dates) > 1)
 {
 min_max_value<-sqldf("select min(RsValue), max(RsValue),min(gold_Price),    max(gold_Price),min(nse_p_index), max(nse_p_index),min(bse_p_index), max(bse_p_index)  from sanalysis")
 sanalysis[1,match("blankplot",names(sanalysis))]<-apply(min_max_value, 1, function(x)  min(x[x!=0]) )
 sanalysis[2,match("blankplot",names(sanalysis))]<-apply(min_max_value, 1, function(x)  max(x[x!=0]) )
 }

 plot(sanalysis$"Date", sanalysis$"blankplot", type="n",col="green", xlab="Periods",  main=main_header, ylab="Index",lwd="0")
 lines(sanalysis$"Date", sanalysis$"gold_price", type="l", col="yellow",  lwd="1")
 par(new=T)
 lines(sanalysis$"Date", sanalysis$"RsValue", type="l", col="green",  lwd="1")
 par(new=T)
 lines(sanalysis$"Date", sanalysis$"bse_p_index", type="l", col="blue",  lwd="1")
 par(new=T)
 lines(sanalysis$"Date", sanalysis$"nse_p_index", type="l", col="red",  lwd="1")
 legend("topright", c("Gold Price (in Rs 500)","Rs Value","NSE (in 100)","BSE (in  350)"),lty=c(1,1,1,1), lwd=c(2.5,2.5,2.5,2.5),col=c("yellow","green","blue","red"), ncol=2,    cex=.70)
 }

 detailed_graph <- function(option, from_year, from_day, from_month, to_year, to_day,  to_month) {

 sanalysis <- analysis[(as.Date(analysis$Date,format="%Y-%d-%m") >=  as.Date(paste(from_year, from_day, match(from_month, month.abb),sep="-"), format="%Y-  %d-%m")) & (as.Date(analysis$Date,format="%Y-%d-%m") <= as.Date(paste(to_year,  to_day, match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]

 if(option=="1")
 {
  plot(x<- sanalysis$Dates,y<-sanalysis$RsValue,type = "l" , main = "Currency Data",col =  "green",xlab="Periods",ylab="Rupee value",lwd=3)
 }
 else if(option=="2")
 {
 plot(x<- sanalysis$Dates,y<-sanalysis$bse_Index,type = "l" , main = "BSE Data",col =  "blue",xlab="Periods",ylab="BSE Values",lwd=3)
 }
 else if(option=="3")
 {
 plot(x<- sanalysis$Dates,y<-sanalysis$nse_index,type = "l" , main = "NSE Data",col =  "red",xlab="Periods",ylab="NSE Values",lwd=3)
 }
 else if(option=="4")
 {
  plot(x<- sanalysis$Dates,y<-sanalysis$gold_price,type = "l" , main = "Gold Price",col =  "Yellow",xlab="Periods",ylab="Gold value",lwd=3)
 }
 }

 detailed_graph_Timeseries <- function(option, from_year, from_day, from_month, to_year,  to_day, to_month) {

 from_year<-as.numeric(from_year)
 to_year<-as.numeric(to_year)

 if(option=="1")
 {
   
 bse$Date <-as.Date(bse$Date,format="%m/%d/%Y")
 bse_analysis <- bse[(bse$Date >= as.Date(paste(from_year, from_day, match(from_month,  month.abb),sep="-"), format="%Y-%d-%m")) & (bse$Date <= as.Date(paste(to_year, to_day,  match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
   
 Close.xts = xts(x=bse_analysis$Close, order.by=bse_analysis$Date)
 Open.xts = xts(x=bse_analysis$Open, order.by=bse_analysis$Date)
 High.xts = xts(x=bse_analysis$High, order.by=bse_analysis$Date)
 Low.xts = xts(x=bse_analysis$Low, order.by=bse_analysis$Date)
   
 close = ts(Close.xts, freq=12, start=c(from_year, 1),end=c(to_year,12))
 open = ts(Open.xts, freq=12, start=c(from_year, 1),end=c(to_year,12))
 high = ts(High.xts, freq=12, start=c(from_year, 1),end=c(to_year,12))
 low = ts(Low.xts, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 bse_merge_ts<-cbind(open,close,low,high)
 plot(bse_merge_ts,main="BSE Time-series analysis based on Input Date Range ",xlab="Time  interval",ylab="Index",sub="Source of Data : Bombay Stock  Exchange",lwd=2,col=c("black","red","blue","Yellow"),xlim=c(from_year,to_year),ylim=c(10,10  0))
 }
  else if(option=="2")
  {
 nse$Date <-as.Date(nse$Date,format="%m/%d/%Y")
 nse_analysis <- nse[(nse$Date >= as.Date(paste(from_year, from_day, match(from_month,  month.abb),sep="-"), format="%Y-%d-%m")) & (nse$Date <= as.Date(paste(to_year, to_day,  match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]

 Close.xts = xts(x=nse_analysis$Close, order.by=nse_analysis$Date)
 Open.xts = xts(x=nse_analysis$Open, order.by=nse_analysis$Date)
 High.xts = xts(x=nse_analysis$High, order.by=nse_analysis$Date)
 Low.xts = xts(x=nse_analysis$Low, order.by=nse_analysis$Date)
   
 close = ts(Close.xts, freq=12, start=c(from_year, 1),end=c(to_year,12))
 open = ts(Open.xts, freq=12, start=c(from_year, 1),end=c(to_year,12))
 high = ts(High.xts, freq=12, start=c(from_year, 1),end=c(to_year,12))
 low = ts(Low.xts, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 nse_merge_ts<-cbind(open,close,low,high)
 plot(nse_merge_ts,main="NSE Time-series analysis based on Input Date Range",xlab="Time  interval",ylab="Index",sub="Source of Data : Bombay Stock  Exchange",lwd=2,col=c("black","red","blue","Yellow"),xlim=c(from_year,to_year),ylim=c(10,10  0))
 }
  else if(option=="3")
  {
 gold$Date <-as.Date(gold$Date,format="%m/%d/%Y")
 gold_analysis <- gold[(gold$Date >= as.Date(paste(from_year, from_day, match(from_month,  month.abb),sep="-"), format="%Y-%d-%m")) & (gold$Date <= as.Date(paste(to_year,    to_day, match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
   
 x = xts(x=gold_analysis$Price, order.by=gold_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
 plot(x.ts,main="Gold Time-series analysis based on Input Date Range ",xlab="Time  interval",ylab="Price",sub="Source of Data : Bombay Stock Exchange",lwd=2,col="red")
 }
 else if(option=="4")
 {
 currency$Date <-as.Date(currency$Date,format="%m/%d/%Y")
 currency_analysis <- currency[(currency$Date >= as.Date(paste(from_year, from_day,  match(from_month, month.abb),sep="-"), format="%Y-%d-%m")) & (currency$Date <=  as.Date(paste(to_year, to_day, match(to_month, month.abb), sep="-"), format="%Y-%d-  %m")),]

 x = xts(x=currency_analysis$RsValue, order.by=currency_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
 plot(x.ts,main="CURRENCY Time-series analysis based on Input Date Range ",xlab="Time  interval",ylab="Rate",sub="Source of Data : Bombay Stock Exchange",lwd=2,col="red")
 }
 }


 detailed_graph_BoxPlot <- function(option, from_year, from_day, from_month, to_year,  to_day, to_month) {
 from_year<-as.numeric(from_year)
 to_year<-as.numeric(to_year)
 if(option=="1")
 {
   
 bsedf<-data.frame(Open=bse$Open,Close=bse$Close,Low=bse$Low,High=bse$High)
 boxplot(bsedf,ylab="Index range",xlab="Stock exhange  index",las=2,col=c("red","sienna","palevioletred1","royalblue2"),notch=TRUE,main="Boxplot of  BSE indexes")
 }
 else if(option=="2")
 {
 nsedf<-data.frame(Open=nse$Open,Close=nse$Close,Low=nse$Low,High=nse$High)
 boxplot(nsedf,ylab="Index range",xlab="Stock exhange  index",las=2,col=c("red","sienna","palevioletred1","royalblue2"),notch=TRUE,main="Boxplot of  NSE indexes")
 }
 else if(option=="3")
 {
 golddf<-data.frame(Price=gold$Price)
 boxplot(golddf,ylab="Gold Price",xlab="Gold Price for period  range",las=2,col=c("red"),main="Boxplot of Gold Price")
 }
 else if(option=="4")
 {
 currdf<-data.frame(Price=currency$RsValue)
 boxplot(currdf,ylab="Currency Conversion Rate Range",xlab="(USD/INR)  Converison",las=2,col=c("red"),main="Boxplot of (USD/INR) Conversion")
   
}
}

 detailed_graph_Forecast_NSE <- function(option, from_year, from_day, from_month, to_year,  to_day, to_month)
 {

 nse$Date <-as.Date(nse$Date,format="%m/%d/%Y")
 nse_analysis <- nse[(nse$Date >= as.Date(paste(from_year, from_day, match(from_month,  month.abb),sep="-"), format="%Y-%d-%m")) & (nse$Date <= as.Date(paste(to_year, to_day,  match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
 nse$Date <-as.Date(nse$Date,format="%Y-%d-%m")
 nse_analysis <- nse[(nse$Date >= as.Date(paste(from_year, from_day, match(from_month,  month.abb),sep="-"), format="%Y-%d-%m")) & (nse$Date <= as.Date(paste(to_year, to_day,  match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
 from_year<-as.numeric(from_year)
 to_year<-as.numeric(to_year)

 nse_analysis$Date = as.Date(nse_analysis$Date,format="%m/%d/%Y")
 if(option=="1")
 {
 x = xts(x=nse_analysis$Close, order.by=nse_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 plot(forecast(ets(x.ts), 40),main="nse Close from ETS(M,N,N) based on Input Date Range  ",xlab="Time in Years",ylab="Closing Index",sub="Source of Data : Bombay Stock  Exchange",col="red",lwd=2,xlim=c(from_year,(to_year+2)))
 }
 else if(option=="2")
{
 x = xts(x=nse_analysis$Open, order.by=nse_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 plot(forecast(ets(x.ts), 10),main="nse Open Forecasts from ETS(M,N,N) based on Input Date  Range ",xlab="Time in Years",ylab="Closing Index",sub="Source of Data : Bombay Stock  Exchange",col="red",lwd=2,xlim=c(from_year,(to_year+2)))
   
}
else if(option=="3")
{
 x = xts(x=nse_analysis$Low,order.by=nse_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 plot(forecast(ets(x.ts), 10),main="nse Low Forecasts from ETS(M,N,N) based on Input Date  Range ",xlab="Time in Years",ylab="Closing Index",sub="Source of Data : Bombay Stock  Exchange",col="red",lwd=2,xlim=c(from_year,(to_year+2)))
   
}
 else if(option=="4")
 {
 x = xts(x=nse_analysis$High,order.by=nse_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 plot(forecast(ets(x.ts), 10),main="nse High Forecasts from ETS(M,N,N) based on Input Date  Range ",xlab="Time in Years",ylab="Closing Index",sub="Source of Data : Bombay Stock  Exchange",col="red",lwd=2,xlim=c(from_year,(to_year+2)))
 }
 }
 detailed_graph_Forecast_GOLD<- function(option, from_year, from_day, from_month,  to_year, to_day, to_month)
 {


 gold$Date <-as.Date(gold$Date,format="%m/%d/%Y")
 gold_analysis <- gold[(gold$Date >= as.Date(paste(from_year, from_day, match(from_month,  month.abb),sep="-"), format="%Y-%d-%m")) & (gold$Date <= as.Date(paste(to_year,  to_day, match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
 from_year<-as.numeric(from_year)
 to_year<-as.numeric(to_year)
 gold_analysis$Date = as.Date(gold_analysis$Date,format="%m/%d/%Y")
 if(option=="1")
 {
 x = xts(x=gold_analysis$Price, order.by=gold_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
 modArima<-arima(x.ts,order=c(1,1,0),seasonal=list(order=c(1,1,0), period=12))
 plot(forecast(modArima,20),main="Gold price forecasts from ARIMA with seasonalilty  Adjusted,based on data from 1979 to 2013",xlab="Time in Years",ylab="Price",sub="Source of  Data : Bombay Stock Exchange",lwd=2,xlim=c(1979,2015))
 }
 }
 detailed_graph_Forecast_CURRENCY <- function(option, from_year, from_day, from_month,  to_year, to_day, to_month)
 {

 currency$Date <-as.Date(currency$Date,format="%Y-%d-%m")
 currency_analysis <-currency[(currency$Date >= as.Date(paste(from_year, from_day,  match(from_month, month.abb),sep="-"), format="%Y-%d-%m")) & (currency$Date <=  as.Date(paste(to_year, to_day, match(to_month, month.abb), sep="-"), format="%Y-%d-  %m")),]
  from_year<-as.numeric(from_year)
  to_year<-as.numeric(to_year)
  currency_analysis$Date = as.Date(currency_analysis$Date,format="%m/%d/%Y")

  if(option=="1")
 {
 x = xts(x=currency_analysis$RsValue, order.by=currency_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
 modArima_curr<-arima(x.ts,order=c(1,1,0))
 plot(forecast(modArima_curr,20),main="Currency conversion(USD/INR) forecasts from  ARIMA based on data from 1993 to 2013",xlab="Time in Years",ylab="Conversion  Price",sub="Source of Data : Bombay Stock Exchange",lwd=2,xlim=c(1993,2015))

 }
 }

 detailed_graph_Forecast_BSE <- function(option, from_year, from_day, from_month, to_year,  to_day, to_month)
 {
 bse$Date <-as.Date(bse$Date,format="%Y-%d-%m")
 bse_analysis <- bse[(bse$Date >= as.Date(paste(from_year, from_day, match(from_month,  month.abb),sep="-"), format="%Y-%d-%m")) & (bse$Date <= as.Date(paste(to_year, to_day,  match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
 nse$Date <-as.Date(nse$Date,format="%Y-%d-%m")
 nse_analysis <- nse[(nse$Date >= as.Date(paste(from_year, from_day, match(from_month,  month.abb),sep="-"), format="%Y-%d-%m")) & (nse$Date <= as.Date(paste(to_year, to_day,  match(to_month, month.abb), sep="-"), format="%Y-%d-%m")),]
 from_year<-as.numeric(from_year)
 to_year<-as.numeric(to_year)
 print(head(nse_analysis$Date))
 bse_analysis$Date = as.Date(bse_analysis$Date,format="%m/%d/%Y")
 if(option=="1")
 {            
 x = xts(x=bse_analysis$Close, order.by=bse_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
 plot(forecast(ets(x.ts), 40),main="BSE Close from ETS(M,N,N) based on Input Date Range  ",xlab="Time in Years",ylab="Closing Index",sub="Source of Data : Bombay Stock  Exchange",col="red",lwd=2,xlim=c(from_year,(to_year+2)))
 }
 else if(option=="2")
 {
 x = xts(x=bse_analysis$Open, order.by=bse_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 plot(forecast(ets(x.ts), 10),main="BSE Open Forecasts from ETS(M,N,N) based on Input Date  Range ",xlab="Time in Years",ylab="Closing Index",sub="Source of Data : Bombay Stock  Exchange",col="red",lwd=2,xlim=c(from_year,(to_year+2)))
   
 }
 else if(option=="3")
 {
 x = xts(x=bse_analysis$Low,order.by=bse_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 plot(forecast(ets(x.ts), 10),main="BSE Low Forecasts from ETS(M,N,N) based on Input Date  Range ",xlab="Time in Years",ylab="Closing Index",sub="Source of Data : Bombay Stock  Exchange",col="red",lwd=2,xlim=c(from_year,(to_year+2)))
   
 }
 else if(option=="4")
 {
 x = xts(x=bse_analysis$High,order.by=bse_analysis$Date)
 x.ts = ts(x, freq=12, start=c(from_year, 1),end=c(to_year,12))
   
 plot(forecast(ets(x.ts), 10),main="BSE High Forecasts from ETS(M,N,N) based on Input Date  Range ",xlab="Time in Years",ylab="Closing Index",sub="Source of Data : Bombay Stock  Exchange",col="red",lwd=2,xlim=c(from_year,(to_year+2)))
 }
 }

 shinyServer(function(input, output) {

  # from dropdown
 output$from_year_dropdown <- reactiveUI(function() {
 selectInput(inputId = "fromyear", label = "", TimePeriods, select=format(Sys.Date(),"%Y"))
 })

 output$from_month_dropdown <- reactiveUI(function() {
 selectInput(inputId = "frommonth", label = "", month(), select="Jan")
 })

 output$from_day_dropdown <- reactiveUI(function() {
 selectInput(inputId = "fromday", label="From Date:", seq(from=1, to=31, by=1), select="1")
 })


  # to dropdown
 output$to_year_dropdown <- reactiveUI(function() {
 selectInput(inputId = "toyear", label = "", TimePeriods, select=format(Sys.Date(),"%Y"))
 })

 output$to_month_dropdown <- reactiveUI(function() {
 selectInput(inputId = "tomonth", label = "", month(), select=format(Sys.Date(),"%b"))
 })

 output$to_day_dropdown <- reactiveUI(function() {
 selectInput(inputId = "today", label="To Date:", seq(from=1, to=31, by=1),  select=format(Sys.Date(),"%d"))
 })

# SelectedCombined plot
 output$SelectedCombinedPlot <- renderPlot({
 if(input$stock_options == "Comparitive" | input$stock_options == "Combined")
 {
 comp_graph("1",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 }
 else if(input$stock_options == "Detailed")
 {
 detailed_graph("1",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 }
 else if(input$stock_options == "Forecasting-BSE")
 {
   
 detailed_graph_Forecast_BSE("1",input$fromyear, input$fromday,  input$frommonth,input$toyear, input$today, input$tomonth)

 }
 else if(input$stock_options == "Forecasting-NSE")
 {
   
 detailed_graph_Forecast_NSE("1",input$fromyear, input$fromday,  input$frommonth,input$toyear, input$today, input$tomonth)
   
   
 }
 else if(input$stock_options == "Forecasting-GOLD")
 {
   
 detailed_graph_Forecast_GOLD("1",input$fromyear, input$fromday,  input$frommonth,input$toyear, input$today, input$tomonth)
   
   
 }else if(input$stock_options == "Forecasting-CURRENCY")
 {
   
 detailed_graph_Forecast_CURRENCY("1",input$fromyear, input$fromday,  input$frommonth,input$toyear, input$today, input$tomonth)
   
   
 }
 else if(input$stock_options == "Time-Series")
 {
   
 detailed_graph_Timeseries("1",input$fromyear, input$fromday, input$frommonth,input$toyear,  input$today, input$tomonth)
   
   
 }
 else if(input$stock_options == "BoxPlot")
{
 detailed_graph_BoxPlot("1",input$fromyear, input$fromday, input$frommonth,input$toyear,  input$today, input$tomonth)
   
 }

 })

 output$MonthBack_SelectedCombinedPlot <- renderPlot(
 {
 if(input$stock_options == "Comparitive" | input$stock_options == "Combined")
 {
 par(mfrow = c(1,2))
 comp_graph("2",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 comp_graph("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 }
 else if(input$stock_options == "Detailed")
 {
 detailed_graph("2",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 }

 else if(input$stock_options == "Forecasting-BSE")
 {
 detailed_graph_Forecast_BSE("2",input$fromyear, input$fromday,    input$frommonth,input$toyear, input$today, input$tomonth)

}
else if(input$stock_options == "Forecasting-NSE")
{
 detailed_graph_Forecast_NSE("2",input$fromyear, input$fromday,  input$frommonth,input$toyear, input$today, input$tomonth)
   
}
 else if(input$stock_options == "Forecasting-GOLD")
 {
 detailed_graph_Forecast_GOLD("2",input$fromyear, input$fromday,  input$frommonth,input$toyear, input$today, input$tomonth)
   
 }
 else if(input$stock_options == "Forecasting-CURRENCY")
 {
 detailed_graph_Forecast_CURRENCY("2",input$fromyear, input$fromday,  input$frommonth,input$toyear, input$today, input$tomonth)
   
 }
 else if(input$stock_options == "Time-Series")
 {
 detailed_graph_Timeseries("2",input$fromyear, input$fromday, input$frommonth,input$toyear,  input$today, input$tomonth)
   
 }

 else if(input$stock_options == "BoxPlot")
 {
 detailed_graph_BoxPlot("2",input$fromyear, input$fromday, input$frommonth,input$toyear,  input$today, input$tomonth)
}
})


 output$TwoMonthBack_SelectedCombinedPlot <- renderPlot({
 if(input$stock_options == "Combined")
 {
 sanalysis <- analysis[(analysis$Dates >= as.Date(paste(input$fromyear, input$fromday,    match(input$frommonth, month.abb),sep="-"), format="%Y-%d-%m")-31) & (analysis$Dates  <= as.Date(paste(input$toyear, input$today, match(input$tomonth, month.abb), sep="-"),  format="%Y-%d-%m")),]
   
 par(mfrow = c(2,2))
 detailed_graph("1",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 detailed_graph("2",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 detailed_graph("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 detailed_graph("4",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 }
 else if(input$stock_options == "Detailed")
  {
 par(mfrow = c(2,1))
 detailed_graph("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
 detailed_graph("4",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today,  input$tomonth)
  }
  else if(input$stock_options == "Forecasting-BSE")
  {
    par(mfrow = c(2,1))
    detailed_graph_Forecast_BSE("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
    detailed_graph_Forecast_BSE("4",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
  }
  else if(input$stock_options == "Forecasting-NSE")
  {
    par(mfrow = c(2,1))
    detailed_graph_Forecast_NSE("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
    detailed_graph_Forecast_NSE("4",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
  }
  else if(input$stock_options == "Forecasting-GOLD")
  {
    par(mfrow = c(2,1))
    detailed_graph_Forecast_GOLD("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
    detailed_graph_Forecast_GOLD("4",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
  }
  else if(input$stock_options == "Forecasting-CURRENCY")
  {
    par(mfrow = c(2,1))
    detailed_graph_Forecast_CURRENCY("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
    detailed_graph_Forecast_CURRENCY("4",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
  }
  else if(input$stock_options == "Time-Series")
  {
    par(mfrow = c(2,1))
    detailed_graph_Timeseries("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
    detailed_graph_Timeseries("4",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
  }
  else if(input$stock_options == "BoxPlot")
  {
    par(mfrow = c(2,1))
    detailed_graph_BoxPlot("3",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
    detailed_graph_BoxPlot("4",input$fromyear, input$fromday, input$frommonth,input$toyear, input$today, input$tomonth)
  }
  })
 })

Place both the script ui.r and server.r in a folder; I place them in UI_Nikhil folder at /home/share/

Open terminal –Load R session, issue list of command shown below

setwd(‘/home/share’)
getwd()
library(‘shiny’)
runApp(‘UI_Nikhil’)

No comments:

Post a Comment