Stocks and Dividends

Data Computing

Computing project

Stocks and Dividends

Many companies are publicly traded. This means that the company issues stock certificates which can be bought and sold on an exchange. Investors buy stock certificates mainly because they can sell them in the future, perhaps making a profit, and because companies pay dividends, a share of the company’s profit, to each shareholder.

Figure 1. Stock prices for Ford, Honda, and Toyota

Figure 1. Stock prices for Ford, Honda, and Toyota

Figure 2. Accumulated dividends (per share) paid by Ford, Honda, and Toyota Figure 2. Accumulated dividends (per share) paid by Ford, Honda, and Toyota

Figures 1 and 2 show the scale of price fluctuations and of accumulated dividends.

Task

Compare the income (or loss) that comes from buying and selling a stock certificate to the income that comes from dividends over the same period. Answer these questions:

Getting Price Data

Sites such as finance.yahoo.com collect and distribute information about individual companies. You can use the readStockPrices() function (in the DataComputing package) to read such data directly from Yahoo into R.

For example, here are some automotive stocks and their daily prices from 2010 to 2015.

companies <- c("F", "TM", "HMC")
Prices <- 
  read_stock_prices(companies, what="daily",
                  start_year=2000, end_year=2015) 

Buy/Sell Profit

  action       date
1    buy 2006-01-03
2   sell 2014-12-30
The Actions table constructed by the previous command.

Pick a buy date and a sell date. You can use a command like this to create your data table:

Combine Prices and Actions to produce a table like this which shows the profits from buying at the start of 2006 and selling at the end of 2014.1 The dollar amount is profit per share.

Hints: (1) What kind of join should you use so that you get only those cases that match one of the dates in the Actions table? (2) Wide-vs-long techniques will be useful. (See Data Computing Chapter 11.)

From the data table with buy and sell prices, calculate the dollar amount of profit (or loss) and the percentage change, as shown here.

  company profit percent
1       F   7.67   98.00
2     HMC   0.24    0.82
3      TM  19.10   18.00
Profit and percentage change in price.

Indexing Prices

Since stock prices vary markedly from one company to another, a common practice is to “index” the price to a particular date as in Figure 3. (Question: In the graph, roughly which date was used for the reference?)

ref_date <- ymd("2005-01-03")
Reference <-
  Prices %>% 
  filter(date==ref_date) %>%
  select(company, standard=close)

Figure 3. Indexed stock prices for Ford, Honda, and Toyota Figure 3. Indexed stock prices for Ford, Honda, and Toyota

Dividends

You can read in dividend data like this:

Dividends <- 
  read_stock_prices(companies, what="dividends") 

Once you have the dividend data, extract out the dividends for all dates between your buy and sell dates. (Hint: Join Dividends to Actions using company to match.)

  company total_dividend
1       F          1.750
2     HMC          1.885
3      TM          6.259
Table 1. Dividends (in dollars per share) paid out by each company over the interval 2005 through 2014.