-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTime Series Stock Prices-Monthly.R
95 lines (77 loc) · 3.17 KB
/
Time Series Stock Prices-Monthly.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
#Time Series
#Forecasting the Stock Price with Avg Monthly Stock Prices:
mydata <- read.csv("F:/rWork/rProjects/AbhiAnalytics/StockPriceMonthly.csv", header = T)
attach(mydata)
View(mydata)
#Load Libraries
library(MASS)
library(tseries)
library(forecast)
#Plot & Convert to ln format
lnstock=log(price[1:30])
lnstock
#the reason we are taking log of stock prices is because:stock prices are based on returns
#and returns are based on percentages, so we are going to cover the price in log format
#to make sure that we are picking up that attribute in our time series.
#Dickey-Fuller Test
adf.test(lnstock)
#this test shows that this time series is not stationary.
#ACF, PACF
acf(lnstock, lag.max = 20)
pacf(lnstock, lag.max = 20)
difflnstock <- diff(lnstock)
difflnstock
adf.test(difflnstock)
#the time series has not become stationary even now, hence, we differenciate it second time.
diff2lnstock <- diff(diff(lnstock))
diff2lnstock
adf.test(diff2lnstock)
#this test shows that this time series is stationary now.
#Time Series & auto.arima
pricearima <- ts(lnstock,start = c(2015,1),end = c(2017,6),frequency = 12)
class(pricearima)
fitlnstock <- auto.arima(pricearima)
fitlnstock
plot(pricearima)
title ('Stock Price')
exp(lnstock)
#Forecasted values from ARIMA
forecastedvalues_ln = forecast(fitlnstock,h=6)
forecastedvalues_ln
plot(forecastedvalues_ln)
forecastedvaluesextracted = as.numeric(forecastedvalues_ln$mean)
final_forecastedvalues = exp(forecastedvaluesextracted)
final_forecastedvalues
#Percentage Error:
df <-data.frame(price[31:36],final_forecastedvalues)
col_headings <-c("Actual Price","Forecasted Price")
names(df) <- col_headings
View(df)
attach(df)
percentage_error = ((df$'Actual Price'- df$'Forecasted Price')/(df$'Actual Price'))
percentage_error
mean(percentage_error)
#Ljung-Box test:
# To see if our residuals are random or not,
# bcz if there are correlations between our residuals then its going to create problems in our time series model bcz its not good to have correlations between the residuals.
#and it could skew the accuracy of the estimates/firecasts we are getting.
Box.test(fitlnstock$resid, lag = 5,type = "Ljung-Box")
Box.test(fitlnstock$resid, lag = 10,type = "Ljung-Box")
#Now, since the p value is in-significant, hence, we cant reject the Null hypothesis
#that the residuals are random.
-------------------------------------
#Fetching Company's financial data from online source & analyzing it using quantmode package----
install.packages("quantmod")
library(quantmod)
start <- as.Date("2019-10-01") ;end <- as.Date("2019-12-20")
getSymbols("SBIN.NS", src = "yahoo", from = start, to = end)
SBIN.NS
head(SBIN.NS)
plot(SBIN.NS[, "SBIN.NS.Close"], main = "SBIN.NS")
candleChart(SBIN.NS, up.col = "black", dn.col = "red", theme = "white")
#multiple stocks - prices from
getSymbols(c("ICICIBANK.NS", "TATAMOTORS.NS"), src = "yahoo", from = start, to = end)
stocks = as.xts(data.frame(SBIN = SBIN.NS[, "SBIN.NS.Close"], ICICI = ICICIBANK.NS[, "ICICIBANK.NS.Close"], TATAMOTORS = TATAMOTORS.NS[, "TATAMOTORS.NS.Close"]))
head(stocks)
plot(as.zoo(stocks), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price")
legend("right", c("SBIN", "ICICI", "TATATMOTORS"), lty = 1:3, cex = 0.5)