Introduction
On the fourth part of my Oil&Gas series, I wanted to go deeper into formula investing using O&G firms only as an example. Dollar-Cost Averaging (DCA) is the term given to the process of buying a fixed dollar amount of stocks every week, month or quarter. DCA averages decline and upturn in stocks, building a portfolio more robust against fluctuations.
I first came across this concept in The Wealthy Barber, a book I found lying in my basement and put to good use. Today, I will be using R to perform DCA analysis of Suncor. The analysis will be performed from a worst-case perspective: buying stock as it increases in value – only to have oil crash and plateau at a price much lower than what you began with.
Suncor (SU) stocks will be brought on the first Monday of each month beginning on May 2014, when Suncor was showing a strong growth at $38, till May 2017, when it plateaued around $31. I assumed a monthly investment of USD $1,000 into Suncor with a $30 commission per trade. I found that using DCA yielded an average price per share of $30.18 by May 2017. If sold on May 15th, The net profit is $1,810. With commission, the profit is a modest $700. With dividends, we can expect a greater yeild.
If spread over time, even a loss can be profitable. Or, at the very least, safe.
![]() |
2015’s Oil Glut caused a drop on Suncor’s stock, which it is still recovering from |
Technique
All coding is done in R, a statistical programming language. First, I loaded the necessary packages. I used Quandl to extract historical financial data.
#load necessary libraries
library(“Quandl”)
library(“sqldf”)
![]() |
Quandl captures financial data from a range of companies, as Suncor |
I defined necessary variables. Feel free to experiment with different values.
#define variables
start_date = “01-05-2014”
end_date = “15-05-2017”
ticker = “GOOG/NYSE_SU”
To simplify repetitive simulations, I put most of the computation in its own function. My secret ingredient was sqldf. Sqldf is a package that allows me to code in SQL within a R environment, opening the way for powerful querying and analysis.
#Dollar Cost Averaging Function
DCA_calc <- function(x){
names(x)[1]=”date”
names(x)[2]=”price”
x$date = as.character(x$date)
a<-sqldf(“select min(substr(date,9,2)), date, price from x group by substr(date,1,7)”)
b<-sqldf(“select sum(1000/price) d,(select count(price) from a) e from a”)
c<-sqldf(“select (e*1000)/(d) AveragePricePerShare, d TotalNumberofStocks, e*1000 TotalCapitalSpent from b”)
return (c)
}
Finally call the function.
#call Quandl function
Suncor_price=data.frame(Quandl(ticker,trim_start=start_date,trim_end=end_date,type=”raw”))
result=DCA_calc(Suncor_price)
Conclusions
DCA only works when the stock has spent a considerable time at a much lower value than what it had originally began. Thus, even a slight increase in price can yeild a profit or dramatically cut losses. Additionally, this post does not factor in the dividends gained from the company. Factoring dividends can yeild higher profits.
References