/
bonusandsplit.Rmd
172 lines (144 loc) · 5.04 KB
/
bonusandsplit.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
```{r}
# path
path = "E:/StockThing"
knitr::opts_knit$set(root.dir = path)
setwd(path)
```
```{r}
library(dplyr)
library(ggplot2)
library(plotly)
```
```{r}
data <- read.csv("complete.csv")
```
```{r}
head(data)
tail(data)
```
```{r}
#convert the date column into suitable format
data$date <- as.Date(as.character(data$date), format = "%Y%m%d")
```
```{r}
#get the splits information
splits <- read.csv("splits.csv")
splits <- na.omit(splits)
splits$date <- as.Date(as.character(splits$date), format = "%d-%m-%Y")
```
```{r}
#get the names of all the companies
companies <- unique(data$name)
```
```{r}
#find out all the companies which are currently being traded as of 2019
comp <- c()
for(company in companies) {
d <- data %>% filter(name == company)
latest <- max(d$date)
y <- as.numeric(format(latest,'%Y'))
if(y == 2019) {
comp <- c(comp, company)
}
}
```
```{r}
#to find whether the companies which are currently being traded have data for all the years from the day that they were listed on the exchange
#if not, then drop the companies
comp_with_all_data <- c()
for(company in comp) {
d <- data %>% filter(name == company)
latest <- max(d$date)
earliest <- min(d$date)
year_latest <- as.numeric(format(latest,'%Y'))
year_earliest <- as.numeric(format(earliest, '%Y'))
years <- seq(year_earliest, year_latest)
y <- format(d['date'], "%Y")
flag <- 1
for(val in years) {
c <- sum(y == val)
if(c == 0) {
flag <- 0
}
}
if(flag == 1) {
comp_with_all_data <- c(comp_with_all_data, company)
}
}
```
```{r}
data <- data %>% filter(name %in% comp)
```
```{r}
#filter out the splits records to take into account only those companies which we have from the previous step
comp <- comp_with_all_data
splits <- splits %>% filter(code %in% comp)
```
```{r}
for(company in comp) {
values <- splits %>% filter(code == company)
dates_splits <- values %>% select('date')
ratio <- 1
if(nrow(values) > 0) {
d <- data %>% filter(name == company, date <= dates_splits[[1]])
for(row in nrow(d):1) {
date_curr_row <- d[row, 'date']
if(date_curr_row %in% dates_splits) {
find <- values %>% filter(date == date_curr_row)
ratio <- ratio * find[1, "old_fv"]/find[1, "new_fv"]
}
data[(data$date == date_curr_row & data$name == company), 'open'] <- data[(data$date == date_curr_row & data$name == company), 'open']/ratio
data[(data$date == date_curr_row & data$name == company), 'high'] <- data[(data$date == date_curr_row & data$name == company), 'high']/ratio
data[(data$date == date_curr_row & data$name == company), 'low'] <- data[(data$date == date_curr_row & data$name == company), 'low']/ratio
data[(data$date == date_curr_row & data$name == company), 'close'] <- data[(data$date == date_curr_row & data$name == company), 'close']/ratio
}
}
}
```
```{r}
#change 1 to nrow(values) in previous markdown cell
#wip <- data %>% filter(name == 'WIPRO')
#obj <- ggplot(wip, aes(date, open)) + geom_line() + ggtitle("ABB Stock Price Variation")
#ggplotly(obj)
```
```{r}
bonuses <- read.csv("combinedbonuses.csv")
names(bonuses) <- c("name", "code", "ratio", "ann", "date", "ex")
bonuses$ann <- as.Date(as.character(bonuses$ann), format = "%d-%m-%Y")
bonuses$date <- as.Date(as.character(bonuses$date), format = "%d-%m-%Y")
bonuses$ex <- as.Date(as.character(bonuses$ex), format = "%d-%m-%Y")
bonuses <- bonuses[!(is.na(bonuses$code) | bonuses$code==""), ]
bonuses <- na.omit(bonuses)
```
```{r}
vect <- c()
bonuses$ratio <- as.character(bonuses$ratio)
for(i in 1:nrow(bonuses)) {
trial <- bonuses[i,]['ratio']
numbers <- strsplit(trial[[1]], "[:]")
vect <- c(vect, (as.integer(numbers[[1]][1]) + as.integer(numbers[[1]][2]))/(as.integer(numbers[[1]][2])))
}
bonuses$mult <- vect
```
```{r}
bonuses <- bonuses %>% filter(code %in% comp)
for(company in comp) {
bonus <- bonuses %>% filter(code == company)
dates_bonus <- bonus %>% select('date')
ratio <- 1
if(nrow(values) > 0) {
d <- data %>% filter(name == 'WIPRO', date <= bonus[[nrow(bonus), 'date']])
for(row in nrow(d):1) {
date_curr_row <- d[row, 'date']
if(any(dates_bonus$date == date_curr_row)) {
find <- bonus %>% filter(date == date_curr_row)
ratio <- ratio * find[1, "mult"]
}
data[(data$date == date_curr_row & data$name == company), 'open'] <- data[(data$date == date_curr_row & data$name == company), 'open']/ratio
data[(data$date == date_curr_row & data$name == company), 'high'] <- data[(data$date == date_curr_row & data$name == company), 'high']/ratio
data[(data$date == date_curr_row & data$name == company), 'low'] <- data[(data$date == date_curr_row & data$name == company), 'low']/ratio
data[(data$date == date_curr_row & data$name == company), 'close'] <- data[(data$date == date_curr_row & data$name == company), 'close']/ratio
}
}
}
```