forked from ckozzyy/StockMarketSuccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCocaColaData.R
129 lines (102 loc) · 4.66 KB
/
CocaColaData.R
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
#Installing quantmod annd other libraries
install.packages("quantmod")
install.packages("TimeWarp")
install.packages("xts")
install.packages("forecast")
install.packages("zoo")
install.packages("lubridate")
install.packages("neuralnet")
install.packages("BBmisc")
library(quantmod)
library(TimeWarp)
library("xts")
library("forecast")
library("zoo")
library("lubridate")
library("neuralnet")
#Task: get data into file
getSymbols(Symbols = "KO", auto.assign = TRUE)
plot(KO$KO.Close)
#Holtwinters
KO.pred <- HoltWinters(KO$KO.Close, beta = FALSE, gamma = FALSE)
KO.pred
plot(KO.pred)
install.packages("forecast")
library("forecast")
KO.pred2 <- forecast:::forecast.HoltWinters(KO.pred, h = 100)
KO.pred2
forecast:::plot.forecast(KO.pred2)
#Linear model
#Filtering for all of the relevant data.
#Getting rid of all of the "noise"
subset = KO[1500:nrow(KO),]
# Work with subset from now on. Chart subset (note I removed
# subset argument from call to chartSeries)
chartSeries(subset, TA = NULL, theme = "white", up.col = "green", dn.col = "red")
# Linear model on same range as your chart
indices = 1:nrow(subset)
model=lm(KO.Close~indices,data=subset)
# Draw line
abline(model$coefficients[1],model$coefficients[2])
#neural networks
#Convert to dataframe + Clean
KO.df <- data.frame("Open" = KO$KO.Open, "Close" = KO$KO.Close, "Year"=as.numeric(format(index(KO), "%Y")),
"Month"=as.numeric(format(index(KO), "%m")), "Day"=as.numeric(format(index(KO), "%d")),
"Is_Month_Start" = FALSE, "Is_Month_End" = FALSE, "Is_Year_End" = FALSE,
"Is_Year_Start" = FALSE, "Is_Quarter_Start" = FALSE, "Is_Quarter_End" = FALSE,
"Quarters"=quarters(index(KO)))
for (i in 1:nrow(KO.df)){
date <- as.POSIXlt(paste(KO.df[i,]$Year, "-", KO.df[i,]$Month, "-", KO.df[i,]$Day, sep=""))
#Determine if it is beginning/end of the month and change dataset accordingly
if (KO.df[i,]$Day <= 7){ #7 is the first ~25% of the month
KO.df[i,]$Is_Month_Start = TRUE
} else if (KO.df[i,]$Day > 24){ #24 is the last ~25% of the month
KO.df[i,]$Is_Month_End = TRUE
}
#Determine if it is the beginning/end of the year and change dataset accordingly
if (KO.df[i,]$Quarters == 'Q1'){ #Q1 is the first ~25% of the year
KO.df[i,]$Is_Year_Start = TRUE
} else if (KO.df[i,]$Quarters == 'Q4'){ #Q4 is the last ~25% of the year
KO.df[i,]$Is_Year_End = TRUE
}
#Determine if it is the beginning of the quarter and change dataset accordingly
if (yday(date) <= 22){ #days 1-22 is the first ~25% of Q1
KO.df[i,]$Is_Quarter_Start = TRUE
} else if (yday(date) > 91 & yday(date) <= 113){ #days 91-113 is the first ~25% of Q2
KO.df[i,]$Is_Quarter_Start = TRUE
} else if (yday(date) > 182 & yday(date) <=204){ #days 182-204 is the first ~25% of Q3
KO.df[i,]$Is_Quarter_Start = TRUE
} else if (yday(date) > 273 & yday(date) <=296){ #days 273-296 is the first ~25% of Q4
KO.df[i,]$Is_Quarter_Start = TRUE
}
#Determine if it is the end of the quarter and change dataset accordingly
if (yday(date) <= 91 & yday(date) >= 69){ #days 69-91 is the last ~25% of Q1
KO.df[i,]$Is_Quarter_End = TRUE
} else if (yday(date) <= 182 & yday(date) >= 160){ #days 160-182 is the last ~25% of Q2
KO.df[i,]$Is_Quarter_End = TRUE
} else if (yday(date) <= 273 & yday(date) >= 251){ #days 251-273 is the last ~25% of Q3
KO.df[i,]$Is_Quarter_End = TRUE
} else if (yday(date) <= 366 & yday(date) >= 343){ #days 343-365 is the last ~25% of Q4
KO.df[i,]$Is_Quarter_End = TRUE
}
}
KO.df <- KO.df[,1:11]#Remove the last column, it was only used to clean data
#change true/false values into 1/0s
cols <- sapply(KO.df, is.logical)
KO.df[,cols] <- lapply(KO.df[,cols], as.numeric)
#normalize dataset
KO.df <- BBmisc::normalize(KO.df)
#Create training and testing data sets
KO.train <- KO.df[1:(.75 * nrow(KO.df)),] #75% of data set
KO.test <- KO.df[(.75 * nrow(KO.df)):nrow(KO.df),]
#Predict with NeuralNet.predict()
KO.NN <- neuralnet(KO.Close ~ KO.Open + Year + Month + Day + Is_Month_Start + Is_Month_End + Is_Year_End +
Is_Year_Start + Is_Quarter_Start + Is_Quarter_End
, data = KO.train, linear.output=TRUE, hidden=c(5,3,3))
plot(KO.NN)
KO.pred <- predict(KO.NN, KO.test)
KO.pred.total <- c(KO.train$KO.Close, KO.pred)
plot(KO.pred.total, xlab="Number of Days recorded with Quantmod", ylab="Predicted Stock Value Normalized", main="Normalized Stock Predictions of KO")
abline(v = (.75 * nrow(KO.df)), col='red')
plot(KO.df$KO.Close, xlab="Number of Days recorded with Quantmod", ylab="Actual Stock Value Normalized", main="Actual Stock Values of KO")
abline(v = (.75 * nrow(KO.df)), col='red')