-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stock_simulation.Rmd
295 lines (225 loc) · 6.84 KB
/
Stock_simulation.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
---
title: "Simulacao baseada em ECOR.SA"
author: "JOAO VICTOR"
date: "Agosto 01, 2020"
output:
html_document:
fig_height: 4
highlight: pygments
theme: spacelab
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r include=FALSE}
#library(tidyverse)
#library(TTR)
library(quantmod)
#library(PerformanceAnalytics)
library(RColorBrewer)
#library(tseries)
#library(lubridate)
library(Quandl)
#library(ggplot2)
Quandl.api_key("Ns8L-9dpHErbn-S_V6ny")
options("getSymbols.yahoo.warning"=FALSE)
```
# Importa-se os dados sobre a acao ECOR3.SA e cria- se as variaveis referentes a var diaria e valor de fechamento
```{r}
eco <- getSymbols("ECOR3.SA", auto.assign = F)
eco_cl <- eco$ECOR3.SA.Close
eco$ECOR3.SA.Change <- eco$ECOR3.SA.High - eco$ECOR3.SA.Low
eco_ch <- eco$ECOR3.SA.Change
```
# Ambos variacoes entre dias e variacao diarias serao modeladas como normais
```{r}
summary(eco_ch$ECOR3.SA.Change)
up.down <- eco_ch$ECOR3.SA.Change/2
sd(up.down, na.rm = TRUE)
daily_change_eco <- eco_cl/(stats::lag(eco_cl, 1)) - 1
summary(daily_change_eco)
sd(daily_change_eco, na.rm = TRUE)
```
# Modela-se agora o comportamento de ambas, diariamente e ao longo dos dias.
```{r}
valor.Open <- c(10)
valor.Close <- c(NULL)
variacao <- c(NULL)
alta.i <- valor.Open[1] + abs(rnorm(1, mean = 0.384, sd = 0.102))
baixa.i <- valor.Open[1] - abs(rnorm(1, mean = 0.384, sd = 0.102))
valor.High <- c(alta.i)
valor.Low <- c(baixa.i)
for(i in 2:length(eco_cl)){
variacao[i] <- rnorm(1, mean = 0.000552, sd = 0.02479242)
valor.Open[i] = valor.Open[i-1] * (variacao[i] + 1)
valor.Close[i-1] = valor.Open[i]
valor.High[i] = valor.Open[i] + abs(rnorm(1, mean = 0.384, sd = 0.102))
valor.Low[i] = valor.Open[i] - abs(rnorm(1, mean = 0.384, sd = 0.102))
}
options(digits=3)
valor.Close[length(valor.Close) + 1] <- valor.High[length(valor.High)]
valor.Open <- reclass(valor.Open, eco_cl)
valor.Low <- reclass(valor.Low, eco_cl)
valor.Close <- reclass(valor.Close, eco_cl)
valor.High <- reclass(valor.High, eco_cl)
Acaosimulada <- merge(valor.Open, valor.High, valor.Low, valor.Close)
chartSeries(Acaosimulada,
type = "line",
TA = c(addBBands(n = 20, sd = 2),addRSI()),
theme = chartTheme("white"))
chartSeries(Acaosimulada,
subset = "2011-01-01::2011-05-01",
type = "candlesticks",
TA = c(addBBands(n = 20, sd = 2),addRSI()),
theme = chartTheme("black"))
chartSeries(eco,
subset = "2011-01-01::2011-05-01",
type = "candlesticks",
TA = c(addBBands(n = 20, sd = 2),addRSI()),
theme = chartTheme("black"))
chartSeries(Acaosimulada,
subset = "2017-01-01::2017-05-01",
type = "candlesticks",
TA = c(addBBands(n = 20, sd = 2),addRSI()),
theme = chartTheme("black"))
chartSeries(eco,
subset = "2017-01-01::2017-05-01",
type = "candlesticks",
TA = c(addBBands(n = 20, sd = 2),addRSI()),
theme = chartTheme("black"))
```
# METODO 1: ACAO PELAS BANDAS
As variaveis necessarias para os sinais de compra e venda estao aqui definidas
```{r}
SE <- BBands(Acaosimulada$valor.Close, n = 20, sd = 2)
SE <- SE[-c(1:19),]
Acaosimulada <- Acaosimulada[-c(1:19),]
sinal1 <- c(NULL)
sinal2 <- c(NULL)
dim(SE)
dim(Acaosimulada)
```
Cria-se aqui os sinais de compra e venda
```{r}
for(i in 2:length(Acaosimulada$valor.Close)) {
if(Acaosimulada[i,4] > SE$up[i]){
sinal2[i] <- 1
}else
sinal2[i] <- 0
}
for(i in 2:length(Acaosimulada$valor.Close)) {
if(Acaosimulada[i,4] < SE$dn[i]){
sinal1[i] <- 1
}else
sinal1[i] <- 0
}
chartSeries(Acaosimulada,
type = "line",
theme = chartTheme("white"))
sinal2 <- reclass(sinal2, Acaosimulada)
sinal1 <- reclass(sinal1, Acaosimulada)
addTA(sinal2, type = "S", col = "red")
addTA(sinal1, type = "S", col = "green")
```
# Modelando um investimento
```{r}
#Montagem da acao
eco <- getSymbols("ECOR3.SA", auto.assign = F)
eco_cl <- eco$ECOR3.SA.Close
eco$ECOR3.SA.Change <- eco$ECOR3.SA.High - eco$ECOR3.SA.Low
eco_ch <- eco$ECOR3.SA.Change
TOTAL <- c()
for(vezes in 1:500){
valor.Open <- c(10)
valor.Close <- c(NULL)
variacao <- c(NULL)
alta.i <- valor.Open[1] + abs(rnorm(1, mean = 0.384, sd = 0.102))
baixa.i <- valor.Open[1] - abs(rnorm(1, mean = 0.384, sd = 0.102))
valor.High <- c(alta.i)
valor.Low <- c(baixa.i)
for(i in 2:length(eco_cl)){
variacao[i] <- rnorm(1, mean = 0.000552, sd = 0.02479242)
valor.Open[i] = valor.Open[i-1] * (variacao[i] + 1)
valor.Close[i-1] = valor.Open[i]
valor.High[i] = valor.Open[i] + abs(rnorm(1, mean = 0.384, sd = 0.102))
valor.Low[i] = valor.Open[i] - abs(rnorm(1, mean = 0.384, sd = 0.102))
}
options(digits=3)
valor.Close[length(valor.Close) + 1] <- valor.High[length(valor.High)]
valor.Open <- reclass(valor.Open, eco_cl)
valor.Low <- reclass(valor.Low, eco_cl)
valor.Close <- reclass(valor.Close, eco_cl)
valor.High <- reclass(valor.High, eco_cl)
Acaosimulada <- merge(valor.Open, valor.High, valor.Low, valor.Close)
# metodo 1
SE <- BBands(Acaosimulada$valor.Close, n = 20, sd = 2)
SE <- SE[-c(1:19),]
Acaosimulada <- Acaosimulada[-c(1:19),]
sinal1 <- c(NULL)
sinal2 <- c(NULL)
for(i in 2:length(Acaosimulada$valor.Close)) {
if(Acaosimulada[i,4] > SE$up[i]){
sinal2[i] <- 1
}else
sinal2[i] <- 0
}
for(i in 2:length(Acaosimulada$valor.Close)) {
if(Acaosimulada[i,4] < SE$dn[i]){
sinal1[i] <- 1
}else
sinal1[i] <- 0
}
sinal2 <- reclass(sinal2, Acaosimulada)
sinal1 <- reclass(sinal1, Acaosimulada)
#Agora o investimento
options(scipen = 9999)
Acaosimulada <- Acaosimulada[-c(1),]
sinal2 <- sinal2[-c(1),]
sinal1 <- sinal1[-c(1),]
variacao <- variacao[-c(1)]
meudinheiro <- 5000
investido <- 5000
soma <- c()
passos <- c()
for(i in 1:(length(Acaosimulada$valor.Close))){
soma[i] <- investido + meudinheiro
investido <- investido + (as.numeric(variacao[i]) * investido)
passos[i] <- investido + meudinheiro
if(as.numeric(sinal1[i,1]) == 1){
meudinheiro <- meudinheiro - as.numeric(Acaosimulada$valor.Close[i])
investido <- investido + as.numeric(Acaosimulada$valor.Close[i])
} else if(investido >= as.numeric(Acaosimulada$valor.Close[i])) {
if(as.numeric(sinal2[i,1]) == 1) {
meudinheiro <- meudinheiro + as.numeric(Acaosimulada$valor.Close[i])
investido <- investido - as.numeric(Acaosimulada$valor.Close[i])
} else {
}
} else{
}
}
TOTAL[vezes] <- meudinheiro + investido
if(vezes > 1){
if(TOTAL[vezes] > TOTAL[as.numeric(vezes)-1]){
variacaomax <- variacao
passosmax <- passos
somamax <- soma
}
}
}
```
```{r}
preco <- c(10)
for(i in 2:length(variacaomax)){
preco[i] <- preco[i-1] * (1 + variacaomax[i-1])
}
head(preco)
tail(preco)
```
```{r}
plot(preco, type = "line")
```
```{r}
plot(TOTAL)
hist(TOTAL, breaks = 500, xlim = c(0,50000))
summary(TOTAL)
```