shinyServer( 
  function(input, output,session) {
    library(rsconnect)
    library(tseries)
    library(quantmod)
    library(forecast)
    library(car)
    library(haven)
    
    yahoo1<- reactive({
      paste("Please enter the code for YAHOO finance")
    })
    output$yahooop<-renderText({ yahoo1()})
   
    observeEvent(
    input$goButton,{
     index2<-as.character( input$index1)
    index3<-getSymbols(c(index2),from = "1900-01-05",auto.assign = FALSE)
    index.open<-na.omit(data.frame(index3[,1]))
    index.close<-na.omit(data.frame(index3[,4]))
    index.open.test<-data.frame(-tail(index.open,120))
    index.close.test<-data.frame(-tail(index.close,120))
      index.open.train<-data.frame(index.open[1:(nrow(index.open)-365),])
      index.close.train<-data.frame(index.close[1:(nrow(index.open)-365),])
    index.open.year<-data.frame(tail(index.open,365))
    index.close.year<-data.frame(tail(index.close,365))
    colnames(index.open.year)="OP.value"
    colnames(index.open.train)="OP.value"
    colnames(index.close.year)="close.value"
    colnames(index.close.train)="close.value"
    mod1<-auto.arima(index.open.train, seasonal = TRUE,ic="aic",test = "adf",seasonal.test ="seas",allowdrift = TRUE,
                     allowmean = TRUE,stepwise=FALSE,approximation=FALSE)
    mod2<-auto.arima(index.close.train, seasonal = TRUE,ic="aic",test = "adf",seasonal.test ="seas",allowdrift = TRUE,
                     allowmean = TRUE,stepwise=FALSE,approximation=FALSE)
    predict.open<-forecast(index.open.test,mod=mod1,h=120)
    predict.close<-forecast(index.close.test,mod=mod2,h=120)
    for(x in c(1:120)){d1<-(index.open[x]-index.close[x])-(predict.close$fitted[x]-predict.open$fitted[x])
    d2<-sum(d1)}
    output$open.predict.plot1<-renderPlot(plot(forecast(tail(index.open.year,30) ,model=mod1,h=5),main = paste(c(index2),".open","ARIMA predicton plot")))
    
    output$open.value1<-renderTable(tail(index.open.year,5),rownames = TRUE)
    open.formulaText1 <- reactive({
      paste("it is the index opening index in the past five days")
    })
    output$open.table1<-renderText({ open.formulaText1()})
    output$open.predict.table1<-renderTable(data.frame(forecast(tail(index.open.year,30) ,model=mod2,h=5)))
    
    open.formulaText2 <- reactive({
      paste("The expected value of the opening index  in the next five days and its 80%, 95% confidence interval")
    })
    output$open.table2<-renderText({ open.formulaText2()})
    
    output$close.predict.plot1<-renderPlot(plot(forecast(tail(index.close.year,30) ,model=mod2,h=5),main =paste(c(index2),".close"," ARIMA predicton plot")))
    output$close.value1<-renderTable(tail(index.close.year,5),rownames = TRUE)
    close.formulaText1 <- reactive({
      paste("it is the index closeing index in the past five days")
    })
    output$close.table1<-renderText({ close.formulaText1()})
    output$close.predict.table1<-renderTable(data.frame(forecast(tail(index.close.year,30) ,model=mod2,h=5)))
    
    close.formulaText2 <- reactive({
      paste("The expected value of the closeing index  in the next five days and its 80%, 95% confidence interval")
    })
    output$close.table2<-renderText({ close.formulaText2()})
    income <- reactive({
      paste("the net income is",d2 )
    })  
    })})