


library(rsconnect)
library(shiny)
library(quantmod)
library(rvest)
library(XML)
library(e1071)
library(timeSeries)
library(forecast)
library(neuralnet)
library(nnet)
library(lattice)
library(ggplot2)
library(caret)
library(MLmetrics)
library(rsconnect)
library(devtools)

deployApp("C:\\Users\\User\\Desktop\\shinyApp")


Stock<-read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vRYxupbr1vUlPOpq2JPGzlCuJKcMXHvC4MaWR-qzngSpBiymOBFbbXuegSJjgUl6-Rb-3bPvCvJOCFW/pub?gid=1809581675&single=true&output=csv",header=T)
N<-nrow(Stock)
st<-Stock[,2]

set.seed(123)
ind1<-sample(N,round(N*0.7))
Train<-Stock[ind1,-1]
Test<-Stock[-ind1,-1]
Test1<-Test[,11:18]    


mod1<-lm(stock~usd+coffee+rice+oil+wheatADJ+Djopen+cuADJ+cornADJ,data=Train)




TWII=getSymbols( "^TWII" ,from = "2017-9-1", src="yahoo",auto.assign = FALSE)
ul="http://q...content-available-to-author-only...o.com/exchanges/contracts.html?r=CBOT_ZW"
wheatc = data.frame( readHTMLTable(ul)) 

wheat1<-data.frame(as.character( wheatc$NULL.V3))

oil1=getSymbols("CL=F",from = "2017-09-01", src="yahoo",auto.assign = FALSE)
ul2="http://q...content-available-to-author-only...o.com/exchanges/contracts.html?r=CBOT_ZR"
ricec<-data.frame(readHTMLTable(ul2))
rice1<-data.frame(as.character(ricec$NULL.V3))

ul1="http://w...content-available-to-author-only...m.br/indicadores-de-mercado/precos-cepeaesalq/"
coffeec = data.frame( readHTMLTable(ul1)) 
coffee1<- coffeec$table_1.Conillon.US.

corn1=getSymbols("C=F",from = "2017-09-01", src="yahoo",auto.assign = FALSE)
cu1<-getSymbols("^YHOH711",auto.assign = F)
dj1=getSymbols( "^DJI" ,from = "2017-09-01", src="yahoo",auto.assign = FALSE)
usd1=getSymbols( "USDTWD=X" ,from = "2017-09-01", src="yahoo",auto.assign = FALSE)

tw.ts<-TWII$TWII.Adjust[-(1:3552),]
tw<-as.numeric(tail(TWII$TWII.Adjust,n=30))

oil2<-as.numeric(tail(oil1[,6],n=1))
oil3<-(oil2-mean(Stock$oil))/sd(Stock$oil)
coffee2<-tail(as.numeric (coffee1),n=1)
coffee3<-(coffee2-mean(Stock$coffee))/sd(Stock$coffee)
rice2<-head( as.numeric(as.character(rice1[-(1:3),])),n=1)
rice3<-(rice2-mean(Stock$rice))/sd(Stock$rice)
corn2<-as.numeric(tail(corn1[,6],n=1)/10)
corn3<-(corn2-mean(Stock$cornADJ))/sd(Stock$cornADJ)
cu2<-as.numeric(tail(cu1[,6],n=1)/10)
cu3<-(cu2-mean(Stock$cuADJ))/sd(Stock$cuADJ)
dj2<-as.numeric(tail(dj1[,1],n=1)/100)
dj3<-(dj2-mean(Stock$Djopen))/sd(Stock$Djopen)

usd2<-as.numeric (tail(usd1[,6],n=1))
usd3<-(usd2-mean(Stock$usd))/sd(Stock$usd)
wheat2<-head(as.numeric( as.character( wheat1[-(1:2),])),n=1)/10
wheat3<-(wheat2-mean(Stock$wheatADJ))/sd(Stock$wheatADJ)


d1<-data.frame(oil=oil2,coffee=coffee2,usd=usd2,rice=rice2,wheatADJ=wheat2,cuADJ=cu2,Djopen=dj2,cornADJ=corn2)
d2<-data.frame(usd.z=usd3,coffee.z=coffee3,rice.z=rice3,oil.z=oil3,dj.z=dj3,wheat.z=wheat3,corn.z=corn3,cu.z=cu3)
new<-predict(mod1,newdata = d1)

new1<-stack(data.frame(tw,new))

new2<-new1[1:31,1]

sttdiff<-diff(st,differences=1)



stt3<- arima(sttdiff, order=c(14,1,0),method = "ML")


stt_cor<-forecast(tw, h=5, level = c(95),model=stt3) 



x<-rep(2,time=30)
y<-4
z<-data.frame(x,y)
z1<-stack(z)
z2<-z1[1:31,1]
model <-neuralnet(formula =stock.z~usd.z+coffee.z+rice.z+oil.z+wheat.z+dj.z+cu.z+corn.z,data = Train, hidden = c(14)  ,learningrate = 0.1, threshold =
                    0.1,stepmax =90000) 
pred <- compute(model,d2)
new.n<-(pred$net.result)
new.n1<-new.n*sd(Stock$stock)+mean(Stock$stock)
new1.n<-stack(data.frame(tw,new.n1))
new2.n<-new1.n[1:31,1]
plot(new2,type="b",ylab="twstock" ,xlab="last mounth",cex=z2,col=z2,lwd=3,main="regression prediction")
s1<-svm(stock.z~usd.z+coffee.z+rice.z+oil.z+wheat.z+dj.z+cu.z+corn.z,data=Train,gamma=2^-1,cost=2^6)
svm1<-predict(s1,newdata = d2)

da<-data.frame( model$generalized.weights)
da2<-abs(da)
da3<-data.frame(usd=sum(da2$X1),coffee=sum(da2$X2),rice=sum(da2$X3),oil=sum(da2$X4),wheat=sum(da2$X5),dj=sum(da2$X6),cu=sum(da2$X7),corn=sum(da2$X8))

svm2<-svm1*sd(Stock$stock)+mean(Stock$stock)
svm3<-stack(data.frame(tw,svm2))
svm4<-svm3[1:31,1]
shinyServer(function(input, output) { 
 
  output$twstock.mod <- renderPlot(plot(new2,type="b",ylab="twstock" ,xlab="last mounth",cex=z2,col=z2,lwd=3,main="regression prediction"))
    
   output$twtime<-renderPlot(plot(stt_cor))
  output$twnet<-renderPlot(plot( new2.n,type="b",ylab="twstock" ,xlab="last mounth",cex=z2,col=z2,lwd=3,main="neuralnet prediction"))
  output$twsvm<-renderPlot(plot( svm4,type="b",ylab="twstock" ,xlab="last mounth",cex=z2,col=z2,lwd=3,main="svm prediction"))


  
  
})
# your code goes here