#Q1. Install library tidyquant, timetk and quantmod. Import data tej_2016_2018.txt.

library(tidyquant)
library(timetk)
library(quantmod)
library(dplyr)
library(readr)
tej_2016_2018 <- read.delim("C:/Users/HP/Downloads/tej_2016_2018.txt")

#Q2. Select column CO_ID, DATE, Close. Convert CO_ID into text format, DATE into date format and change data from long format to wide format.

tej_2016_2018 %>% select(CO_ID, Date, Close)
as.character(tej_2016_2018$CO_ID)
library(anytime)
format(anydate(tej_2016_2018$Date), format="%B %d %Y")

#Q3. Convert data into xts format. Compute daily returns. Show the first five daily returns.

ret_day <- tej_2016_2018 %>% 
  tk_xts(select = -Date, date_var = Date) %>% 
  Return.calculate(method = "log") 

ret_day[is.na(ret_day)] <- 0
head(ret_day)

#Q4. Select the first 10 stocks and compute monthly returns. (Hint: you can use `to.monthly()` or `to.period()` to convert daily data into monthly data. Show the first 10 monthly returns. 

ret_mon <- stock_day_3_year %>% 
  tk_xts(select = -Date, date_var = Date) %>%
  to.period(period  = "months",
            indexAt = "lastof",
            OHLC    = FALSE)  %>%
  Return.calculate(method = 'log')

# Q5. Find the 20 largest market capitalization firms at the end of 2017 and 2018. And compute each firm’s market capitalization share of the total 20 largest firms. (Hint: `filter()`, `arrange()`, `slice()`, `sum()`)。

largest20_2017 <- tej_2016_2018 %>% 
  select(CO_ID, CoName, Date, Shares) %>%
  filter(Date == "2017-12-29") %>%
  mutate(year1 = year(Date)) %>%
  select(Date, year1, Shares, CO_ID, CoName) %>%
  arrange(desc(Shares)) %>%
  slice(1:20) %>%
  ungroup()
glimpse(largest20_2017)

largest20_2018 <- tej_stock %>% 
  select(CO_ID, CoName, Date, Shares) %>%
  filter(Date == "2018-12-28") %>%
  mutate(year2 = year(Date)) %>%
  select(Date, year2, Shares, CO_ID, CoName) %>%
  arrange(desc(Shares)) %>%
  slice(1:20) %>%
  ungroup()
glimpse(largest20_2018)

#Q6. Plot the share of each firm’s market cap in Q5 in descending order.

largest20_2017%>% 
  ggplot(aes(x = reorder(CO_ID, Shares, desc), y = Shares)) + 
  geom_col(fill = "pink") + 
  labs(x = "CO_ID") + 
  labs(y = 'Market share %')+
  labs(title = 'The share of each firm’s market cap in Q5 in descending order') +
theme(axis.text.x = element_text(face="bold", color="#001000", size=8, angle=90), axis.text.y = element_text(face="bold", color="#001000", size=8, angle=0))
  
largest20_2018%>% 
  ggplot(aes(x = reorder(CO_ID, Shares, desc), y = Shares)) + 
  geom_col(fill = "darkblue") + 
  labs(x = "CO_ID") +
  labs(y = 'Market share %')+
  labs(title = 'The share of each firm’s market cap in Q5 in descending order') +
theme(axis.text.x = element_text(face="bold", color="#001000", size=8, angle=90), axis.text.y = element_text(face="bold", color="#001000", size=8, angle=0))
  
#Q7. Based on the 20 firms selected from Q5, find their daily returns in 2018. (Hint: `filter()`)

ret_day_2018 <- largest20_2018 %>% 
  tk_xts(select = -Date, date_var = Date) %>% 
  Return.calculate(method = "log") 
ret_day_2018[is.na(ret_day_2018)] <- 0
head(ret_day_2018)