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 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.
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:
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)
Choose a few companies of interest to you. You can find stock company symbols at finance.yahoo.com
. (Suggestion: pick a sector of the economy, e.g. energy, high-tech, consumer products, etc. and use companies from that sector.)
Plot out the “closing price” (Close
) versus date
to get a graphic like Figure 1.
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.
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?)
date
, company
, and close
variables, renaming close
as standard
. Call the resulting data frame 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
Reference
with each day’s price data for that company. You’ll find the standardized price on each day by creating a new variable which is the ratio of the day-to-day price (use Close
) to the standard
for that company. Before you can do this, you’ll need to combine the Prices
and Reference
data tables. You’ll use a join verb to do this. In order to check your results, sketch out what you think the result should be before you do the join.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.