Monte Carlo simulation with R

General/technical analysis of GAFA’s future stock-price using Monte Carlo simulation

remove (list = objects() )

library(quantmod)
library(xts)
library(rvest)
library(tidyverse)
library(stringr)
library(forcats)
library(lubridate)
library(plotly)
library(dplyr)
library(PerformanceAnalytics)

Loads the company stock using ticke

invisible(getSymbols(“AMZN”,from=”2008-08-01″,to=”2018-08-20″))
invisible(getSymbols(“FB”,from=”2008-08-01″,to=”2018-08-20″))
invisible(getSymbols(“TSLA”,from=”2008-08-01″,to=”2018-08-20″))
invisible(getSymbols(“AAPL”,from=”2008-08-01″,to=”2018-08-20″))
invisible(getSymbols(“GOOGL”,from=”2008-08-01″,to=”2018-08-20″))

Stock returns in log

amazon_logs<-AMZN%>%Ad()%>%dailyReturn(type=’log’)
facebook_logs<-FB%>%Ad()%>%dailyReturn(type=’log’)
tesla_logs<-TSLA%>%Ad()%>%dailyReturn(type=’log’)
apple_logs<-AAPL%>%Ad()%>%dailyReturn(type=’log’)
googe_logs<-GOOGL%>%Ad()%>%dailyReturn(type=’log’)

Mean of log stock returns

AMZN_mean_log<-mean(amazon_logs)
FB_mean_log<-mean(facebook_logs)
TSLA_mean_log<-mean(tesla_logs)
AAPL_mean_log<-mean(apple_logs)
GOOGL_mean_log<-mean(googe_logs)

round it to 4 decimal places

mean_log<-c(AMZN_mean_log,FB_mean_log,TSLA_mean_log,AAPL_mean_log,GOOGL_mean_log)
mean_log<-round(mean_log,4)

standard deviation of log stock returns

AMZN_sd_log<-sd(amazon_logs)
FB_sd_log<-sd(facebook_logs)
TSLA_sd_log<-sd(tesla_logs)
AAPL_sd_log<-sd(apple_logs)
GOOGL_sd_Log<-sd(googe_logs)

round it to 4 decimal places

sd_log<-c(AMZN_sd_log,FB_sd_log,TSLA_sd_log,AAPL_sd_log,GOOGL_sd_Log)
sd_log<-round(sd_log,4)

create data frame

graphic1<-data.frame(rbind(c(“AMZN”,AMZN_mean_log,AMZN_sd_log),c(“FB”,FB_mean_log,FB_sd_log),c(“TSLA”,TSLA_mean_log,TSLA_sd_log),c(“AAPL”,AAPL_mean_log,AAPL_sd_log),c(“GOOGL”,GOOGL_mean_log,GOOGL_sd_Log)),stringsAsFactors = FALSE)
graphic1<-data.frame(mean_log,sd_log)
rownames(graphic1)<-c(“AMZN”,”FB”,”TSLA”,”AAPL”,”GOOGL”)
colnames(graphic1)<-c(“Mean_Log_Return”, “Sd_Log_Return”)

Technical Analysis

AMZN%>%Ad()%>%chartSeries()
AMZN%>%chartSeries(TA=’addBBands();addVo();addMACD()’,subset=’2018′)

Comparisons between portfolio companies

library(PerformanceAnalytics)
data<-cbind(diff(log(Cl(AMZN))),
diff(log(Cl(FB))),
diff(log(Cl(TSLA))),
diff(log(Cl(AAPL))),
diff(log(Cl(GOOGL))))
chart.Correlation(data)

Risk v. Return

xlab<-list(title=”Reward”)
ylab<-list(title=”Risk”)
plot(mean_log, sd_log,
main= “Risk v. Reward (in%)”,
xlab= “Reward”,
ylab= “Risk”,
col= “blue”, pch = 19, cex = 1, lty = “solid”, lwd = 3)
text(mean_log, sd_log, labels=rownames(graphic1), cex= 0.7, pos=4)

Price Prediction (for 4 years(1,000 business-days) future)

mu<-AMZN_mean_log
sig<-AMZN_sd_log
testsim<-rep(NA,1000)

generate random daily exponent increase rate using AMZN’s mean and sd log returns

one year 252 trading days, simulate for 4 years

4*252 trading days

price<-rep(NA,252*4)

most recent price

price[1]<-as.numeric(AMZN$AMZN.Adjusted[length(AMZN$AMZN.Adjusted),])

start simulating prices

for(i in 2:length(testsim)){
price[i]<-price[i-1]*exp(rnorm(1,mu,sig))
}

data_frame<-cbind(price,1:(252*4))
colnames(data_frame)<-c(“Price”,”Day”)
data_frame<-as.data.frame(data_frame)

data_frame%>%ggplot(aes(Day,Price))+geom_line()+labs(title=”Amazon (AMZN) price for next 4 years(1,000 buiness-days)”)+theme_bw()

500 Monte Carlo Simulation

N<-500 mc_matrix<-matrix(nrow=252*4,ncol=N) mc_matrix[1,1]<-as.numeric(AMZN$AMZN.Adjusted[length(AMZN$AMZN.Adjusted),]) for(j in 1:ncol(mc_matrix)){ mc_matrix[1,j]<-as.numeric(AMZN$AMZN.Adjusted[length(AMZN$AMZN.Adjusted),]) for(i in 2:nrow(mc_matrix)){ mc_matrix[i,j]<-mc_matrix[i-1,j]*exp(rnorm(1,mu,sig)) } } name<-str_c(“Sim “,seq(1,500)) #500 or N? name<-c(“Day”,name) final_mat<-cbind(1:(252*4),mc_matrix) final_mat<-as.tibble(final_mat) colnames(final_mat)<-name dim(final_mat) #1008 501 final_mat%>%gather(“Simulation”,”Price”,2:501)%>%ggplot(aes(x=Day,y=Price,Group=Simulation))+geom_line(alpha=0.2)+labs(title=”Amazon Stock (AMZN): 500 Monte Carlo Simulations for nest 4 Years”)+theme_bw()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: