Last active
February 27, 2017 19:17
-
-
Save lockefox/f63ffc5bb84d23f2f62558a891523791 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### Libraries #### | |
library(quantmod) #getSymbols/chartSeries | |
library(zoo) #rollmean/rollapply | |
library(plyr) | |
library(TTR) | |
library(prophet) | |
library(dplyr) | |
library(ggplot2) | |
library(ggthemes) | |
library(cowplot) | |
library(zoo) | |
#### Globals #### | |
chart_path = paste0(getwd(),"/Plots/") | |
dir.create(chart_path, showWarnings=FALSE) | |
plot.width = 1600 | |
plot.height = 900 | |
plot.days = 200 | |
plot.predict = 60 | |
plot.sma.slow = 15 | |
plot.sma.fast = 5 | |
subset.range = 700 | |
step_back = 7 | |
#### Fetch Ticker Data #### | |
StockSymbol = 'AMD' | |
StockDF <- as.data.frame(getSymbols(Symbols=StockSymbol, src="google", env=NULL, return.class="data.frame", auto.assign=FALSE)) | |
StockDF$Date <- as.Date(row.names(StockDF)) | |
names(StockDF) <- c("Open","High","Low","Close","Volume","Date") | |
StockDF <- subset(StockDF, Date > Sys.Date() - subset.range) | |
#### Manipulate Data #### | |
StockDF$move <- StockDF$Close - StockDF$Open | |
prophet.move <- prophet(data.frame( | |
ds=StockDF$Date, | |
y=StockDF$move | |
)) | |
prophet.move.predict <- predict( | |
prophet.move, | |
make_future_dataframe(prophet.move, periods=plot.predict) | |
) | |
plot.data.tmp <- data.frame( | |
ds=StockDF$Date, | |
y=StockDF$Close | |
) | |
stock.prophet <- prophet(plot.data.tmp) | |
stock.predict <- predict( | |
stock.prophet, | |
make_future_dataframe(stock.prophet, periods=plot.predict) | |
) | |
plot.data <- merge( | |
plot.data.tmp, | |
stock.predict, | |
by='ds', | |
all=TRUE | |
) | |
plot.data$monte <- NA | |
monte.date <- max(StockDF$Date, na.rm=TRUE) | |
monte.date <- StockDF$Date[which(StockDF$Date == monte.date) - step_back] #walk back N rows | |
monte.price <- StockDF$Close[StockDF$Date == monte.date] | |
for(val in 1:(plot.predict+step_back)){ | |
step_date <- monte.date + val | |
if(!step_date %in% prophet.move.predict$ds){ | |
next | |
} | |
monte.price <- monte.price + prophet.move.predict$yhat[prophet.move.predict$ds == step_date] | |
plot.data$monte[plot.data$ds == step_date] <- monte.price | |
} | |
#### Build Plot #### | |
plot.range = Sys.Date() - plot.days | |
plot.data <- subset(plot.data, ds > plot.range) | |
plot <- ggplot( | |
plot.data, | |
aes( | |
x=ds, | |
ymin=yhat_lower, | |
ymax=yhat_upper | |
) | |
) + geom_ribbon( | |
color='light blue', | |
fill='light blue', | |
alpha=0.5 | |
) + geom_line( | |
aes( | |
x=ds, | |
y=y | |
), | |
data=plot.data | |
) + geom_line( | |
aes( | |
x=ds, | |
y=monte | |
), | |
data=plot.data, | |
color='red' | |
) + labs( | |
x='Date', | |
y='Closing Price', | |
title=paste0(StockSymbol, ' + Prediction ', plot.predict, ' -- ', Sys.Date()) | |
) + theme_fivethirtyeight() | |
print(plot) | |
#### Save Plot #### | |
image_title <- paste( | |
StockSymbol, | |
paste0("prediction-", plot.predict), | |
Sys.Date(), | |
sep="_" | |
) | |
png(paste0(chart_path,image_title,".png"), width=plot.width, height=plot.height) | |
print(plot) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment