knitr::opts_chunk$set(root.dir = "C:/Users/rsman/Documents/PERS FOLDERS/RAJDEEP/INVESTMENTS/ANALYSIS/DATA")
library(lubridate)
library(stringr)
library(pander)
library(data.table)
library(xlsx)
library(dplyr)
library(scales)
library(ggplot2)
library(Quandl)
library(tabulizer)
library(knitr)

# libraries required 
library(magrittr)
library(rvest)
library(rio)
library(writexl)
library(readxl)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(root.dir = "C:/Users/rsman/Documents/PERS FOLDERS/RAJDEEP/INVESTMENTS/ANALYSIS/DATA")
#getwd()

###Functions
colFmt1 = function(x,color){
  outputFormat = knitr::opts_knit$get("rmarkdown.pandoc.to")
  if(outputFormat == 'latex')
    paste("\\textcolor{",color,"}{",x,"}",sep="")
  else if(outputFormat == 'html')
    paste("<font color='",color,"'>",x,"</font>",sep="")
  else
    x
}

colFmt = function(x,color){
  outputFormat = knitr::opts_knit$get("rmarkdown.pandoc.to")
  if(outputFormat == 'html')
    paste("<font color='",color,"'>",x,"</font>",sep="")
  else
    x
}

makeCharac <- function(rx.vr){
  new.vr <- as.character(rx.vr)
  return(new.vr)
}

makeNumeric <- function(rx.vr){
  new.vr <- as.numeric(rx.vr)
  return(new.vr)
}


###End Functions


pandoc.header(colFmt("Section-1: Line Graphs", 'red'), 1, 'setext')

Section-1: Line Graphs

CWD <- "C:/Users/rsman/Documents/PERS FOLDERS/RAJDEEP/INVESTMENTS/ANALYSIS/DATA/"
setwd("C:/Users/rsman/Documents/PERS FOLDERS/RAJDEEP/INVESTMENTS/ANALYSIS/DATA")
mf_2.df <- as.data.frame(read.csv(unzip("AMFI_metadata.csv.zip"), sep = ","))#AMFI Data
mf_2.df$n_company <- trimws(substr(mf_2.df[,2],1, str_locate(mf_2.df[,2],"-")-2))
mf_2.df$n_fund <- trimws(substr(mf_2.df$name, str_locate(mf_2.df$name,"-")+1, 500))

CWD <- getwd()

#ValueResearch.df <- as.data.frame(read.xlsx(paste0(CWD,"/PERFORMANCE/all_funds_portfolio.xlsx"), sheetIndex  = 1))#Fund Companies
#moneyctrl.df <- as.data.frame(read.xlsx(paste0(CWD,"/PERFORMANCE/moneycontrol_portfolio.xlsx"), sheetIndex  = 1))#Fund Companies
nse.df <- as.data.frame(read.xlsx(paste0(CWD,"/PERFORMANCE/nse_10_years.xlsx"), sheetIndex  = 1))#nse data d/l 13 Aug 2019
oil.df <- read.csv(file =paste0(CWD,"/PERFORMANCE/oil_prices_file.csv"))
gold.df <- as.data.frame(read.xlsx(paste0(CWD,"/PERFORMANCE/gold_prices.xlsx"), sheetIndex  = 1))

#user_ans <- readline(prompt =  "data updated till Do you want to Update?")
user_ans <- "N"

#ifelse(user_ans == "N", jump_to(148), jump_to(100))
if(user_ans == "Y" | user_ans == "y"){
  
  start_date <- format(as.Date(max(nse.df$Date))+1, "%d-%m-%Y") 
  date_diff <- today() - as.Date(max(nse.df$date)) 
  end_date <- ifelse(as.numeric(date_diff) > 89, format(as.Date(max(nse.df$Date))+89, "%d-%m-%Y"), format(as.Date(max(nse.df$Date))+ as.numeric(date_diff), "%d-%m-%Y") )  
  
  a <-"https://www1.nseindia.com/products/dynaContent/equities/indices/historicalindices.jsp?indexType=NIFTY%2050"
  # pasting paramters to the URL
  b <-paste0("&fromDate=",start_date)
  c <-paste0("&toDate=",end_date)
  
  # the final link 
  nse_url <-paste0(a,b,c)
  
  # getting table of data from the URL
  
  pagina <- read_html(nse_url, as.data.frame=T, stringsAsFactors = TRUE)
  
  #creating a function with read_html to read the web page.
  
  pagina %>%  
    html_nodes("table") %>% 
    #indicating that this is the table we want to extract.
    .[[1]] %>% 
    html_table(fill=T) -> x
  
  # dropping un needed columns
  colnames(x) = x[2,]
  x <- x[-(1:2),]
  x <- x[-nrow(x),]
  row.names(x) <- NULL
  x$Open <- makeNumeric(x$Open)
  x$High <- makeNumeric(x$High)
  x$Low <- makeNumeric(x$Low)
  x$Close <- makeNumeric(x$Close)
  x$`Shares Traded` <- makeNumeric(x$`Shares Traded`)
  x$`Turnover (  Cr)`<- makeNumeric(x$`Turnover (  Cr)`)
  colnames(nse.df) <- colnames(x)
  x$Date <- dmy(x$Date)
  nse.df <- rbind(nse.df, x)
  
  # getting the file into your PC
  #write_xlsx(nse.df, paste0(CWD,"/PERFORMANCE/nse_10_years.xlsx"), sheetIndex  = 1)))
  
  write_xlsx(nse.df, paste0(CWD, "/PERFORMANCE/nse_10_years.xlsx"))
  
  #########
  d <-"https://fred.stlouisfed.org/graph/fredgraph.csv?bgcolor=%23e1e9f0&chart_type=line&drp=0&fo=open%20sans&graph_bgcolor=%23ffffff&height=450&mode=fred&recession_bars=on&txtcolor=%23444444&ts=12&tts=12&width=1168&nt=0&thu=0&trc=0&show_legend=yes&show_axis_titles=yes&show_tooltip=yes&id=DCOILBRENTEU&scale=left&line_color=%234572a7&link_values=false&line_style=solid&mark_type=none&mw=3&lw=2&ost=-99999&oet=99999&mma=0&fml=a&fq=Daily&fam=avg&fgst=lin&fgsnd=2009-06-01&line_index=1&transformation=lin&vintage_date=2020-05-06&revision_date=2020-05-06&nd=1987-05-20"
  
  # setting start date and end date  in (yyyy-mm-dd) format / customized by you 
  
  start_date = as.character("2010-01-04")
  end_date = as.character("2020-05-01")
  
  # pasting paramters to the URL
  f <-paste0("&cosd=",start_date)
  c <-paste0("&coed=",end_date)
  
  # the final link
  second_url <-paste0(d,f,c)
  
  # getting the file into your PC
  
  destfile <- "C:/Users/rsman/Documents/PERS FOLDERS/RAJDEEP/WEB SITE/Project-7-AUM-FII/upwork/Oil_Prices_file.csv"
  destfile <- (paste0(CWD,"/PERFORMANCE/oil_prices_file.csv"))
  
  download.file(second_url,destfile, mode = 'wb')
  
  # preparing file2
  
  oil.df <- read.csv(file =paste0(CWD,"/PERFORMANCE/oil_prices_file.csv") )
  
  
  
  
}



##################
nse.df <- as.data.frame(read.xlsx(paste0(CWD,"/PERFORMANCE/nse_10_years.xlsx"), sheetIndex  = 1))#nse data d/l 13 Aug 2019

oil.df <- read.csv(file =paste0(CWD,"/PERFORMANCE/oil_prices_file.csv"))
#oil.df$DATE <- ymd(oil.df$DATE)
oil.df$DATE <- mdy(oil.df$DATE)
oil.df$DCOILBRENTEU <- makeNumeric(oil.df$DCOILBRENTEU)
## Warning in makeNumeric(oil.df$DCOILBRENTEU): NAs introduced by coercion
max.yr <- max(year(nse.df$Date))
min.yr <- min(year(nse.df$Date))

nse.df <- merge(nse.df, oil.df, by.x = "Date", by.y = "DATE")
nse.df <- merge(nse.df, gold.df, by.x = "Date", by.y = "Date")

nse.df$fin_year <- ifelse(month(nse.df$Date)>=4, 
                          paste0(year(nse.df$Date),"-", year(nse.df$Date)+1),paste0(year(nse.df$Date)-1,"-", year(nse.df$Date))) 
 

pandoc.header(colFmt("1A: Turnover Vs No of Shares traded", 'blue'), 1, 'setext')

1A: Turnover Vs No of Shares traded

temp.df<- nse.df
ggplot(temp.df, aes(Date))+ 
  geom_line(aes(y=Turnover....Cr., color = "Turnover(Cr)"))+
  geom_line(aes(y=Shares.Traded/10000, color = "Shares/10000")) +
  scale_color_manual("", breaks = c("Turnover(Cr)","Shares/10000"), values = c("red", "blue"))+
  ggtitle("Period: 2010 to 2019")

plotter.1 <- function(rx.df, yr1, yr2){
  
  plot.prep <-  ggplot(rx.df, aes(Date))+ 
    geom_line(aes(y=Turnover....Cr., color = "Turnover(Crore)"))+
    geom_line(aes(y=Shares.Traded/10000, color = "Shares/10000"))+
    geom_line(aes(y=DCOILBRENTEU*100, color ="Crude"))+
    geom_line(aes(y=Indian.rupee/2, color ="Indian.rupee/2"))+
    scale_color_manual("", breaks = c("Turnover....Cr.","Shares/10000", "Crude","Indian.rupee/2"), values = c("red", "blue", "black", "gold"))+
    ggtitle(paste("Period: " , yr1, "To ",  yr2))
  
  

  plot.prep
  
}
pandoc.header(colFmt("1B: Plot of Turnover,No of Shares traded, Brent Price, Gold", 'blue'), 1, 'setext')

1B: Plot of Turnover,No of Shares traded, Brent Price, Gold

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-9,"-", max.yr-8) & nse.df$Indian.rupee != 0,], max.yr-9, max.yr-8)
## Warning: Removed 247 row(s) containing missing values (geom_path).

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-8,"-", max.yr-7) & nse.df$Indian.rupee != 0,], max.yr-8, max.yr-7)
## Warning: Removed 247 row(s) containing missing values (geom_path).

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-7,"-", max.yr-6) & nse.df$Indian.rupee != 0,], max.yr-7, max.yr-6)
## Warning: Removed 248 row(s) containing missing values (geom_path).

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-6,"-", max.yr-5) & nse.df$Indian.rupee != 0,], max.yr-6, max.yr-5)
## Warning: Removed 242 row(s) containing missing values (geom_path).

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-5,"-", max.yr-4) & nse.df$Indian.rupee != 0,], max.yr-5, max.yr-4)
## Warning: Removed 247 row(s) containing missing values (geom_path).

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-4,"-", max.yr-3) & nse.df$Indian.rupee != 0,], max.yr-4, max.yr-3)
## Warning: Removed 246 row(s) containing missing values (geom_path).

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-3,"-", max.yr-2) & nse.df$Indian.rupee != 0,], max.yr-3, max.yr-2)
## Warning: Removed 246 row(s) containing missing values (geom_path).

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-2,"-", max.yr-1) & nse.df$Indian.rupee != 0,], max.yr-2, max.yr-1)
## Warning: Removed 248 row(s) containing missing values (geom_path).
## Warning: Removed 1 row(s) containing missing values (geom_path).

plotter.1(nse.df[nse.df$fin_year== paste0(max.yr-1,"-", max.yr) & nse.df$Indian.rupee != 0,], max.yr-1, max.yr)
## Warning: Removed 245 row(s) containing missing values (geom_path).