Stock Price Predictions

Predicting Stock Prices using linear Regression.

Peter Sullivan
2022-02-12
Stock = "AAPL"

start_date <- Sys.Date()
start_date
[1] "2022-02-12"
retrieval_date <- start_date - years(4)

Stock_data <- tq_get(Stock, get = "stock.prices", from = retrieval_date, to = start_date)

Stock_data %>% datatable()

Getting Signals

Stock_data <- Stock_data %>%
  group_by(symbol) %>%
  tq_mutate(select = close,
            mutate_fun = MACD,
            col_rename = c("MACD", "Signal"))%>%
  tq_mutate(select = adjusted, mutate_fun = RSI) %>%
  tq_mutate(select = adjusted, mutate_fun = BBands, col_rename = "Bbands") %>%
  tq_mutate_xy(x = close, y =volume, mutate_fun = EVWMA, col_rename = "EVWMA") %>%
  mutate(Stock_movement = case_when(
    lag(close)- close > 0 ~ "Decrease",
    close > lag(close) ~ "Increase",
    close == lag(close) ~"No Change"
  ) 
           ) %>%
  select(symbol:adjusted,Stock_movement,MACD,rsi,EVWMA)





#str(Stock_data)
Stock_data <- Stock_data %>% mutate(forecast1day = lead(close,1),
                                    forecast1week = lead(close,7),
                                    forecast2week = lead(close,14),
                                    forecast4week = lead(close,31),
                                    forecast2month = lead(close,60),
                                    forecast4month = lead(close,120))

Stock_data %>% head(10) %>% select(close, forecast1day,forecast1week)
# A tibble: 10 x 4
# Groups:   symbol [1]
   symbol close forecast1day forecast1week
   <chr>  <dbl>        <dbl>         <dbl>
 1 AAPL    40.7         41.1          43.1
 2 AAPL    41.1         41.8          43.9
 3 AAPL    41.8         43.2          44.7
 4 AAPL    43.2         43.1          44.6
 5 AAPL    43.1         43.0          44.5
 6 AAPL    43.0         42.8          43.8
 7 AAPL    42.8         43.1          44.1
 8 AAPL    43.1         43.9          44.2
 9 AAPL    43.9         44.7          44.2
10 AAPL    44.7         44.6          43.8
Stock_data <- Stock_data[complete.cases(Stock_data),]
#Stock_data %>% colnames()



current <- lm(close ~ volume + MACD+ rsi+ EVWMA , data = Stock_data)
forecast1daymodel <- lm(forecast1day ~ volume + MACD+ rsi+ EVWMA , data = Stock_data)
forecast1weekmodel <- lm(forecast1week ~ volume + MACD+ rsi+ EVWMA , data = Stock_data)
forecast2weekmodel <- lm(forecast2week ~ volume + MACD+ rsi+ EVWMA , data = Stock_data)
forecastmonthmodel <-lm(forecast4week~ volume + MACD+ rsi+ EVWMA , data = Stock_data)
forecast2monthmodel <- lm(forecast2month~ volume + MACD+ rsi+ EVWMA , data = Stock_data)
forecast4monthmodel <- lm(forecast4month~ volume + MACD+ rsi+ EVWMA , data = Stock_data)
#summary(current)
#summary(forecast1weekmodel)
#summary(forecast2weekmodel)
#summary(forecast1weekmodel)
#summary(forecastmonthmodel)
#summary(forecast2monthmodel)
#summary(forecast4monthmodel)

Stock_data$currentPrediction <- predict(current, Stock_data)
Stock_data$day1prediction <- predict(forecast1daymodel, Stock_data)
Stock_data$day7prediction <- predict(forecast1weekmodel, Stock_data)
Stock_data$day14prediction <- predict(forecast2weekmodel, Stock_data)
Stock_data$day31prediction <- predict(forecastmonthmodel, Stock_data)

correlations <- c(cor(Stock_data$currentPrediction,Stock_data$close),cor(Stock_data$day1prediction, Stock_data$forecast1day),cor(Stock_data$day7prediction, Stock_data$forecast1week),cor(Stock_data$day14prediction, Stock_data$forecast2week),cor(Stock_data$day31prediction, Stock_data$forecast4week))

Predictions_cor <- c("Current", "1dayModel","1weekModel","2weekModel","1monthModel")

cbind(Predictions_cor,round(correlations,3)) %>% knitr::kable()
Predictions_cor
Current 0.999
1dayModel 0.998
1weekModel 0.992
2weekModel 0.985
1monthModel 0.968
Stock_data %>% ggplot()+
  geom_point(aes(x = date, Stock_data$day7prediction), color ="red")+
  geom_point(aes(x = date, Stock_data$forecast2week), color ="blue")
Stock_data %>% ggplot()+
  geom_point(aes(x = date, Stock_data$day31prediction), color ="red")+
  geom_point(aes(x = date, Stock_data$forecast4week), color ="blue")
Stock_data %>% ggplot()+
  geom_point(aes(x = date, Stock_data$day7prediction), color ="red")+
  geom_point(aes(x = date, Stock_data$forecast1week), color ="blue")

start_date <- Sys.Date()
start_date
[1] "2022-02-12"
retrieval_date <- start_date - months(6)

new_stock_data <- tq_get(Stock, get = "stock.prices", from = retrieval_date, to = start_date)


new_stock_data <- new_stock_data %>%
  group_by(symbol) %>%
  tq_mutate(select = close,
            mutate_fun = MACD,
            col_rename = c("MACD", "Signal"))%>%
  tq_mutate(select = adjusted, mutate_fun = RSI) %>%
  tq_mutate(select = adjusted, mutate_fun = BBands, col_rename = "Bbands") %>%
  tq_mutate_xy(x = close, y =volume, mutate_fun = EVWMA, col_rename = "EVWMA") %>%
  mutate(Stock_movement = case_when(
    lag(close)- close > 0 ~ "Decrease",
    close > lag(close) ~ "Increase",
    close == lag(close) ~"No Change"
  ) 
           ) %>%
  select(symbol:adjusted,Stock_movement,MACD,rsi,EVWMA)

new_stock_data$monthforecast <- predict(forecastmonthmodel, new_stock_data)
new_stock_data$week2forecast <- predict(forecast2weekmodel, new_stock_data)
new_stock_data$month2forecast <- predict(forecast2monthmodel, new_stock_data)
new_stock_data$month4forecast <- predict(forecast4monthmodel, new_stock_data)

new_stock_data$monthforecastdate <- new_stock_data$date + days(31)
new_stock_data$week2forecastdate <- new_stock_data$date +days(14)
new_stock_data$month2forecastdate <- new_stock_data$date+ days(60)
new_stock_data$month4forecastdate <- new_stock_data$date +days(120)




new_stock_data%>% ggplot()+
  geom_line(aes(x = monthforecastdate, y = monthforecast),color = "red")+
  geom_line(aes(x = week2forecastdate, y = week2forecast),color = "green")+
  geom_line(aes(x = date, y = close), color = "blue")+
  geom_line(aes(x = month2forecastdate, y = month2forecast) ,color = "orange")+
  geom_line(aes(x = month4forecastdate, y = month4forecast), color = "gray")
#new_stock_data %>% select(monthforecastdate,monthforecast) %>% filter(monthforecastdate > "2022-01-01")

new_stock_data %>% ggplot()+geom_line(aes(x = month4forecastdate, y = month4forecast), color = 'red')+
  geom_line(aes(x = date, y = close), color = 'blue')

x<-new_stock_data %>%
  ggplot()+geom_line(data = new_stock_data   %>% filter(month2forecastdate > Sys.Date()),aes(x = month2forecastdate, y = month2forecast), color = 'red')+
  geom_line(aes(x = date, y = close), color = 'blue')+
  geom_line(data = new_stock_data %>% filter(month4forecastdate > Sys.Date()),aes(x = month4forecastdate,y = month4forecast), color = 'green')+
  geom_line(data = new_stock_data %>% filter(monthforecastdate >Sys.Date()), aes(x = monthforecastdate,y = monthforecast), color = 'orange')+
  labs(title = "Price Predictions")+
  xlab("Date")+
  ylab("Price")

print(x)