The objective of this project is to explore the relationship among the variables S&P500, CBOE Volatility, Crude Oil Price, Currency( Real Vs Dolar) and Bovespa Volume with the BOVESPA index, the main Brazilian stocks index.
library(quantmod)
library(dplyr)
library(ggplot2)
library(caret)
library(neuralnet)
b <- 1000
start <- as.Date(Sys.Date()- b)
end <- as.Date(Sys.Date())
Symbol <- c ( "^BVSP", "^GSPC", "^VIX")
getSymbols(Symbol, from=start , to=end)
## Warning: ^BVSP contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## [1] "BVSP" "GSPC" "VIX"
data <- merge(BVSP, Cl(GSPC),Cl(VIX))
date = index(data)
data <- data.frame(date,data)
data <- select(data, date,BVSP.Close,BVSP.Volume, GSPC.Close, VIX.Close)
colnames(data) <- c("date","BVSP", "BVSP_Volume", "SP500", "Volatility")
tail(data)
## date BVSP BVSP_Volume SP500 Volatility
## 2018-05-02 2018-05-02 84547 3720600 2635.67 15.97
## 2018-05-03 2018-05-03 83288 3977800 2629.73 15.90
## 2018-05-04 2018-05-04 83118 3472900 2663.42 14.77
## 2018-05-07 2018-05-07 82714 2843000 2672.63 14.75
## 2018-05-08 2018-05-08 82956 3949500 2671.92 14.71
## 2018-05-09 2018-05-09 84265 4695400 2697.79 13.42
getSymbols("BRL=X",src="yahoo",from = start)
## Warning: BRL=X contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## [1] "BRL=X"
currency <-`BRL=X`
colnames(currency) <- c("open", "high", "low", "close","volume","ad")
date = index(currency)
currency <- data.frame(date,currency)
currency <- select(currency,date,close)
colnames(currency) <- c("date","dolar")
tail(currency)
## date dolar
## 2018-05-02 2018-05-02 3.5496
## 2018-05-03 2018-05-03 3.5266
## 2018-05-06 2018-05-06 3.5255
## 2018-05-07 2018-05-07 3.5492
## 2018-05-08 2018-05-08 3.5619
## 2018-05-10 2018-05-10 3.5423
getSymbols('DCOILWTICO',src='FRED')
## [1] "DCOILWTICO"
oil <- na.locf(DCOILWTICO)
date = index(oil)
oil<- data.frame(date, oil)
oil<- subset(oil, date > start & date < end)
colnames(oil) <- c("date","oil")
tail(oil)
## date oil
## 2018-04-30 2018-04-30 68.56
## 2018-05-01 2018-05-01 67.28
## 2018-05-02 2018-05-02 67.91
## 2018-05-03 2018-05-03 68.45
## 2018-05-04 2018-05-04 69.71
## 2018-05-07 2018-05-07 70.74
total <- merge(data,currency, by="date")
total <- merge(total,oil, by="date")
total <- na.omit(total)
tail(total)
## date BVSP BVSP_Volume SP500 Volatility dolar oil
## 622 2018-04-25 85044 3218400 2639.40 17.84 3.4809 68.00
## 623 2018-04-26 86383 3138000 2666.94 16.24 3.4742 68.18
## 624 2018-04-30 86115 2181000 2648.05 15.93 3.5058 68.56
## 626 2018-05-02 84547 3720600 2635.67 15.97 3.5496 67.91
## 627 2018-05-03 83288 3977800 2629.73 15.90 3.5266 68.45
## 628 2018-05-07 82714 2843000 2672.63 14.75 3.5492 70.74
procValues <- preProcess(total, method = c("range"))
totalnorm <- predict(procValues, total)
set.seed(1000)
inTrain <- createDataPartition(totalnorm$BVSP, p=0.70, list=FALSE)
trainData <- totalnorm[inTrain,]
testData <- totalnorm[-inTrain,]
trainData <- select(trainData, -date)
testData <- select(testData, -date)
tail(trainData)
## BVSP BVSP_Volume SP500 Volatility dolar oil
## 620 0.9591076 0.2196131 0.8059188 0.2278481 0.3510277 0.9297419
## 621 0.9564559 0.2575430 0.7716878 0.2810126 0.3712243 0.9308642
## 623 0.9746790 0.2840667 0.8027092 0.2246835 0.3736372 0.9425365
## 624 0.9693357 0.1974345 0.7846118 0.2148734 0.4018767 0.9510662
## 627 0.9129715 0.3600894 0.7670603 0.2139240 0.4204647 0.9485971
## 628 0.9015272 0.2573619 0.8081605 0.1775316 0.4406613 1.0000000
tail(testData)
## BVSP BVSP_Volume SP500 Volatility dolar oil
## 615 0.9559574 0.2568188 0.7998830 0.2958861 0.3171582 0.9176207
## 616 0.9044780 0.2637349 0.8131521 0.2348101 0.3217158 0.8987654
## 618 0.9625768 0.3799234 0.8426597 0.2044304 0.2868633 0.9483726
## 619 0.9635338 0.3188735 0.8278004 0.2158228 0.2918677 0.9452301
## 622 0.9479823 0.2913449 0.7763246 0.2753164 0.3796247 0.9384961
## 626 0.9380732 0.3368065 0.7727511 0.2161392 0.4410188 0.9364759
set.seed(1000)
set.seed(1000)
neu <- neuralnet(BVSP ~ BVSP_Volume + SP500 + Volatility + dolar + oil,
data = trainData, hidden = c(3,2), linear.output = FALSE)
plot(neu, rep = "best")
## Results Matrix Summary
neu$result.matrix
## 1
## error 0.471839073613
## reached.threshold 0.009135321908
## steps 1124.000000000000
## Intercept.to.1layhid1 -3.457196981680
## BVSP_Volume.to.1layhid1 -1.089483456670
## SP500.to.1layhid1 1.204513331975
## Volatility.to.1layhid1 8.555306083709
## dolar.to.1layhid1 0.250242528789
## oil.to.1layhid1 1.191301060462
## Intercept.to.1layhid2 1.877987019740
## BVSP_Volume.to.1layhid2 2.850036784101
## SP500.to.1layhid2 1.839181910447
## Volatility.to.1layhid2 -4.006089938808
## dolar.to.1layhid2 -2.173683880370
## oil.to.1layhid2 1.314238168677
## Intercept.to.1layhid3 1.085489977839
## BVSP_Volume.to.1layhid3 -0.549913661577
## SP500.to.1layhid3 -2.057903907342
## Volatility.to.1layhid3 1.662816342260
## dolar.to.1layhid3 1.128081647969
## oil.to.1layhid3 -0.484010393748
## Intercept.to.2layhid1 -1.911757664173
## 1layhid.1.to.2layhid1 1.062369486046
## 1layhid.2.to.2layhid1 3.869745270970
## 1layhid.3.to.2layhid1 -3.139384851191
## Intercept.to.2layhid2 0.124904946614
## 1layhid.1.to.2layhid2 7.299787817784
## 1layhid.2.to.2layhid2 0.193243339913
## 1layhid.3.to.2layhid2 -9.459346712574
## Intercept.to.BVSP -2.604651867527
## 2layhid.1.to.BVSP 4.312357186461
## 2layhid.2.to.BVSP 2.471399714259
Note: This activity has a purely didactic proposal for Big Data Analisys, utilization for investment proposals are not permitted without author’s approval.