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(10,12,8,5), activationfun = "sigm", learningrate = 0.3,momentum = 0.4, learningrate_scale = 1, output = "linear", numepochs = 30,batchsize = 100, hidden_dropout = 0, visible_dropout = 0, cd = 1)
## begin to train dbn ......
## training layer 1 rbm ...
## training layer 2 rbm ...
## training layer 3 rbm ...
## training layer 4 rbm ...
## dbn has been trained.
## begin to train deep nn ......
## deep nn has been trained.
nn.test(dnn, TestSetPredM,TestSetTargetM )
## [1] 0.26
nn.predict(dnn,TestSetPredM)
## [,1]
## 1001 0.4235511
## 1002 0.4235772
## 1003 0.4235272
## 1004 0.4235637
## 1005 0.4235680
## 1006 0.4235246
## 1007 0.4234956
## 1008 0.4234835
## 1009 0.4235069
## 1010 0.4235842
## 1011 0.4235321
## 1012 0.4235040
## 1013 0.4235294
## 1014 0.4234850
## 1015 0.4235515
## 1016 0.4235976
## 1017 0.4236324
## 1018 0.4236447
## 1019 0.4235546
## 1020 0.4235986
## 1021 0.4235028
## 1022 0.4235053
## 1023 0.4234642
## 1024 0.4234745
## 1025 0.4235104
## 1026 0.4235567
## 1027 0.4236114
## 1028 0.4236502
## 1029 0.4236056
## 1030 0.4236096
## 1031 0.4236254
## 1032 0.4234885
## 1033 0.4235031
## 1034 0.4234897
## 1035 0.4235039
## 1036 0.4235415
## 1037 0.4235210
## 1038 0.4234990
## 1039 0.4234556
## 1040 0.4234293
## 1041 0.4234681
## 1042 0.4235226
## 1043 0.4234324
## 1044 0.4235353
## 1045 0.4235596
## 1046 0.4235852
## 1047 0.4235640
## 1048 0.4235706
## 1049 0.4235475
## 1050 0.4235660
## 1051 0.4236174
## 1052 0.4235749
## 1053 0.4235305
## 1054 0.4235290
## 1055 0.4236047
## 1056 0.4235657
## 1057 0.4235684
## 1058 0.4235046
## 1059 0.4234890
## 1060 0.4234590
## 1061 0.4234918
## 1062 0.4235492
## 1063 0.4235663
## 1064 0.4235822
## 1065 0.4236003
## 1066 0.4234956
## 1067 0.4234617
## 1068 0.4234412
## 1069 0.4235285
## 1070 0.4235535
## 1071 0.4235469
## 1072 0.4235988
## 1073 0.4236252
## 1074 0.4236050
## 1075 0.4235842
## 1076 0.4235575
## 1077 0.4236241
## 1078 0.4236076
## 1079 0.4236564
## 1080 0.4236509
## 1081 0.4236806
## 1082 0.4236870
## 1083 0.4236954
## 1084 0.4236397
## 1085 0.4236007
## 1086 0.4235622
## 1087 0.4235243
## 1088 0.4235165
## 1089 0.4235607
## 1090 0.4235806
## 1091 0.4235478
## 1092 0.4234819
## 1093 0.4235706
## 1094 0.4235474
## 1095 0.4235491
## 1096 0.4234866
## 1097 0.4234784
## 1098 0.4235329
## 1099 0.4235537
## 1100 0.4236184
## 1101 0.4235759
## 1102 0.4235361
## 1103 0.4235115
## 1104 0.4234456
## 1105 0.4234657
## 1106 0.4236265
## 1107 0.4236347
## 1108 0.4236580
## 1109 0.4236608
## 1110 0.4236627
## 1111 0.4236139
## 1112 0.4236511
## 1113 0.4236532
## 1114 0.4236819
## 1115 0.4236508
## 1116 0.4236454
## 1117 0.4236109
## 1118 0.4236001
## 1119 0.4235807
## 1120 0.4235229
## 1121 0.4235116
## 1122 0.4234978
## 1123 0.4234753
## 1124 0.4235724
## 1125 0.4235236
## 1126 0.4235706
## 1127 0.4235341
## 1128 0.4235742
## 1129 0.4235914
## 1130 0.4236211
## 1131 0.4235885
## 1132 0.4236262
## 1133 0.4236365
## 1134 0.4236554
## 1135 0.4236826
## 1136 0.4236891
## 1137 0.4236925
## 1138 0.4236997
## 1139 0.4236402
## 1140 0.4236412
## 1141 0.4236581
## 1142 0.4236782
## 1143 0.4236792
## 1144 0.4236821
## 1145 0.4236468
## 1146 0.4235831
## 1147 0.4235582
## 1148 0.4236111
## 1149 0.4236538
## 1150 0.4236633
## 1151 0.4236298
## 1152 0.4236481
## 1153 0.4236740
## 1154 0.4237105
## 1155 0.4236639
## 1156 0.4236122
## 1157 0.4236207
## 1158 0.4236444
## 1159 0.4235549
## 1160 0.4235343
## 1161 0.4235669
## 1162 0.4235300
## 1163 0.4234827
## 1164 0.4235594
## 1165 0.4236084
## 1166 0.4235606
## 1167 0.4235056
## 1168 0.4235896
## 1169 0.4235039
## 1170 0.4234715
## 1171 0.4235360
## 1172 0.4235224
## 1173 0.4234473
## 1174 0.4235304
## 1175 0.4235098
## 1176 0.4235617
## 1177 0.4235761
## 1178 0.4235792
## 1179 0.4235956
## 1180 0.4236027
## 1181 0.4236199
## 1182 0.4236566
## 1183 0.4236630
## 1184 0.4236784
## 1185 0.4236835
## 1186 0.4236615
## 1187 0.4236743
## 1188 0.4236853
## 1189 0.4236659
## 1190 0.4236200
## 1191 0.4236526
## 1192 0.4235934
## 1193 0.4236109
## 1194 0.4235903
## 1195 0.4235642
## 1196 0.4236312
## 1197 0.4236506
## 1198 0.4235842
## 1199 0.4236466
## 1200 0.4236339
## 1201 0.4236669
## 1202 0.4236709
## 1203 0.4236077
## 1204 0.4235819
## 1205 0.4235469
## 1206 0.4236548
## 1207 0.4236788
## 1208 0.4236909
## 1209 0.4236982
## 1210 0.4236457
## 1211 0.4236497
## 1212 0.4235800
## 1213 0.4235415
## 1214 0.4235482
## 1215 0.4234703
## 1216 0.4234440
## 1217 0.4235675
## 1218 0.4235872
## 1219 0.4235911
## 1220 0.4235904
## 1221 0.4236236
## 1222 0.4236212
## 1223 0.4235697
## 1224 0.4236050
## 1225 0.4236343