-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolioMain.R
82 lines (57 loc) · 1.9 KB
/
portfolioMain.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
# Set up
#Cleared the work space
rm(list=ls())
#sets the variable "path" to the name of Working Directory
path <- "/Users/Desktop/Projects/Portfolio Optimization"
#Set the Working Directory using the variable "path"
setwd(path)
# Libraries and Source Code
# https://cran.r-project.org/package=timetk
library(timetk)
# https://cran.r-project.org/package=plotly
library(plotly)
# https://cran.r-project.org/package=tibble
library(tibble)
# https://cran.r-project.org/package=e1071
library(e1071)
#Import portfolioObject.R
source('portfolioObject.R')
# Data
#AAPL data frame from https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch
AAPL.df <- read.csv("AAPL.csv")
#MSFT data frame from https://finance.yahoo.com/quote/MSFT?p=MSFT&.tsrc=fin-srch
MSFT.df <- read.csv("MSFT.csv")
#GOOGL data frame from https://finance.yahoo.com/quote/GOOGL?p=GOOGL&.tsrc=fin-srch
GOOGL.df <- read.csv("GOOGL.csv")
#AMZN data frame from https://finance.yahoo.com/quote/AMZN?p=AMZN&.tsrc=fin-srch
AMZN.df <- read.csv("AMZN.csv")
# Create a data frame for risk-free with 1 column and 200 rows with 0%
riskf <- data.frame(Adj.Close = rep(0, 200))
# Portfolio Object
# names of the stocks
names <- c("AAPL","MSFT","GOOGL","AMZN")
# instance of the portfolio object
port.obj <- portfolio(AAPL.df,MSFT.df,GOOGL.df,AMZN.df,names.list= names, rf= riskf,num.ports = 6000)
# Functions
# portfolio statistics
port_stats <- port.obj$get.Stats()
# portfolio covariance
port_cov <- port.obj$get.Cov()
# portfolio correlation
port_cor <- port.obj$get.Cor()
# portfolio mvp
port_mvp <- port.obj$get.MVP()
# portfoilio mvep
port_mvep <- port.obj$get.MVEP()
# Effecient frontier plot
port.obj$plot.EF()
# MVP bar chart
port.obj$plot.MVP()
# MVEP bar chart
port.obj$plot.MVEP()
# Gets the dates of data used in the portfolio
port.obj$date
# Set your weights
weights <- c(.4,.3,.2,.1)
# weighted portfolio returns
port.obj$plot.returns(weights)