tq_get()
: Getting Financial Data from the web. tq_get(“company symbol”, get = “information”)tq_transmute()
and tq_mutate()
: Manipulating Financial Datatq_performance
and tq_portfolio
Performance Analysis and Portfolio Analysis
Initialize work ranking 500 companies and Nasdaq Composites for the future implementation. Then using glimpse()
function for geting main variables.
sp500 <- tq_index("sp500")
## Getting holdings for SP500
nyse <- tq_exchange("nyse")
## Getting data...
nasdaq <- tq_exchange("NASDAQ")
## Getting data...
glimpse(sp500)
## Observations: 506
## Variables: 8
## $ symbol <chr> "MSFT", "AAPL", "AMZN", "FB", "BRK.B", "JPM", "GO…
## $ company <chr> "Microsoft Corporation", "Apple Inc.", "Amazon.co…
## $ identifier <chr> "59491810", "03783310", "02313510", "30303M10", "…
## $ sedol <chr> "2588173", "2046251", "2000019", "B7TL820", "2073…
## $ weight <dbl> 0.044357910, 0.042883576, 0.028614025, 0.01861036…
## $ sector <chr> "Information Technology", "Information Technology…
## $ shares_held <dbl> 85326824, 47472344, 4643216, 26884278, 21912680, …
## $ local_currency <chr> "USD", "USD", "USD", "USD", "USD", "USD", "USD", …
glimpse(nyse)
## Observations: 3,130
## Variables: 7
## $ symbol <chr> "DDD", "MMM", "WBAI", "WUBA", "EGHT", "AHC", "AO…
## $ company <chr> "3D Systems Corporation", "3M Company", "500.com…
## $ last.sale.price <dbl> 8.330, 165.170, 9.310, 61.370, 18.980, 2.865, 46…
## $ market.cap <chr> "$986.45M", "$94.98B", "$400.1M", "$9.12B", "$1.…
## $ ipo.year <dbl> NA, NA, 2013, 2013, NA, NA, NA, 2014, NA, NA, NA…
## $ sector <chr> "Technology", "Health Care", "Consumer Services"…
## $ industry <chr> "Computer Software: Prepackaged Software", "Medi…
glimpse(nasdaq)
## Observations: 3,533
## Variables: 7
## $ symbol <chr> "TXG", "YI", "PIH", "PIHPP", "TURN", "FLWS", "BC…
## $ company <chr> "10x Genomics, Inc.", "111, Inc.", "1347 Propert…
## $ last.sale.price <dbl> 64.9600, 5.0100, 4.7500, 25.3000, 2.1483, 12.710…
## $ market.cap <chr> "$6.24B", "$409.11M", "$28.59M", "$17.71M", "$66…
## $ ipo.year <dbl> 2019, 2018, 2014, NA, NA, 1999, 2019, NA, NA, 20…
## $ sector <chr> "Capital Goods", "Health Care", "Finance", "Fina…
## $ industry <chr> "Biotechnology: Laboratory Analytical Instrument…
Analyzing each sector has how many corresponding companies for sp500. The main sectors are: Industries, Information Technology, Fianacials, Consumer Discretionary, and Health Care.
sector_fields <- sp500 %>%
group_by(sector) %>%
summarise(
Quantity = n()
) %>%
arrange(desc(Quantity))
sector_fields
## # A tibble: 11 x 2
## sector Quantity
## <chr> <int>
## 1 Industrials 69
## 2 Information Technology 69
## 3 Financials 67
## 4 Consumer Discretionary 64
## 5 Health Care 61
## 6 Consumer Staples 33
## 7 Real Estate 32
## 8 Energy 28
## 9 Materials 28
## 10 Utilities 28
## 11 Communication Services 27
Selecting stock and using inner_joint and keeping companies’ symbols for the SP500 list that are also traded on NYSE and NASDAQ. We only keep the important variables like symbol, company, market.cap …. Then we arrange the data with a descending order of weight, in order to figure out which company has high market weight. We want to find out the stock prices and monthly return corresponding with though companies.
stocks.selection <- sp500 %>%
inner_join(rbind(nyse,nasdaq) %>% select(symbol, last.sale.price,market.cap, ipo.year), by = c("symbol")) %>%
filter(ipo.year<2000 & !is.na(market.cap)) %>%
arrange(desc(weight)) %>%
slice(1:10)
stocks.selection <- stocks.selection %>%
select(symbol, company, weight, sector,shares_held,last.sale.price,market.cap,ipo.year)
stocks.selection
## # A tibble: 10 x 8
## symbol company weight sector shares_held last.sale.price market.cap
## <chr> <chr> <dbl> <chr> <dbl> <dbl> <chr>
## 1 MSFT Micros… 0.0444 Infor… 85326824 149. $1139.06B
## 2 AAPL Apple … 0.0429 Infor… 47472344 259. $1152.81B
## 3 AMZN Amazon… 0.0286 Consu… 4643216 1770. $877.54B
## 4 CSCO Cisco … 0.00731 Infor… 47442100 44.3 $187.92B
## 5 ADBE Adobe … 0.00575 Infor… 5432115 304. $147.12B
## 6 AMGN Amgen … 0.00544 Healt… 6712285 233. $138.31B
## 7 NVDA NVIDIA… 0.00491 Infor… 6796950 208. $126.46B
## 8 ORCL Oracle… 0.00468 Infor… 24642536 54.6 $179.19B
## 9 SBUX Starbu… 0.00396 Consu… 13397522 84.8 $100.48B
## 10 QCOM QUALCO… 0.00358 Infor… 12768429 80.6 $92.01B
## # … with 1 more variable: ipo.year <dbl>
Selecting Five biggest information technology company from sp500, and also traded on NYSE or NASDAQ. We find Google, Amazon, Facebook, Apple, and Microsoft are top 5 IT companies.
Information_technology_company <- sp500 %>%
inner_join(rbind(nyse,nasdaq) %>%
select(symbol, last.sale.price,market.cap, ipo.year),by = c("symbol")) %>%
filter(!is.na(market.cap) & ( sector == "Information Technology" | sector == "Communication Services" | sector == "Consumer Discretionary" ) & !is.na(ipo.year)) %>%
arrange(desc(weight)) %>%
head(5)
Information_technology_company <- Information_technology_company %>%
select(symbol, company, weight,sector,shares_held, last.sale.price, market.cap, ipo.year)
Information_technology_company
## # A tibble: 5 x 8
## symbol company weight sector shares_held last.sale.price market.cap
## <chr> <chr> <dbl> <chr> <dbl> <dbl> <chr>
## 1 MSFT Micros… 0.0444 Infor… 85326824 149. $1139.06B
## 2 AAPL Apple … 0.0429 Infor… 47472344 259. $1152.81B
## 3 AMZN Amazon… 0.0286 Consu… 4643216 1770. $877.54B
## 4 FB Facebo… 0.0186 Commu… 26884278 199. $566.98B
## 5 GOOG Alphab… 0.0152 Commu… 3376825 1295. $893.34B
## # … with 1 more variable: ipo.year <dbl>
Monthly return is the period returns re-scaled to a period of 1 month. This allows investors to compare returns of different assets that they have owned for different lengths of time.
As can be see from the result, the monthly shows an unstable fluctuations between -0.1 to +0.2.
Google <- tq_get("GOOG", get = "stock.prices")
Google_monthly_return <- Google %>%
tq_transmute(
select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "monthly_return",
)
Google_monthly_return %>% head()
## # A tibble: 6 x 2
## date monthly_return
## <date> <dbl>
## 1 2009-01-30 0.0536
## 2 2009-02-27 -0.00160
## 3 2009-03-31 0.0298
## 4 2009-04-30 0.138
## 5 2009-05-29 0.0537
## 6 2009-06-30 0.0104
Google_monthly_return %>% ggplot(aes(x = date, y = monthly_return)) + geom_line() + theme_tq() + scale_color_tq()
Then we want to analyze the stock price for google in the past 10-15 years.
Although the montly return shows a dramatically fluctuation trend, however, the stock.price incrases dramatically with some minimum fluctuations from the past 15 years. (from less then 150 to more then 1750). However, as can be seen from the line graph. The graph indicates there are some big drops of the stock price between 2015 and 2019. In order to figure out the reason for this result, we want to get more companies information.
tq_get(c("GOOG"), get="stock.prices") %>%
ggplot(aes(date, close)) +
geom_line() + theme_tq() + scale_color_tq()
In order to dig deep for this problem, we want to compare Google’s stock prices with more big name IT companies like Amazon, Facebook, Apple, Microsoft.
The graph indicates Facebook experiences a period with dramatically influctuation from 2011-2013.
MAAFG <- c("MSFT","AAPL", "AMZN","FB","GOOG")
tq_get(MAAFG, get = "stock.prices") %>%
head()
## # A tibble: 6 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MSFT 2009-01-02 19.5 20.4 19.4 20.3 50084000 15.6
## 2 MSFT 2009-01-05 20.2 20.7 20.1 20.5 61475200 15.8
## 3 MSFT 2009-01-06 20.8 21 20.6 20.8 58083400 16.0
## 4 MSFT 2009-01-07 20.2 20.3 19.5 19.5 72709900 15.0
## 5 MSFT 2009-01-08 19.6 20.2 19.5 20.1 70255400 15.5
## 6 MSFT 2009-01-09 20.2 20.3 19.4 19.5 49815300 15.0
MAAFG_monthly_returns <- tq_get(MAAFG, get = "stock.prices") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "monthly_return")
head(MAAFG_monthly_returns)
## # A tibble: 6 x 3
## # Groups: symbol [1]
## symbol date monthly_return
## <chr> <date> <dbl>
## 1 MSFT 2009-01-30 -0.159
## 2 MSFT 2009-02-27 -0.0491
## 3 MSFT 2009-03-31 0.137
## 4 MSFT 2009-04-30 0.103
## 5 MSFT 2009-05-29 0.0376
## 6 MSFT 2009-06-30 0.138
MAAFG_monthly_returns_graph <- ggplot(data =MAAFG_monthly_returns, aes(date, monthly_return, color = symbol)) + geom_line() + theme_tq() + scale_color_tq()
MAAFG_monthly_returns_graph
MAAFG_monthly_returns_graph + facet_wrap(~symbol, scales = "free_y")
MAAFG_monthly_returns %>%
mutate(year = year(date)) %>%
group_by(symbol, year) %>%
summarise(mean = mean(monthly_return),
sd = sd(monthly_return)) %>%
ggplot(aes(x = year, y = mean, fill = symbol)) +
geom_bar(stat = "identity", position = "dodge", width = 0.7) +
scale_y_continuous(breaks = seq(-0.1,0.5,0.02),
labels = scales::percent) +
scale_x_continuous(breaks = seq(2005, 2018,1)) +
labs(x = "Year", y = "Mean Returns") +
theme_bw() + theme(legend.position = "top") +
scale_fill_brewer(palette = "Set1",
name = "Stocks") +
ggtitle("Monthly Mean Returns for MAFFG stocks")
Then we make a graph of stock’s prices graph for Facebook, Google, Amazon, Apple, and Microsoft. All these five companies show an increasing trend for the stock’s price. However, we realize that the increasing rate for Google and Amazon are much quicker than the other companies. We also realize both Google and Amazon experience a fluctuational periods for stock’s price between 2015 - 2019.
The reason caused Google and Amazon win on price stock since 2015: create their own home service Peter Faricy, vice president of Amazon Marketplace, said the company has 2.4 million serve offers covering more 700 types of services. “I can tell you that with 85 million customers purchasing products from Amazon that needed installation or assembly, customers have told us that Amazon Home Services fills an important need,” he said.
For Google, where a spokesman said the effort was still in early stages, it is a chance to move beyond the advertising dollar to become part of the transaction itself, and to challenge Amazon more directly, as it is already trying with shopping and delivery services like Google Express.
“Amazon is always focused on having the widest selection on earth,” Mr. Faricy said, “and we will do the same with services.”
the cloud dataset The major players are Amazon and Google, who now offer their own infrastructure to the rest of the world as cloud computing services.
This spring, Amazon revealed that its cloud operation is now a $4.6 billion business, and the company expects it to grow to $6.23 billion by the end of the year. Google may not grow as quickly as Amazon’s. But it will grow. It’s where the world is moving.
GAFA_data <- tq_get(c("MSFT","AAPL", "AMZN","FB","GOOG"), get = "stock.prices")
GAFA_data %>% head()
## # A tibble: 6 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MSFT 2009-01-02 19.5 20.4 19.4 20.3 50084000 15.6
## 2 MSFT 2009-01-05 20.2 20.7 20.1 20.5 61475200 15.8
## 3 MSFT 2009-01-06 20.8 21 20.6 20.8 58083400 16.0
## 4 MSFT 2009-01-07 20.2 20.3 19.5 19.5 72709900 15.0
## 5 MSFT 2009-01-08 19.6 20.2 19.5 20.1 70255400 15.5
## 6 MSFT 2009-01-09 20.2 20.3 19.4 19.5 49815300 15.0
tq_get(c("MSFT","AAPL", "AMZN","FB","GOOG"), get="stock.prices") %>%
ggplot(aes(date, close, color=symbol)) +
geom_line() + theme_tq() + scale_color_tq()
tq_get(c("MSFT","AAPL", "AMZN","FB","GOOG"), get="stock.prices") %>%
ggplot(aes(date, close, color=symbol)) +
geom_line() + theme_tq() + scale_color_tq() + facet_wrap(~symbol, scales = "free_y")
Using data wrangling to select the top 5 traditional industrials companies. Then analyze the monthly return and stock’s prices for those company. The top 5 industrials companies are United Parcel Service Inc., Roper Technologies Inc., IHS Markit Ltd., Cintas Corporation, and Fastenal Company.
Industrials <- sp500 %>%
inner_join(rbind(nasdaq, nyse) %>%
select(symbol, last.sale.price, market.cap, ipo.year), by = c("symbol")) %>%
filter(!is.na(ipo.year) & !is.na(market.cap) & sector == "Industrials" & ipo.year <= 2000) %>%
arrange(desc(weight)) %>%
slice(1:10)
Industrials %>%
select(symbol, company, weight, sector,shares_held,last.sale.price,market.cap,ipo.year) %>%
head()
## # A tibble: 6 x 8
## symbol company weight sector shares_held last.sale.price market.cap
## <chr> <chr> <dbl> <chr> <dbl> <dbl> <chr>
## 1 UPS United… 3.18e-3 Indus… 7816542 117. $100.31B
## 2 ROP Roper … 1.42e-3 Indus… 1161176 352. $36.6B
## 3 INFO IHS Ma… 1.13e-3 Indus… 4489505 72.3 $29B
## 4 INFO IHS Ma… 1.13e-3 Indus… 4489505 72.3 $29B
## 5 CTAS Cintas… 8.26e-4 Indus… 929018 255. $26.43B
## 6 FAST Fasten… 7.75e-4 Indus… 6376596 34.9 $20.02B
## # … with 1 more variable: ipo.year <dbl>
Calculating the monthly return and plotting the corresponding line graph.
tq_get(c("UPS", "ROP", "INFO", "CTAS","FAST"), get = "stock.prices") %>%
head()
## # A tibble: 6 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 UPS 2009-01-02 55.3 56.4 54.4 56.1 4233200 40.2
## 2 UPS 2009-01-05 55.9 56.0 54.7 55.2 3593500 39.5
## 3 UPS 2009-01-06 55.4 56.2 54.3 55.4 4578400 39.7
## 4 UPS 2009-01-07 54.9 55 53.2 53.4 5222100 38.2
## 5 UPS 2009-01-08 53.3 53.7 51.7 52.2 5726800 37.4
## 6 UPS 2009-01-09 52.2 52.7 50.9 50.9 4417500 36.5
tq_get(c("UPS", "ROP", "INFO", "CTAS","FAST"), get = "stock.prices") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "monthly_return") %>%
ggplot(aes(date,monthly_return, color = symbol )) + geom_line() + theme_tq() + scale_color_tq()
After analyzing the data and plotting the line graph for stock’s price for top 5 industries company we find that all companies’ stock prices increase stablly. Then we find out some information and business decision for those companies.
tq_get(c("UPS", "ROP", "INFO", "CTAS","FAST"), get="stock.prices") %>%
ggplot(aes(date, close, color=symbol)) +
geom_line() + theme_tq() + scale_color_tq()
* UPS Nearly 50 percent of 35 million sorted packages per day are processed using UPS’s new more-automated facilities. When UPS complete this phase of its Global Smart Logistics Network enhancement by 2022, 100% of eligible volume in the U.S. will be sorted using these new more highly automated sites. Seven new ‘super hub’ automated sortation facilities will be opened during the period, with 30-35% higher efficiency than comparable less-automated facilities. More than 70 expansion projects will be implemented during the period.
ROP Since the beginning of 2010, Roper Industries have deployed over $4 billion to acquire technology-focused businesses, including medical software, information networks, medical products and SaaS-based trading solutions
INFO Enhancing customer experience leads to major investment in network operations digital transformation with operations support systems (OSS), business support systems (BSS) and network management systems (NMS) accounting for 60 percent The Internet of Things (IoT), video and enterprise IP virtual private networks (IP VPNs) top the list of new digital services that service providers plan to launch over their transformed networks
CTAS Cintas rolled out a broad portfolio of solutions to digitalize HR processes, simplify organizational management and empower employee-partners to perform at their best with a world-class, mobile-enabled HR experience. Haiku — Deloitte’s HR-focused data-migration and data-transformation solution — helped significantly reduce conversion timelines and increase data-quality assurance in the always-critical area of data conversion.
FAST While Fastenal purchased the automated storage systems, the company built its own sortation system and developed its own WMS to serve its unique needs. Fastenal has plans to continue to add automation in the future. For instance, the company has implemented a scaled-down version of the mini-load, with 42,000 tote locations, and a unit load AS/RS.
####Energy, Consumer Staples, Utilities
Then we want to analyze the monthly return and stock’s prices for Energy, Consumer Staples, and Utilities companies. The top 5 companies are Estee Lauder Companies Inc., Kinder Morgan Inc, Concho Resources Inc., Baker Hughes Company , and Diamondback Energy Inc..
ECSU <- sp500 %>%
inner_join(rbind(nasdaq, nyse) %>%
select(symbol, last.sale.price, market.cap, ipo.year), by = c("symbol")) %>%
filter(!is.na(ipo.year) & !is.na(market.cap) & (sector == "Energy"|sector == "Consumer Staples")) %>%
arrange(desc(weight)) %>%
slice(1:10)
ECSU %>%
select(symbol, company, weight, sector,shares_held,last.sale.price,market.cap,ipo.year) %>%
head()
## # A tibble: 6 x 8
## symbol company weight sector shares_held last.sale.price market.cap
## <chr> <chr> <dbl> <chr> <dbl> <dbl> <chr>
## 1 EL Estee … 1.67e-3 Consu… 2476110 194. $69.74B
## 2 KMI Kinder… 1.44e-3 Energy 21667158 19.2 $43.37B
## 3 CXO Concho… 5.53e-4 Energy 2235091 71.1 $14.29B
## 4 BKR Baker … 5.40e-4 Energy 7272103 21.3 $21.9B
## 5 FANG Diamon… 4.89e-4 Energy 1824992 76.9 $12.34B
## 6 LW Lamb W… 4.80e-4 Consu… 1632427 84.4 $12.33B
## # … with 1 more variable: ipo.year <dbl>
Plotting the monthly return line graph for these five companies.
tq_get(c("EL","KMI","CXO","BKR","FANG"), get = "stock.prices") %>%
head()
## # A tibble: 6 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 EL 2009-01-02 15.8 16.4 15.5 16.3 2971400 13.9
## 2 EL 2009-01-05 16.0 16.1 15.3 15.5 4459400 13.2
## 3 EL 2009-01-06 15.7 15.8 15.2 15.6 5353600 13.3
## 4 EL 2009-01-07 15.3 15.5 15.2 15.5 6067400 13.1
## 5 EL 2009-01-08 15.4 15.6 15.1 15.6 6049400 13.2
## 6 EL 2009-01-09 15.6 15.7 15.2 15.3 6694000 13.0
tq_get(c("EL","KMI","CXO","BKR","FANG"), get = "stock.prices") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "monthly_return") %>%
ggplot(aes(date,monthly_return, color = symbol )) + geom_line() + ylim(-1,1) + theme_tq() + scale_color_tq()
## Warning in to_period(xx, period = on.opts[[period]], ...): missing values
## removed from data
As can be seen from the stocks prices for energy are falling, we want to analyze why stocks’ prices fall for this sector. Why does energy stocks are falling: The dollar rallied to its highest level against a basket of foreign currencies since early 2017. Expectations of higher relative interest rates in the U.S. versus abroad make the dollar more attractive for investors, lifting demand for the currency.It matters for oil and other commodities because they are priced in dollars when traded globally. A more valuable dollar means that a barrel of oil is worth fewer dollars, all else equal. The trade war between China and American effects the net export of energy. Many economists expect that an ongoing US-China trade war could push the global economy toward a recession.
Why does Estee Lauder continually increase stock price: * Earning groth While the historical EPS growth rate for Estee Lauder is 14%, investors should actually focus on the projected growth. The company’s EPS is expected to grow 11.9% this year, crushing the industry average, which calls for EPS growth of 8.7%. *Cash flow growth The company’s annualized cash flow growth rate has been 9.3% over the past 3-5 years versus the industry average of 8.2%. Otherwise, Estee Lauder are also working to balance a physical and digital footprint in that region. For example, the company will use data from their online purchases to determine which cities to enter.s
Plotting the stocks’ prices.
tq_get(c("EL","KMI","CXO","BKR","FANG"), get = "stock.prices") %>%
ggplot(aes(date, close, color = symbol)) + geom_line() + theme_tq() + scale_color_tq()
####Telecommunication AT&T Mobility (T) Verizon Wireless (VZ) T-Mobile (TMUS) Sprint Corporation (S)
tq_get(c("T","VZ","TMUS","S"), get = "stock.prices") %>%
head()
## # A tibble: 6 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 T 2009-01-02 28.7 29.5 28.4 29.4 21879800 16.0
## 2 T 2009-01-05 28.8 28.9 28.1 28.4 32414700 15.4
## 3 T 2009-01-06 28.5 28.7 28 28.3 28746100 15.4
## 4 T 2009-01-07 27.6 27.6 27 27.2 30532700 15.0
## 5 T 2009-01-08 26.9 27.4 26.8 27.2 21431200 15.0
## 6 T 2009-01-09 27.2 27.4 26.6 26.7 30941600 14.7
tq_get(c("T","VZ","TMUS","S"), get = "stock.prices") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "monthly_return") %>%
ggplot(aes(date,monthly_return, color = symbol )) + geom_line() + theme_tq() + scale_color_tq()
tq_get(c("T","VZ","TMUS","S"), get = "stock.prices") %>%
ggplot(aes(date,close, color = symbol)) + geom_line() + theme_tq() + scale_color_tq()
Creating Candlestick charts
google_price <- tq_get('GOOG',
from = '2019-05-01',
to = '2019-10-31',
get = 'stock.prices')
google_price %>%
plot_ly( x = ~date,
type = 'candlestick',
open = ~open,
close = ~close,
high = ~high,
low = ~low ) %>%
layout(title = "Google price since May 2019", xaxis = list(rangeslider = list(visible = F)))
Facebook_price <- tq_get('FB',
from = '2019-05-01',
to = '2019-10-31',
get = 'stock.prices')
Facebook_price %>%
plot_ly( x = ~date,
type = 'candlestick',
open = ~open,
close = ~close,
high = ~high,
low = ~low ) %>%
layout(title = "Facebook price since May 2019", xaxis = list(rangeslider = list(visible = F)))
Microsoft_price <- tq_get('MSFT',
from = '2019-05-01',
to = '2019-10-31',
get = 'stock.prices')
Microsoft_price %>%
plot_ly( x = ~date,
type = 'candlestick',
open = ~open,
close = ~close,
high = ~high,
low = ~low ) %>%
layout(title = "Microsoft price since May 2019", xaxis = list(rangeslider = list(visible = F)))
Apple_price <- tq_get('AAPL',
from = '2019-05-01',
to = '2019-10-31',
get = 'stock.prices')
Apple_price %>%
plot_ly( x = ~date,
type = 'candlestick',
open = ~open,
close = ~close,
high = ~high,
low = ~low ) %>%
layout(title = "Microsoft price since May 2019", xaxis = list(rangeslider = list(visible = F)))
Amazon_price <- tq_get('AMZN',
from = '2019-05-01',
to = '2019-10-31',
get = 'stock.prices')
Microsoft_price %>%
plot_ly( x = ~date,
type = 'candlestick',
open = ~open,
close = ~close,
high = ~high,
low = ~low ) %>%
layout(title = "Amazon price since May 2019", xaxis = list(rangeslider = list(visible = F)))
Based on the result, if we want to make a investment from Facebook, Google, Amazon, Microsoft, and Apple, the best option is Microsoft based on the graph, because the graph shows a really stable increasing trend.
Choose the following 5 stocks
tick <- c('AMZN','AAPL','NFLX', 'XOM', 'T')
price_data <- tq_get(tick,
from = '2014-01-01',
to = '2018-05-31',
get = 'stock.prices')
log_ret_tidy <- price_data %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = 'daily',
col_rename = 'ret',
type = 'log')
head(log_ret_tidy)
## # A tibble: 6 x 3
## # Groups: symbol [1]
## symbol date ret
## <chr> <date> <dbl>
## 1 AMZN 2014-01-02 0
## 2 AMZN 2014-01-03 -0.00385
## 3 AMZN 2014-01-06 -0.00711
## 4 AMZN 2014-01-07 0.0111
## 5 AMZN 2014-01-08 0.00973
## 6 AMZN 2014-01-09 -0.00227
log_ret_xts <- log_ret_tidy %>%
spread(symbol, value = ret) %>%
tk_xts()
## Warning in tk_xts_.data.frame(data = data, select = select, date_var =
## date_var, : Non-numeric columns being dropped: date
## Using column `date` for date_var.
head(log_ret_xts)
## AAPL AMZN NFLX T
## 2014-01-02 0.000000000 0.000000000 0.0000000000 0.0000000000
## 2014-01-03 -0.022210445 -0.003851917 0.0007714349 -0.0043013420
## 2014-01-06 0.005438137 -0.007113316 -0.0097694303 0.0045872329
## 2014-01-07 -0.007177365 0.011115982 -0.0574349094 -0.0002858909
## 2014-01-08 0.006313060 0.009725719 0.0043791809 -0.0072750722
## 2014-01-09 -0.012852257 -0.002266707 -0.0116217990 -0.0206557091
## XOM
## 2014-01-02 0.000000000
## 2014-01-03 -0.002408912
## 2014-01-06 0.001506057
## 2014-01-07 0.014048829
## 2014-01-08 -0.003270495
## 2014-01-09 -0.009775584
mean_ret <- colMeans(log_ret_xts)
cov_mat <- cov(log_ret_xts) * 252
#random weights
wts <- runif(n = length(tick))
wts <- wts/sum(wts)
# Calculate the portfolio returns
port_returns <- (sum(wts * mean_ret) + 1)^252 - 1
# Calculate the portfolio risk
port_risk <- sqrt(t(wts) %*% (cov_mat %*% wts))
# Calculate the Sharpe Ratio
sharpe_ratio <- port_returns/port_risk
print(wts)
## [1] 0.24161535 0.30934006 0.06719062 0.02973902 0.35211495
print(port_returns)
## [1] 0.1939528
print(port_risk)
## [,1]
## [1,] 0.1702965
print(sharpe_ratio)
## [,1]
## [1,] 1.138912
We have everything we need to perform our optimization. All we need is to runn this code on 5000 random portfolios. For that we will use a for loop. Before do that, we need to create empty vectors and matrix for storing our values
num_port <- 5000
# Creating a matrix to store the weights
all_wts <- matrix(nrow = num_port,
ncol = length(tick))
port_returns <- vector('numeric', length = num_port)
port_risk <- vector('numeric', length = num_port)
sharpe_ratio <- vector('numeric', length = num_port)
for (i in seq_along(port_returns)) {
wts <- runif(length(tick))
wts <- wts/sum(wts)
# Storing weight in the matrix
all_wts[i,] <- wts
# Portfolio returns
port_ret <- sum(wts * mean_ret)
port_ret <- ((port_ret + 1)^252) - 1
# Storing Portfolio Returns values
port_returns[i] <- port_ret
# Creating and storing portfolio risk
port_sd <- sqrt(t(wts) %*% (cov_mat %*% wts))
port_risk[i] <- port_sd
# Creating and storing Portfolio Sharpe Ratios
# Assuming 0% Risk free rate
sr <- port_ret/port_sd
sharpe_ratio[i] <- sr
}
#storing the values in the table
portfolio_values <- tibble(Return = port_returns,
Risk = port_risk,
SharpeRatio = sharpe_ratio)
all_wts <- tk_tbl(all_wts)
## Warning in tk_tbl.data.frame(as.data.frame(data), preserve_index,
## rename_index, : Warning: No index to preserve. Object otherwise converted
## to tibble successfully.
colnames(all_wts) <- colnames(log_ret_xts)
portfolio_values <- tk_tbl(cbind(all_wts,portfolio_values))
## Warning in tk_tbl.data.frame(cbind(all_wts, portfolio_values)): Warning: No
## index to preserve. Object otherwise converted to tibble successfully.
head(portfolio_values)
## # A tibble: 6 x 8
## AAPL AMZN NFLX T XOM Return Risk SharpeRatio
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.285 0.171 0.276 0.0677 0.200 0.267 0.194 1.37
## 2 0.170 0.153 0.174 0.226 0.277 0.181 0.158 1.14
## 3 0.101 0.223 0.214 0.197 0.265 0.210 0.173 1.22
## 4 0.362 0.312 0.232 0.0310 0.0633 0.322 0.209 1.54
## 5 0.0717 0.00133 0.00703 0.0895 0.830 0.0133 0.166 0.0800
## 6 0.327 0.276 0.0120 0.380 0.00466 0.194 0.158 1.23
We have the weights in each asset with the rick and returns alongn with the sharpe ratio of each portfolio next take a look at the portfolios that matter the most * The minimum variance portfolio * The tangency portoflio(the portfolio with the hightest sharpe ratio)
min_var <- portfolio_values[which.min(portfolio_values$Risk),]
max_sr <- portfolio_values[which.max(portfolio_values$SharpeRatio),]
p <- min_var %>%
gather(AAPL:XOM, key = Asset,
value = Weights) %>%
mutate(Asset = as.factor(Asset)) %>%
ggplot(aes(x = fct_reorder(Asset, Weights), y = Weights, fill =Asset))+
geom_bar(stat = 'identity')+
theme_minimal()+
labs(x='Assets', y = 'Weights', title = "Minimum Variance Portfolio Weights")+
scale_y_continuous(labels = scales::percent)
ggplotly(p)
From this plot, we can observe the Minimum variance portfolio has no allocation to Netflix and very little allocation to Amazon. The majority of the portfolio is invested in Exxon Mobil andd AT&T stock.
Next lets look at the tangency portfolio or the portfolio with the highest sharpe ratio. Generally, the greater the value of the Sharpe ratio, the more attractive the risk-adjusted return.
p <- max_sr %>%
gather(AAPL:XOM, key = Asset,
value = Weights) %>%
mutate(Asset = as.factor(Asset))%>%
ggplot(aes(x= fct_reorder(Asset,Weights), y = Weights, fill =Asset))+
geom_bar(stat = 'identity')+
theme_minimal()+
labs(x= 'Assets', y = 'Weights', title = "Tangency Portfolio Weights")+
scale_y_continuous(labels = scales::percent)
ggplotly(p)
From the plot, we can observe that the portfolio with the highest sharpe ratio has very little invested in Exxon Mobil and AT&T. This portfolio has most of the assets investedd in Amazon, Netflix and Apple. Three best performinng stocks in the last decade.
Lastly, we plot all the random portfolios and visualize the efficient frontier.
p <- portfolio_values %>%
ggplot(aes(x = Risk, y = Return, color = SharpeRatio)) +
geom_point() +
theme_classic() +
scale_y_continuous(labels = scales::percent) +
scale_x_continuous(labels = scales::percent) +
labs(x = 'Annualized Risk',
y = 'Annualized Returns',
title = "Portfolio Optimization & Efficient Frontier") +
geom_point(aes(x = Risk,
y = Return), data = min_var, color = 'red') +
geom_point(aes(x = Risk,
y = Return), data = max_sr, color = 'red') +
annotate('text', x = 0.20, y = 0.42, label = "Tangency Portfolio") +
annotate('text', x = 0.18, y = 0.01, label = "Minimum variance portfolio") +
annotate(geom = 'segment', x = 0.14, xend = 0.135, y = 0.01,
yend = 0.06, color = 'red', arrow = arrow(type = "open")) +
annotate(geom = 'segment', x = 0.22, xend = 0.2275, y = 0.405,
yend = 0.365, color = 'red', arrow = arrow(type = "open"))
ggplotly(p)
In the plot above, we can observe that all 5000 portfolios. A investor will try to obtain portfolios that lie on the efficient frontier.