library(wikipediatrend)

library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
library(AUC)
## AUC 0.3.0
## Type AUCNews() to see the change log and ?AUC to get an overview.
## 
## Attaching package: 'AUC'
## 
## The following objects are masked from 'package:caret':
## 
##     sensitivity, specificity
library(deepnet)

views<-wp_trend(page = "Citigroup",from = "2010-01-01",to = "2014-12-31",lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)
## 
## Results written to:
## /home/mitra2/git/wp__Citigroup__en.csv
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
startDate = as.Date("2010-01-01")

endDate = as.Date("2014-12-31") 

getSymbols("c", src = "yahoo", from = startDate, to = endDate) 
##     As of 0.4-0, 'getSymbols' uses env=parent.frame() and
##  auto.assign=TRUE by default.
## 
##  This  behavior  will be  phased out in 0.5-0  when the call  will
##  default to use auto.assign=FALSE. getOption("getSymbols.env") and 
##  getOptions("getSymbols.auto.assign") are now checked for alternate defaults
## 
##  This message is shown once per session and may be disabled by setting 
##  options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
## [1] "C"
RSI3<-RSI(Op(C), n= 3) 
#Calculate a 3-period relative strength index (RSI) off the open price

EMA5<-EMA(Op(C),n=5) 
#Calculate a 5-period exponential moving average (EMA)
EMAcross<- Op(C)-EMA5 
#Let’s explore the difference between the open price and our 5-period EMA


DEMA10<-DEMA(Cl(C),n = 10, v = 1, wilder = FALSE)
DEMA10c<-Cl(C) - DEMA10



MACD<-MACD(Op(C),fast = 12, slow = 26, signal = 9) 
#Calculate a MACD with standard parameters

MACDsignal<-MACD[,2] 
#Grab just the signal line to use as our indicator.


SMI<-SMI(Op(C),n=13,slow=25,fast=2,signal=9) 
#Stochastic Oscillator with standard parameters
SMI<-SMI[,1] 
#Grab just the oscillator to use as our indicator

BB<-BBands(Op(C),n=20,sd=2)
BBp<-BB[,4]


CCI20<-CCI(C[,3:5],n=20)
#A 20-period Commodity Channel Index calculated of the High/Low/Close of our data



PriceChange<- Cl(C) - Op(C) 
#Calculate the difference between the close price and open price
Class<-ifelse(PriceChange>0,1,0) 
#Create a binary classification variable, the variable we are trying to predict.

DJIADF<-data.frame(date = index(C), C, row.names=NULL)


CombDF<-merge(views,DJIADF, by.x='date', by.y='date')

DataSet<-data.frame(RSI3,EMAcross,MACDsignal,SMI,BBp,CCI20,DEMA10c) 

DataSet<-DataSet[-c(1:33),] 

Alldata<-cbind(DataSet,CombDF[34:1258,2])


Normalized <-function(x) {(x-min(x))/(max(x)-min(x))}
NormalizedData<-as.data.frame(lapply(Alldata,Normalized))



ClassDF<-data.frame(date = index(Class), Class, row.names=NULL)

ClassDFN<-ClassDF[34:1258,2]
TrainingSetPred<-NormalizedData[1:1000,] 
TrainingSetTarget<-ClassDFN[1:1000] 

TrainingSetPredM <- as.matrix(TrainingSetPred)
TrainingSetTargetM<-as.matrix(TrainingSetTarget)


TestSetPred<-NormalizedData[1001:1225,] 
TestSetTarget<-ClassDFN[1001:1225] 

TestSetPredM <- as.matrix(TestSetPred)
TestSetTargetM<-as.matrix(TestSetTarget)
dnn <- dbn.dnn.train(TrainingSetPredM, TrainingSetTargetM, hidden = c(400), activationfun = "sigm", learningrate = 0.8,momentum = 0.5, learningrate_scale = 1, output = "linear", numepochs = 3,batchsize = 100, hidden_dropout = 0, visible_dropout = 0, cd = 1)
## begin to train dbn ......
## training layer 1 rbm ...
## dbn has been trained.
## begin to train deep nn ......
## deep nn has been trained.
nn.test(dnn, TestSetPredM,TestSetTargetM )
## [1] 0.24
nn.predict(dnn,TestSetPredM)
##           [,1]
## 1001 0.5126766
## 1002 0.5126768
## 1003 0.5126762
## 1004 0.5126767
## 1005 0.5126768
## 1006 0.5126764
## 1007 0.5126756
## 1008 0.5126751
## 1009 0.5126760
## 1010 0.5126769
## 1011 0.5126766
## 1012 0.5126758
## 1013 0.5126766
## 1014 0.5126753
## 1015 0.5126767
## 1016 0.5126770
## 1017 0.5126771
## 1018 0.5126771
## 1019 0.5126767
## 1020 0.5126770
## 1021 0.5126760
## 1022 0.5126764
## 1023 0.5126736
## 1024 0.5126737
## 1025 0.5126757
## 1026 0.5126768
## 1027 0.5126771
## 1028 0.5126771
## 1029 0.5126770
## 1030 0.5126770
## 1031 0.5126771
## 1032 0.5126767
## 1033 0.5126762
## 1034 0.5126754
## 1035 0.5126759
## 1036 0.5126766
## 1037 0.5126763
## 1038 0.5126757
## 1039 0.5126727
## 1040 0.5126686
## 1041 0.5126733
## 1042 0.5126760
## 1043 0.5126674
## 1044 0.5126767
## 1045 0.5126768
## 1046 0.5126769
## 1047 0.5126768
## 1048 0.5126768
## 1049 0.5126767
## 1050 0.5126768
## 1051 0.5126770
## 1052 0.5126769
## 1053 0.5126766
## 1054 0.5126766
## 1055 0.5126770
## 1056 0.5126768
## 1057 0.5126769
## 1058 0.5126760
## 1059 0.5126757
## 1060 0.5126737
## 1061 0.5126754
## 1062 0.5126767
## 1063 0.5126768
## 1064 0.5126769
## 1065 0.5126770
## 1066 0.5126760
## 1067 0.5126736
## 1068 0.5126702
## 1069 0.5126762
## 1070 0.5126766
## 1071 0.5126766
## 1072 0.5126770
## 1073 0.5126771
## 1074 0.5126770
## 1075 0.5126770
## 1076 0.5126768
## 1077 0.5126771
## 1078 0.5126771
## 1079 0.5126771
## 1080 0.5126771
## 1081 0.5126771
## 1082 0.5126771
## 1083 0.5126771
## 1084 0.5126771
## 1085 0.5126771
## 1086 0.5126770
## 1087 0.5126766
## 1088 0.5126765
## 1089 0.5126769
## 1090 0.5126770
## 1091 0.5126768
## 1092 0.5126755
## 1093 0.5126769
## 1094 0.5126767
## 1095 0.5126768
## 1096 0.5126754
## 1097 0.5126741
## 1098 0.5126765
## 1099 0.5126768
## 1100 0.5126770
## 1101 0.5126769
## 1102 0.5126765
## 1103 0.5126761
## 1104 0.5126723
## 1105 0.5126733
## 1106 0.5126771
## 1107 0.5126771
## 1108 0.5126771
## 1109 0.5126771
## 1110 0.5126771
## 1111 0.5126770
## 1112 0.5126771
## 1113 0.5126771
## 1114 0.5126771
## 1115 0.5126771
## 1116 0.5126771
## 1117 0.5126771
## 1118 0.5126771
## 1119 0.5126770
## 1120 0.5126767
## 1121 0.5126765
## 1122 0.5126762
## 1123 0.5126753
## 1124 0.5126769
## 1125 0.5126763
## 1126 0.5126769
## 1127 0.5126765
## 1128 0.5126769
## 1129 0.5126770
## 1130 0.5126771
## 1131 0.5126770
## 1132 0.5126771
## 1133 0.5126771
## 1134 0.5126771
## 1135 0.5126771
## 1136 0.5126771
## 1137 0.5126771
## 1138 0.5126771
## 1139 0.5126771
## 1140 0.5126771
## 1141 0.5126771
## 1142 0.5126771
## 1143 0.5126771
## 1144 0.5126771
## 1145 0.5126771
## 1146 0.5126770
## 1147 0.5126770
## 1148 0.5126771
## 1149 0.5126771
## 1150 0.5126771
## 1151 0.5126771
## 1152 0.5126771
## 1153 0.5126771
## 1154 0.5126771
## 1155 0.5126771
## 1156 0.5126771
## 1157 0.5126771
## 1158 0.5126771
## 1159 0.5126769
## 1160 0.5126767
## 1161 0.5126770
## 1162 0.5126767
## 1163 0.5126756
## 1164 0.5126769
## 1165 0.5126770
## 1166 0.5126769
## 1167 0.5126764
## 1168 0.5126770
## 1169 0.5126762
## 1170 0.5126748
## 1171 0.5126769
## 1172 0.5126768
## 1173 0.5126725
## 1174 0.5126764
## 1175 0.5126757
## 1176 0.5126768
## 1177 0.5126769
## 1178 0.5126769
## 1179 0.5126770
## 1180 0.5126770
## 1181 0.5126771
## 1182 0.5126771
## 1183 0.5126771
## 1184 0.5126771
## 1185 0.5126771
## 1186 0.5126771
## 1187 0.5126771
## 1188 0.5126771
## 1189 0.5126771
## 1190 0.5126771
## 1191 0.5126771
## 1192 0.5126770
## 1193 0.5126771
## 1194 0.5126770
## 1195 0.5126769
## 1196 0.5126771
## 1197 0.5126771
## 1198 0.5126770
## 1199 0.5126771
## 1200 0.5126771
## 1201 0.5126771
## 1202 0.5126771
## 1203 0.5126770
## 1204 0.5126770
## 1205 0.5126770
## 1206 0.5126771
## 1207 0.5126771
## 1208 0.5126771
## 1209 0.5126771
## 1210 0.5126771
## 1211 0.5126771
## 1212 0.5126770
## 1213 0.5126769
## 1214 0.5126769
## 1215 0.5126759
## 1216 0.5126738
## 1217 0.5126769
## 1218 0.5126770
## 1219 0.5126770
## 1220 0.5126770
## 1221 0.5126770
## 1222 0.5126770
## 1223 0.5126769
## 1224 0.5126770
## 1225 0.5126771