This is the weekly **Energy Sector** financial summary.  

```{r load_libraries, message = FALSE, warning = FALSE, echo = FALSE}
library(tidyverse)
library(forcats)
library(lubridate)
library(scales)
library(bdscale)

# setting up the path within the project for easier portability of code
thePath <- here::here()
tickers <- readr::read_csv(paste0(thePath, "/10_energy/", "tickers.csv"))

# Change some of the columns into factors
tickers[,4:7] <- tickers[,4:7] %>% mutate_all(funs(as_factor))
```

```{r download_data, eval = FALSE, echo = FALSE}
# Downloading data with AV
av_tickers <- tickers %>% select(av_ticker) %>% na.omit()

#av_tickers <- av_tickers[109:115,]

for (i in 1:nrow(av_tickers)){
  print(av_tickers$av_ticker[i])
  data = quantmod::getSymbols(Symbols = av_tickers$av_ticker[i],
                    src = "av",
                    api.key = "W7SHG93NFG5YWE2K", 
                    output.size = "full", 
                    auto.assign = FALSE,
                    index.class="POSIXct")
  colnames(data) = c("Open", "High", "Low", "Close", "Volume")
  zoo::write.zoo(data, paste0(thePath, "/10_energy/StockData_av/", av_tickers$av_ticker[i], ".csv"), 
                 sep = ",", row.names = FALSE)
  Sys.sleep(14)
}
```

```{r used_functions, echo = FALSE}
## function to read the .csv file for the given period
read_fin_data <- function(tickers, 
                          thePath = paste0(here::here(), "/10_energy/StockData_av/"),  
                          start_date = today()-365, end_date = today()) {
  df <- read_csv(paste0(thePath, tickers, ".csv"))
  df <- df %>% as_tibble() %>% select(Index, Close)
  df$Index <- ymd(df$Index)
  df <- df %>% filter(Index <= end_date) %>% 
    filter(Index >= start_date) %>% 
    mutate(adj_return = round((Close / first(Close) - 1)*100, 1))
  return(df)
}

# function calculate the essentials variables for each stock 
# DO I need week_number???????
create_big_df <- function(tickerA){
  yo <- read_csv(paste0(thePath, "/10_energy/StockData_av/", tickerA, ".csv")) 
  yo$Index <- ymd(yo$Index)
  yo <- yo %>% arrange(Index) %>% 
    mutate(year = year(Index), week_number = isoweek(Index), 
           days_of_week = wday(Index, label = TRUE, abbr = FALSE), 
           weekly_return = ((Close / lag(Close, 5)) - 1) * 100, 
           daily_return = ((Close / lag(Close)) - 1) * 100, 
           monthly_return = ((Close / lag(Close, 20)) - 1) * 100, 
           trimester_return = ((Close / lag(Close, 62)) - 1) * 100, 
           semester_return = ((Close / lag(Close, 120)) - 1) * 100, 
           yearly_return = ((Close / lag(Close, 252)) - 1) * 100, 
           rsi14 = TTR::RSI(Close, n = 14), 
           rsi5 = TTR::RSI(Close,  n = 5), 
           monthly_sharp_Ratio = round(RollingWindow::RollingMean(daily_return, window = 21, na_method = "ignore") / 
                                      RollingWindow::RollingStd(daily_return, window = 21, na_method = "ignore"), 3), 
           yearly_sharp_Ratio = round(RollingWindow::RollingMean(daily_return, window = 252, na_method = "ignore") / 
                                      RollingWindow::RollingStd(daily_return, window = 252, na_method = "ignore"), 3),
           bi_monthly_return = ((Close / lag(Close, 10)) - 1) * 100, 
           SMA50 = TTR::SMA(Close, n = 50), 
           SMA50 = (Close / SMA50) - 1, 
           SMA50_bin = if_else(SMA50 > 0, 1, 0), 
           roll_sd_sma50 = RollingWindow::RollingStd(SMA50, window = 250, na_method = "ignore"), 
           roll_mean_sma50 = RollingWindow::RollingMean(SMA50, window = 250, na_method = "ignore"), 
           diff_sd_sma50 = round((SMA50 - roll_mean_sma50) / roll_sd_sma50, 2)) %>% 
    #filter(days_of_week == "Monday") %>% tail(., 12) %>% 
    #mutate(bi_monthly_yes = rep(seq(0, 1), 6)) %>% 
    select(Index, days_of_week, weekly_return, bi_monthly_return, monthly_return, trimester_return, SMA50_bin, 
           semester_return, yearly_return, monthly_sharp_Ratio, yearly_sharp_Ratio, diff_sd_sma50) 
  }

```

```{r main_df, echo=FALSE, message = FALSE, warning = FALSE}
possibly_create_big_df <- possibly(create_big_df, otherwise = NA)

df2 <- tickers %>% select(ticker = av_ticker, ticker_name, ETF, ticker_industry, ticker_subsector) %>% 
  filter(ETF != "ETF") %>% select(-ETF) %>% na.omit() %>% 
  mutate(yoop = map(ticker, possibly_create_big_df)) %>% filter(!is.na(yoop)) %>% unnest() 

df2$Index <- ymd(df2$Index)

```

The *Weekly Energy Report* attempt to see the big trends in the energy sectors[^0]. 

At the moment, the report is build around `r nrow(tickers)` companies.  You can see their tickers, names, sectors and sub-sectors at [the end](#list_financial_instru) of the report. Also, the focus is really on North-American Oil & Gas companies.  We will address this shortcoming ~~soon~~ by adding more European and Asian energy companies to the mix as well as diversifying the industries to include uranium, electricity generations, renewable energies and industrials[^1].  

[^1]: We use the *AlphaVantage* [API](https://www.alphavantage.co/) to get our data.  

[^0]: The report has been generated with the R language and thanks to the many community members who have contributed their time in building packages.  
 
We would love to hear from you. Are there typos, mistakes?  Are graphs unclear?  not useful? Should we add additional resources and analysis?  Write us a [mail](mailto:f.deryckel@gmail.com)    

To read the last report, go [here](https://fderyckel.github.io/WER/reports/WER_2018_09_03.html) and to see the second to last one go [here](https://fderyckel.github.io/WER/reports/WER_2018_06_11.html)

# What made the news ?  {.tabset}

## Oil and Gas
* Transocean is buying its rival Ocean Rig in $2.7B deal.  => Confidence that the price of oil might stay high enough to warrant further investment in deep sea drilling. (some of the costlier oil exploration and exploitation energy business) 
* Qatar is investing in German gas.  Intention to invest in LNG port terminal.  The Qatari Wealth Fund has already invested over $25 B in german Cie (Deutsche Bank, Volswagen, AG, Siemens, etc. )  
* India is the second biggest iranian oil importer (after china).  India has found a way to deal (contravene) with US sanctions.  It is being shipped the oil CIF; that is Iran find the tankers and inssure them.  
* OPEC oil output increased.  Recovery in Lybian and Southern Irak export.  A less decrease from Iran.  Venezuela and Angola are still the biggest looser in term of output.  
* The US Permian bassin is colling off.  Halliburton warned of a decreased in EPS due to a decrease in renting of oil rig in the area.  
  + There is still issue with the Permian Bassin.  How to dispose of the unwanted water (which has been mixed with sands and chemicals) and natural gaz that comes with the oil extraction
  + Still a shortage of pipeline capacity that take oil from West Texas to the Gulf of Mexico.  


## Renewable Energies
* Orsted, a Danish independent utility company, finsihed building the biggest wind farm in the Irish see, 145 sq km.  It can produce 659 Mw (enough for 600,000 houses).  87 turbines.  The blade were made in the UK by both Vestas and Siemens Gamesa.  


## Uranium  
*  The **World Nuclear Association Symposium** kicks off on Wednesday, analysts expect discussion to centre on the recent supply cuts and the deal that saw Kazatomprom, the world’s biggest uranium producer, bypass the spot market and sell a large chunk of its annual output to Yellow Cake, a London-listed investment vehicle that is planning to buy and store a large amount of the metal in anticipation of higher prices. (FT.com)
    + After the 2011 Fukushima disaster, reactors in Japan and Germany shut down. Spot price for Uranium went from $70 to $20.  Now it is around $26.  
    + Inventory is still high.  So the recovery of price might not be so sudden.  
    + Kazatoprom has lowered its uranium output.  Cameco has stopped some of its operation.  

* 


## Utilities - Electricity Generations
* 

# Report higlights. 

*   Over the last 3 months, the general market with the SPY has outperformed all the energy sectors.  After a correction finishing mid-August, most ETF are staging a come back.  During the correction **MLPX**, the energy infrastructure ETF, has hold its ground pretty firm.  (a very defensive sub-sector during energy bear market).  
*  OVer the last 12 months, the **XOP** ETF, Energy Exploration and Production, has well outperformed the other.  The **XES**, the O&G Equiment and Services ETF, has lagged the rally.  


# General Energy overview {#ener_overview} 

List of ETF selected for the general overview.  
```{r energy_global_etf_choice, echo = FALSE, message = FALSE}
df_ticker <- tibble(ticker = c("VDE", "IXC", "KOL", "URA", "UNG", "MLPX", "USO", "SPY"))

df <- inner_join(df_ticker %>% select(av_ticker = ticker), tickers, by = "av_ticker") %>% 
  select(ticker = av_ticker, Name = ticker_name)

knitr::kable(df, caption = "Selected Energy related ETF")
```

Returns of the energy ETF over the last 3 months
```{r energy_3months_return, echo = FALSE, message = FALSE, warning=FALSE, out.width = 900}
df <- df_ticker %>% 
  mutate(price_data = map(ticker, function(.x) read_fin_data(.x, start_date = today() - 63))) %>% 
  unnest()

ggplot(df, aes(x = Index, y = adj_return, color = ticker)) + 
  geom_line() + 
  #scale_x_datetime() + 
  scale_x_bd(business.dates=df$Index, max.major.breaks = 10, labels=date_format("%d '%b"), expand = c(0,0.3)) + 
  ylab(label = "Adjusted return") + 
  ggtitle("3 months ETF return") + 
  theme(legend.title = element_blank(),
        axis.title.x = element_blank(), 
        plot.title = element_text(hjust=0.5, face = "bold"), 
        panel.background = element_rect(fill = "black"), 
        #panel.grid.minor = element_line(color = "white", linetype = "dotted", size = 0.2), 
        panel.grid.minor.x = element_blank(), 
        plot.background = element_rect(fill = "Gray 75"), 
        legend.key = element_rect(fill = "Gray 75"), 
        legend.background = element_rect(fill = "Gray 75"), 
        panel.grid.major.x = element_line(color = "white", linetype = "dotted", size = 0.16), 
        panel.grid.major.y = element_line(color = "Gray 65", linetype = "solid", size = 0.2), 
        panel.grid.minor.y = element_line(color = "Gray 65", linetype = "dotted", size = 0.2),
        axis.text.x = element_text(angle = 60, vjust = 0.5))

#plotly::plot_ly(df, x = ~Index, y = ~adj_return, color = ~ticker, 
#                type = "scatter", mode = "lines")  %>%
#  plotly::layout(title = '3 months ETF Returns', 
#                 xaxis = list(title = "Dates"), yaxis = list(title = "Return on Closed Prices (in %)"))
```

Returns of the energy ETF over the last 12 months
```{r energy_12months_return, echo = FALSE, message = FALSE, warning=FALSE, out.width = 900}
df <- df_ticker %>% 
  # Take out UNG has it has some weird split that have not been accounted for
  filter(ticker != "UNG") %>% 
  mutate(price_data = map(ticker, function(.x) read_fin_data(.x, start_date = today() - 365))) %>% 
  unnest()

ggplot(df, aes(x = Index, y = adj_return, color = ticker)) + 
  geom_line() + 
  #scale_x_datetime() + 
  scale_x_bd(business.dates=df$Index, max.major.breaks = 15, labels=date_format("%d '%b"), expand = c(0,0.3)) + 
  ylab(label = "Adjusted return") + 
  ggtitle("12 months ETF return") + 
  theme(legend.title = element_blank(),
        axis.title.x = element_blank(), 
        plot.title = element_text(hjust=0.5, face = "bold"), 
        panel.background = element_rect(fill = "black"), 
        panel.grid.minor = element_line(color = "white", linetype = "dotted", size = 0.2), 
        panel.grid.minor.x = element_blank(), 
        plot.background = element_rect(fill = "Gray 75"), 
        legend.key = element_rect(fill = "Gray 75"), 
        legend.background = element_rect(fill = "Gray 75"), 
        panel.grid.major.x = element_line(color = "white", linetype = "dotted", size = 0.2), 
        panel.grid.major.y = element_line(color = "Gray 65", linetype = "solid", size = 0.2), 
        panel.grid.minor.y = element_line(color = "Gray 65", linetype = "dotted", size = 0.15),
        axis.text.x = element_text(angle = 60, vjust = 0.5))


#library(plotly)
#plotly::plot_ly(df, x = ~Index, y = ~adj_return, color = ~ticker, 
#                type = "scatter", mode = "lines")  %>%
#  plotly::layout(title = '12 months ETF Returns', 
#                 xaxis = list(title = "Dates"), yaxis = list(title = "Return on Closed Prices (in %)"))
```



# The Oil & Gas Industry   {.tabset}
Following GISC, the Oil & Gas (OG) industry consists of the following sectors and sub-sectors: 
```{r display_sectors_OG, echo = FALSE}
yo <- tickers %>% filter(ticker_industry == "oil_gas") %>%  
  select(ticker_sector, ticker_subsector, ticker_name) %>% 
  group_by(ticker_sector, ticker_subsector) %>% summarize(numerb = n()) %>% 
  #unique() %>% 
  na.omit() %>% select(Sector = ticker_sector, Subsector = ticker_subsector, `Number of stocks` = numerb)

yo$Sector = str_replace_all(yo$Sector, c("energy_equipment_services" = "Equipment & Services", 
                                         "oil_gas_consumable_fuels" = "Consumable Fuels"))

yo$Subsector = str_replace_all(yo$Subsector, c("drilling" = "Drilling", 
                                               "equipment_services" = "Equipment & Services Cies", 
                                               "exploration_production" = "Exploration & Production Cies", 
                                               "integrated" = "Major Integrated Cies",
                                               "refining_marketing" = "Refining & Marketing Cies", 
                                               "storage_transportation" = "Storage & Transportation Cies"))

knitr::kable(yo, caption = "List of Sectors and Sub-sectors of the O&G Industry")
```

[At the bottom of this file](##list_financial_instru), there is the complete list of financial instruments with their sectors and sub-sectors on which the following analysis has been based on. 

## Long term trends
Let's check the main Oil & Gas ETF returns. 
We have picked the following ETF
```{r og_etf, echo = FALSE}
og_etf <- c("XEG.TO", "IXC", "VDE", "CHIE", "XOP", "XES", "XLE")
df <- tickers %>% filter(av_ticker %in% og_etf) %>% 
  select(Ticker = av_ticker, Name = ticker_name)
knitr::kable(df)
```

```{r etf_oneyar_return, echo = FALSE, out.width = 900, warning = FALSE, message = FALSE}
df <- tibble(ticker = og_etf)

df <- df %>% 
  mutate(price_data = map(ticker, function(.x) read_fin_data(.x))) %>% 
  unnest()
df$Index <- ymd(df$Index)

ggplot(df, aes(x = Index, y = adj_return, color = ticker)) + 
  geom_line() + 
  #scale_x_datetime() + 
  scale_x_bd(business.dates=df$Index, max.major.breaks = 15, labels=date_format("%d '%b"), expand = c(0,0.3)) + 
  #scale_x_datetime() + 
  ylab(label = "Adjusted return") + 
  ggtitle("12 months ETF return") + 
  theme(legend.title = element_blank(),
        axis.title.x = element_blank(), 
        plot.title = element_text(hjust=0.5, face = "bold"), 
        panel.background = element_rect(fill = "black"), 
        #panel.grid.minor = element_line(color = "white", linetype = "dotted", size = 0.2), 
        panel.grid.minor.x = element_blank(), 
        plot.background = element_rect(fill = "Gray 75"), 
        legend.key = element_rect(fill = "Gray 75"), 
        legend.background = element_rect(fill = "Gray 65"), 
        panel.grid.major.x = element_line(color = "white", linetype = "dotted", size = 0.2), 
        panel.grid.major.y = element_line(color = "Gray 65", linetype = "solid", size = 0.2), 
        panel.grid.minor.y = element_line(color = "Gray 65", linetype = "dotted", size = 0.15),
        axis.text.x = element_text(angle = 60, vjust = 0.5))

#library(plotly)
#plotly::plot_ly(df, x = ~Index, y = ~adj_return, color = ~ticker, 
#                type = "scatter", mode = "lines")  %>%
#  plotly::layout(title = '1 year ETF Returns', 
#                 xaxis = list(title = "Dates"), yaxis = list(title = "Return on Closed Prices"))

```

## Breadth of the Oil & Gas Market  {#og_breadth}
Usually, on shorter time frame, we use the 50 days Standard Moving Average (SMA) to see if a stock is in an uptrend.  The graphs below does exactly that: it checks the percentage of stocks that are trading above their 50 days SMA.  This also tell us when the sector might be over-extended to the upside or to the downside.  

```{r percent_stock_above_sma50, echo=FALSE, warning=FALSE, out.width = 700, dpi = 300}
df <- df2 %>% group_by(Index) %>% 
  summarise(number_stocks = n(), 
            stocks_above_sma50 = sum(SMA50_bin, na.rm = TRUE)) %>% 
  mutate(percentage_above = stocks_above_sma50 / number_stocks)

# filter df for the last 5 years
yo <- df %>% filter(Index >= today() - 5*365) %>% 
  #filtering out the days some markets are not open
  mutate(market_open = if_else(number_stocks < 0.75 * lag(number_stocks), 0, 1)) %>% 
  filter(market_open == 1)


ggplot(yo, aes(x = Index, y = percentage_above)) + 
  geom_line() + 
  geom_hline(yintercept = 0.875, color = "red", linetype = "dashed") + 
  geom_hline(yintercept = 0.125, color = "red", linetype = "dashed") + 
  scale_y_continuous(labels = scales::percent) + 
  scale_x_date(name = "Dates", date_breaks = "4 months", date_labels = "%b %y") + 
  theme(axis.title.y = element_blank(), 
        axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 60, vjust = 0.5), 
        panel.background = element_rect(fill = "white"), 
        panel.grid.minor.y = element_line(colour = "grey50", linetype = "dashed"), 
        panel.grid.major.y = element_line(colour = "grey80", linetype = "dashed")) + 
  ggtitle("Percentage of stocks above their 50 SMA")
```


## Weekly and bi-monthly returns  {#weekly_returns} 
In this section, we check the weekly return for the last 12 weeks for all stocks belonging to the industry, sectors and sub-sectors.  
```{r main_df_creation, message = FALSE, warning = FALSE, echo = FALSE}
df <- df2 %>% 
  filter(days_of_week == "Monday" & ticker_industry == "oil_gas") %>% group_by(ticker) %>% 
  arrange(desc(Index)) %>% 
  filter(row_number() %in% c(1:12)) %>% 
  mutate(bi_monthly_yes = rep(seq(0, 1), 6)) %>% 
  ungroup()

```


```{r weekly_return, warning=FALSE, echo = FALSE, out.width = 900, dpi = 300}
# Function to detect ouliers-ish (very loose definition)
is_outlier <- function(x) {
  return(x < quantile(x, 0.25) - 0.95 * IQR(x) | x > quantile(x, 0.75) + 0.95 * IQR(x))
}

yo <- ymd(as.character(df$Index)) 
yo <- tibble(dates = yo) %>% group_by(dates) %>% summarise(yup = unique(dates)) %>% arrange(desc(dates)) 

df %>% filter(Index >= yo$dates[12]) %>% 
  mutate(outlier = if_else(is_outlier(weekly_return), ticker, as.character(NA))) %>% 
  ggplot(., aes(x=as.factor(Index), y = weekly_return, col = Index)) + geom_boxplot() + 
  #scale_colour_gradient(low = "dodgerblue1", high = "dodgerblue4") + 
  theme(axis.text.x = element_text(colour="grey20", size=8, angle=50, hjust=.5, vjust=.5), 
        panel.background = element_rect(fill = "white"), 
        panel.grid.minor.y = element_line(colour = "grey50", linetype = "dashed"), 
        panel.grid.major.y = element_line(colour = "grey80", linetype = "dashed"), 
        legend.position = "none") + 
  geom_text(aes(label = outlier), na.rm = TRUE, hjust = -0.3, size = 2, check_overlap = TRUE) + 
  geom_hline(yintercept = 0, color = "red") + 
  ggtitle("Energy - Weekly Return") + ylim(c(-15, 15)) + 
  labs(x= "Dates", y="Weekly Return (in %)") 
```

We do the same graph but with a 2 weeks return and a disaggregation by sub-sector.  
```{r bi_monthly_return, warning=FALSE, echo = FALSE, out.width = 900, dpi = 300}
# bi-monthly return boxplot ordered with sub-sector
n <- seq(0:5) - 1
dates <- tibble(value = yo$dates[1] - 14*n)
df %>% filter(Index %in% unlist(dates)) %>% 
  group_by(Index) %>% 
  #mutate(outlier = if_else(is_outlier(bi_monthly_return), ticker, as.character(NA))) %>% 
  ggplot(., aes(x=as.factor(Index), y = bi_monthly_return, fill = ticker_subsector)) + geom_boxplot() + 
  theme(axis.text.x = element_text(colour="grey20", size=8, angle=50, hjust=.5, vjust=.5), 
        panel.background = element_rect(fill = "white"), 
        panel.grid.minor.y = element_line(colour = "grey50", linetype = "dashed"), 
        panel.grid.major.y = element_line(colour = "grey80", linetype = "dashed"), 
        legend.position = "top", legend.title = element_blank()) + 
  #geom_text(aes(label = outlier), na.rm = TRUE, hjust = -0.3, size = 2, check_overlap = TRUE) + 
  ggtitle("Energy - 2-Weeks Return") + ylim(c(-15, 15)) + 
  labs(x= "Dates", y="Bi-monthly Return (in %)")

```

## Recap table  
```{r trend_table_OG_prep, echo = FALSE, warning=FALSE}
yo <- df2 %>% filter(ticker_industry == "oil_gas") %>% group_by(ticker) %>% 
  filter(Index == max(Index)) %>% 
  ungroup() %>% 
  mutate_at(7:16, round, 2) %>% 
  select(Index, Ticker = ticker, `Ticker Name`= ticker_name, 
         `Rolling std 50 sma`= diff_sd_sma50, 
         `Monthly Sharp R`= monthly_sharp_Ratio, `Yearly Sharp R` = yearly_sharp_Ratio, 
         `Weekly return` = weekly_return, `Bi-monthly return` = bi_monthly_return, 
         `Monthly return` = monthly_return, 
         `Semester return` = semester_return, `Yearly return` = yearly_return)
yu <- yo$Index[1]
```

Now let's have a recap table to drill in at the company level.  This table has been generated with data from `r format(yu, '%d %B, %Y')`
```{r trend_table_OG, echo = FALSE}
## Make a js table with all the variable that can be sorted.  
DT::datatable(yo %>% select(-Index), options = list(pageLength = 10)) %>% 
  DT::formatRound(columns = 11, digits = 2)
```


# Uranium {.tabset}

# The list of financial instruments  {#list_financial_instru}
Here is the list of all equities used in this report, their sector and subsector.  
```{r equities_table, echo=FALSE}
#do graph by industry, sector
#order the bar by decreasing order.  
DT::datatable(tickers %>% select(-yahoo_ticker, -ETF, -GISC_Code), options = list(pageLength = 10), 
             caption = "List of stocks, sectors and sub-sectors") 
```

If you have enjoyed this report, [let us now](mailto:f.deryckel@gmail.com).  

