**Introduction **

**
**In my previous post, I compared oil futures against current OPEC oil price. In this post, I want to take advantage of R to optimize a portfolio of oil and gas companies. Portfolio optimization is a method of diversifying investments to to reach a specified level of risk and return. The portfolio I am building is comprised of companies specializing in oil and gas. Specifically, those in the extraction and processing stage.

My target portfolio will be minimizing variance – or risk. By minimizing risk, I am accepting less return on my investment.

**Goal**

**
**To identify O&G firms from NASDAQ with the least variance in stock price after January 2014. These firms would be the best choice for a low-risk portfolio focusing on petrochemicals.

**Technique **

I learned about the basics of portfolio optimization from an online thesis (Engels, 2004). The first two chapters cover the fundamentals of optimization theory.

The names of companies came from the NASDAQ website. NASDAQ has their data available in a .csv format which I downloaded and imported into R. I filtered the data to show only those in Oil & Gas.

NASDAQ = read.csv(“companylist.csv”,header=T) #import data

NASDAQ_oil = subset(NASDAQ, grepl(“Oil”,as.character(Industry),ignore.case = TRUE) & grepl(“Energy”,as.character(Sector),

ignore.case = TRUE)) #filter data

oil_list = as.vector(as.matrix(NASDAQ_oil)[,1]) #extract ticker symbol from dataset

Next, I got stock prices for each ticker symbol. I chose to ‘train’ the portfolio algorithm with data from January 2014 to present. I chose this period because it included the 2015 oil glut – meaning a more holistic view.

library (quantmod)

library(PerformanceAnalytics)

getSymbols(oil_list,from = ‘2014-01-01′)

return_matrix = NULL

for( symbol in oil_list)

{

return_matrix = merge.xts(return_matrix,Return.calculate(Ad(get(paste(symbol))),method=’discrete’))

}

*quantmod*‘ package is useful because it extracts data from multiple sources, including Google Finance and Yahoo Finance. The getSymbol() function extracts stock prices for the array of ticker symbols. I used the ‘

*PerformanceAnalytics*‘ package to calculate cumulative returns matrix. The ‘

*PerformanceAnalytics*‘ package can create some aesthetic graphs as well.

(Figure 1) Cumulative Returns of Various O&G Companies |

Since October 2014, the performance of many O&G firms had a downward trend. As explained in my previous post, the trend came from uncontrolled overproduction from OPEC. Around July 2016, the downward trend plateaus. The plateau could come from recent news that OPEC, and other countries, would agree to cut production (Cunningham, 2017).

The ‘*quantmod*‘ package is useful because it extracts data from multiple sources, including Google Finance and Yahoo Finance. I took this from an amazing tutorial by Kyle Balkissoon (Balkissoon, 2016). Kyle’s tutorial builds on portfolio optimization in greater detail.

The getSymbol() function extracts stock prices for the array of ticker symbols. I used the ‘*PerformanceAnalytics*‘ package to calculate cumulative returns matrix. The ‘*PerformanceAnalytics*‘ package can create some aesthetic graphs as well.

The next step is to construct the actual portfolio.

What is essentially happening here is that I am creating an object called minSD.opt. This object is essentially a list of user-defined constraints. My minimum variance portfolio reduces the risk to within the defined standard deviation of 0 to 0.3. The “full_investment” constraint means the sum of portfolio weights should add to 1 – meaning the whole portfolio is currently invested. I assume the “long_only” constraint means that the portfolio size does not change. Further clarification on these constraints would be appreciated.

library(DEoptim)

OptimizedPortfolioMinVariance=optimize.portfolio(

R=return_matrix,

portfolio=MinimumVariancePortfolio,

trace=TRUE)

**
**At this point, I attempt to optimize my portfolio with the list of constraints in the MinimumVariancePortfolio object. The optimization should take no more than a minute max.

x<-extractWeights(OptimizedPortfolioMinVariance) #extract weights of portfolio

x=x[order(-x)] #order list based on proportion

z10] #remove companies that have zero weights

z<-as.data.frame(z1) #turn into data frame

r<-sub(‘.Adjusted’, ”, rownames(z)) #by default, name has “.Adjusted” concatenated to it. Remove the “.adjusted” suffix

b = (data.frame(r,as.vector(z1))) #turn list into dataframe

barplot(b$as.vector.z1.,names.arg = r, las = 2, ylab = “Portfolio Proportion”,border = NA,main = “Oil & Gas Portfolio with Minimum Variance”,col = ‘brown1’) #get a barplot

Finally, I extract the proportion of firms in my portfolio as a list. I clean and order the data to make it look more appealing. In the end, I create a barplot to visually illustrate the O&G firms with the lowest risk since January 2014 (Figure 2).

(Figure 2) Weights of Minimum Variance O&G Portfolio |

**Resources**