WACC (weighted average cost of capital) - can be measured at market values and accounting values. You cannot mix them up. They are completely different in a way for counting, so if you mix them up you will get incorrect result that may be misleading. At market values you need both equity and debt of company and the cost of equity and the cost of debt. This measure tells the investors the rate of return they might expect with how much risk they would bare on their investment.

Example:

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

Set User Agent (if necessary for your connection)

options(HTTPUserAgent = "igordros@icloud.com")

Load Microsoft stock data

getSymbols("MSFT") 
## [1] "MSFT"

Constants

outstanding_shares <- 30000000 # Number of outstanding shares
market_cap <- last(MSFT$MSFT.Adjusted) * outstanding_shares
cost_of_debt <- 0.03 # 3% 
tax_rate <- 0.25 # 25% 
beta <- 1.1 # Beta of Microsoft's stock 
risk_free_rate <- 0.02 # 2% risk-free rate 
market_risk_premium <- 0.06 # 6% market risk premium 
market_value_of_debt <- 100e9 # $100 billion 

Cost of Equity Calculation

cost_of_equity <- risk_free_rate + beta * market_risk_premium

Total Market Value

total_market_value <- market_cap + market_value_of_debt

Weights of Debt and Equity

weight_of_debt <- market_value_of_debt / total_market_value
weight_of_equity <- market_cap / total_market_value

WACC Calculation

wacc <- (weight_of_debt * cost_of_debt * (1 - tax_rate)) + (weight_of_equity * cost_of_equity)

Output WACC

cat("WACC for Microsoft:", wacc * 100, "%\n")
## WACC for Microsoft: 2.976346 %