-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathann_technical.Rmd
151 lines (119 loc) · 3.33 KB
/
ann_technical.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
title: "Annex6_Technical_NN"
author: "Alejandro Vaca"
date: "4/4/2018"
output: word_document
---
```{r}
library(readr)
btc <- read_csv('btc_usd_22_03_18.csv')
eth <- read_csv('eth_usd_22_03_18.csv')
btc <- btc[ , 2:ncol(btc)]
eth <- eth[ , 2:ncol(eth)]
colnames(btc) <- c("date", "close", "high", "low", "open", "volfrom", "volto")
colnames(eth) <- c("date", "close", "high", "low", "open", "volfrom", "volto")
library(dplyr)
btc$date <- as.Date(btc$date)
eth$date <- as.Date(eth$date)
btc1 <- btc %>%
filter(date > "2013-05-01")
eth1 <- eth %>%
filter(date > "2016-05-01")
```
First for bitcoin:
```{r}
price = btc1$close
HLC = matrix(c(btc1$high, btc1$low, btc1$close),
nrow = length(btc1$high))
library("TTR")
library(forecast)
bitcoin.lr = diff(log(price))
```
```{r}
for(i in 1:nrow(btc1)) {
if (btc1$date[i] == "2017-10-01") {
print(i)
} else {
next
}
}
```
```{r}
rsi = RSI(price)
macd = MACD(price)
macd= macd[ , 1]
will = williamsAD(HLC)
cci = CCI(HLC)
STOCH = stoch(HLC)
stochK = STOCH[,1]
stochD = STOCH[ , 1]
```
```{r}
#input and target matrix for training and validation dataset.
input = matrix(c(rsi[599:1784], cci[599:1784], #observation 600 is 2014-12-22
macd[599:1784], will[599:1784],
stochK[599:1784], stochD[599:1784]),
nrow = 1186)
target = matrix(c(bitcoin.lr[600:1785]), nrow = 1186)
date = as.Date(btc1$date[599:1784])
trainingdata = cbind(input, target)
colnames(trainingdata) = c("RSI", "CCI", "MACD", "WILL",
"STOCHK", "STOCHD","Return")
trainingdata = data.frame(trainingdata)
trainingdata$date = as.Date(date)
```
```{r}
for(i in 1:nrow(trainingdata)) {
if (trainingdata$date[i] == "2017-10-01") {
print(i)
} else {
next
}
}
```
```{r}
trainingdata = trainingdata[ , -8]
bitcoin.train = trainingdata[1:1014, ]
bitcoin.test = trainingdata[1015:nrow(trainingdata), ]
```
```{r}
#install.packages("nnet")
library(nnet)
#let´s start with neural networks
best.network = matrix(c(5, 0.5))
best.rmse = 1
for (i in 5:15)
for(j in 1:3) {
bitcoin.fit = nnet(Return ~ RSI + CCI + MACD + WILL + STOCHK + STOCHD,
data = bitcoin.train, maxit = 1000, size = i,
decay = 0.01 * j, linout = 1)
bitcoin.predict = predict(bitcoin.fit, newdata = bitcoin.test)
bitcoin.rmse = sqrt((mean(bitcoin.predict - bitcoin.lr[1614:1785])^2))
if(bitcoin.rmse < best.rmse) {
best.network[1,1] -> i
best.network[2,1] -> j
best.rmse = bitcoin.rmse
}
}
```
```{r}
bitcoin.fit = nnet(Return ~ RSI + CCI + MACD + WILL +
STOCHK + STOCHD, data = trainingdata, maxit = 1000,
size = best.network[1,1], decay = 0.1*best.network[2,1],
linout = 1)
bitcoin.predict1 = predict(bitcoin.fit, newdata = bitcoin.test)
for ( i in 1:20) {
bitcoin.fit = nnet(Return ~ RSI + CCI + MACD + WILL +
STOCHK + STOCHD, data = trainingdata, maxit = 1000,
size = best.network[1,1], decay = 0.1*best.network[2,1],
linout = 1)
bitcoin.predict = predict(bitcoin.fit, newdata = bitcoin.test)
bitcoin.predict1 = (bitcoin.predict1 + bitcoin.predict) / 2
}
```
```{r}
plot(bitcoin.predict1, type = "l")
```
```{r}
write.csv(bitcoin.predict1, "ann_bitcoin.csv")
```