#Clear Environment
rm(list = ls())
#install.packages("tidyquant")
#Load Packages
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
##
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
##
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
tq_get("NFLX", get = "stock.prices")
## # A tibble: 2,717 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NFLX 2012-01-03 10.0 10.4 10.0 10.3 29213800 10.3
## 2 NFLX 2012-01-04 10.3 11.6 10.3 11.5 100489900 11.5
## 3 NFLX 2012-01-05 11.3 11.7 11.0 11.3 86552900 11.3
## 4 NFLX 2012-01-06 11.3 12.5 11.2 12.3 125610800 12.3
## 5 NFLX 2012-01-09 12.8 14.2 12.4 14.0 214073300 14.0
## 6 NFLX 2012-01-10 14.3 14.3 13.2 13.7 146335000 13.7
## 7 NFLX 2012-01-11 13.6 13.7 13.1 13.2 87148600 13.2
## 8 NFLX 2012-01-12 13.2 13.3 12.7 13.2 61243000 13.2
## 9 NFLX 2012-01-13 13.1 13.8 12.9 13.5 88839100 13.5
## 10 NFLX 2012-01-17 13.7 14.0 13.5 13.5 58832200 13.5
## # … with 2,707 more rows
sp_500 <- tq_index("SP500") %>%
tq_get(get = "stock.prices")
## Getting holdings for SP500
tq_transmute_fun_options()
## $zoo
## [1] "rollapply" "rollapplyr" "rollmax"
## [4] "rollmax.default" "rollmaxr" "rollmean"
## [7] "rollmean.default" "rollmeanr" "rollmedian"
## [10] "rollmedian.default" "rollmedianr" "rollsum"
## [13] "rollsum.default" "rollsumr"
##
## $xts
## [1] "apply.daily" "apply.monthly" "apply.quarterly" "apply.weekly"
## [5] "apply.yearly" "diff.xts" "lag.xts" "period.apply"
## [9] "period.max" "period.min" "period.prod" "period.sum"
## [13] "periodicity" "to_period" "to.daily" "to.hourly"
## [17] "to.minutes" "to.minutes10" "to.minutes15" "to.minutes3"
## [21] "to.minutes30" "to.minutes5" "to.monthly" "to.period"
## [25] "to.quarterly" "to.weekly" "to.yearly"
##
## $quantmod
## [1] "allReturns" "annualReturn" "ClCl" "dailyReturn"
## [5] "Delt" "HiCl" "Lag" "LoCl"
## [9] "LoHi" "monthlyReturn" "Next" "OpCl"
## [13] "OpHi" "OpLo" "OpOp" "periodReturn"
## [17] "quarterlyReturn" "seriesAccel" "seriesDecel" "seriesDecr"
## [21] "seriesHi" "seriesIncr" "seriesLo" "weeklyReturn"
## [25] "yearlyReturn"
##
## $TTR
## [1] "adjRatios" "ADX" "ALMA"
## [4] "aroon" "ATR" "BBands"
## [7] "CCI" "chaikinAD" "chaikinVolatility"
## [10] "CLV" "CMF" "CMO"
## [13] "CTI" "DEMA" "DonchianChannel"
## [16] "DPO" "DVI" "EMA"
## [19] "EMV" "EVWMA" "GMMA"
## [22] "growth" "HMA" "keltnerChannels"
## [25] "KST" "lags" "MACD"
## [28] "MFI" "momentum" "OBV"
## [31] "PBands" "ROC" "rollSFM"
## [34] "RSI" "runCor" "runCov"
## [37] "runMAD" "runMax" "runMean"
## [40] "runMedian" "runMin" "runPercentRank"
## [43] "runSD" "runSum" "runVar"
## [46] "SAR" "SMA" "SMI"
## [49] "SNR" "stoch" "TDI"
## [52] "TRIX" "ultimateOscillator" "VHF"
## [55] "VMA" "volatility" "VWAP"
## [58] "VWMA" "wilderSum" "williamsAD"
## [61] "WMA" "WPR" "ZigZag"
## [64] "ZLEMA"
##
## $PerformanceAnalytics
## [1] "Return.annualized" "Return.annualized.excess"
## [3] "Return.clean" "Return.cumulative"
## [5] "Return.excess" "Return.Geltner"
## [7] "zerofill"
sp_500 %>%
group_by(symbol) %>%
tq_transmute(adjusted, mutate_fun = monthlyReturn)
## # A tibble: 63,396 × 3
## # Groups: symbol [503]
## symbol date monthly.returns
## <chr> <date> <dbl>
## 1 AAPL 2012-01-31 0.110
## 2 AAPL 2012-02-29 0.188
## 3 AAPL 2012-03-30 0.105
## 4 AAPL 2012-04-30 -0.0260
## 5 AAPL 2012-05-31 -0.0107
## 6 AAPL 2012-06-29 0.0109
## 7 AAPL 2012-07-31 0.0458
## 8 AAPL 2012-08-31 0.0939
## 9 AAPL 2012-09-28 0.00280
## 10 AAPL 2012-10-31 -0.108
## # … with 63,386 more rows
tq_index_options()
## [1] "DOW" "DOWGLOBAL" "SP400" "SP500" "SP600"
tq_index("SP600")
## Getting holdings for SP600
## # A tibble: 601 × 8
## symbol company ident…¹ sedol weight sector share…² local…³
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 ADC Agree Realty Corporation 008492… 2062… 0.00617 Real … 151930 USD
## 2 SM SM Energy Company 78454L… 2764… 0.00609 Energy 233453 USD
## 3 LTHM Livent Corporation 53814L… BD9P… 0.00604 Mater… 341302 USD
## 4 AMN AMN Healthcare Services … 001744… 2813… 0.00589 Healt… 82433 USD
## 5 ENSG Ensign Group Inc. 29358P… B1YW… 0.00555 Healt… 105272 USD
## 6 CYTK Cytokinetics Incorporated 23282W… BBBS… 0.00543 Healt… 179195 USD
## 7 EXPO Exponent Inc. 30214U… 2330… 0.00543 Indus… 97272 USD
## 8 PBF PBF Energy Inc. Class A 69318G… B7F4… 0.00538 Energy 204200 USD
## 9 UFPI UFP Industries Inc. 90278Q… BMQ6… 0.00538 Indus… 117276 USD
## 10 HP Helmerich & Payne Inc. 423452… 2420… 0.00533 Energy 200472 USD
## # … with 591 more rows, and abbreviated variable names ¹identifier,
## # ²shares_held, ³local_currency
tq_exchange_options()
## [1] "AMEX" "NASDAQ" "NYSE"
tq_get_options()
## [1] "stock.prices" "stock.prices.japan" "dividends"
## [4] "splits" "economic.data" "quandl"
## [7] "quandl.datatable" "tiingo" "tiingo.iex"
## [10] "tiingo.crypto" "alphavantager" "alphavantage"
## [13] "rblpapi"
mkc_prices <- tq_get("MKC", get = "stock.prices", from = " 1990-01-01")
mkc_prices
## # A tibble: 8,264 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MKC 1990-01-02 0 3.22 3.17 3.20 569600 1.63
## 2 MKC 1990-01-03 0 3.22 3.19 3.19 1988800 1.63
## 3 MKC 1990-01-04 0 3.22 3.19 3.20 355200 1.63
## 4 MKC 1990-01-05 0 3.22 3.17 3.17 3070400 1.62
## 5 MKC 1990-01-08 0 3.20 3.16 3.17 3081600 1.62
## 6 MKC 1990-01-09 0 3.19 3.03 3.03 2798400 1.55
## 7 MKC 1990-01-10 0 3.03 2.77 2.78 5043200 1.42
## 8 MKC 1990-01-11 0 2.86 2.80 2.80 7345600 1.43
## 9 MKC 1990-01-12 0 2.81 2.71 2.81 7579200 1.44
## 10 MKC 1990-01-15 0 2.81 2.75 2.77 1875200 1.41
## # … with 8,254 more rows
#For Japanese Market, use .japan
x8411T <- tq_get("8411.T", get = "stock.prices.japan", from = "2016-01-01", to = "2016-12-31")
#For FRED Data
#Check out natural gas prices
ngas_price_usd <- tq_get("PNGASEUUSDM", get = "economic.data")
ngas_price_usd
## # A tibble: 128 × 3
## symbol date price
## <chr> <date> <dbl>
## 1 PNGASEUUSDM 2012-01-01 12.3
## 2 PNGASEUUSDM 2012-02-01 12.2
## 3 PNGASEUUSDM 2012-03-01 12.5
## 4 PNGASEUUSDM 2012-04-01 12.6
## 5 PNGASEUUSDM 2012-05-01 12.6
## 6 PNGASEUUSDM 2012-06-01 12.6
## 7 PNGASEUUSDM 2012-07-01 11.4
## 8 PNGASEUUSDM 2012-08-01 11.4
## 9 PNGASEUUSDM 2012-09-01 11.4
## 10 PNGASEUUSDM 2012-10-01 11.6
## # … with 118 more rows
None of this work because I don’t have the relevant financial data accounts required to set up these APIs, but if I did…
#This is useful if you have a Quandl account with access to other financial and economic databases
#This won't work because I don't
# quandl_api_key("<your-api-key>")
# quandl_search(query = "Oil", database_code = "NSE", per_page = 3)
# c("WIKI/FB", "WIKI/AAPL") %>%
# tq_get(get = "quandl",
# from = "2016-01-01",
# to = "2016-12-31")
# c("WIKI/FB", "WIKI/AAPL") %>%
# tq_get(get = "quandl",
# from = "2007-01-01",
# to = "2016-12-31",
# column_index = 11,
# collapse = "annual",
# transform = "rdiff")
#Same thing wit Tiingo...
# tiingo_api_key('<your-api-key>')
# # Tiingo Prices (Free alternative to Yahoo Finance!)
# tq_get(c("AAPL", "GOOG"), get = "tiingo", from = "2010-01-01")
#
# # Sub-daily prices from IEX ----
# tq_get(c("AAPL", "GOOG"),
# get = "tiingo.iex",
# from = "2020-01-01",
# to = "2020-01-15",
# resample_frequency = "5min")
#
# # Tiingo Bitcoin in USD ----
# tq_get(c("btcusd"),
# get = "tiingo.crypto",
# from = "2020-01-01",
# to = "2020-01-15",
# resample_frequency = "5min")
#And Alpha Vantage...
#
# av_api_key("<your-api-key>")
# # Scaling is as simple as supplying multiple symbols
# c("FB", "MSFT") %>%
# tq_get(get = "alphavantage", av_fun = "TIME_SERIES_INTRADAY", interval = "5min")
#And bloomberg...
# blpConnect()
# # Get Bloomberg data in a tidy data frame
# my_bloomberg_data <- c('SPX Index','ODMAX Equity') %>%
# tq_get(get = "Rblpapi",
# rblpapi_fun = "bdh",
# fields = c("PX_LAST"),
# options = c("periodicitySelection" = "WEEKLY"),
# from = "2016-01-01",
# to = "2016-12-31")
Mutating Data
data("FANG")
FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted, mutate_fun = to.monthly, indexAt = "lastof")
## # A tibble: 192 × 3
## # Groups: symbol [4]
## symbol date adjusted
## <chr> <date> <dbl>
## 1 FB 2013-01-31 31.0
## 2 FB 2013-02-28 27.2
## 3 FB 2013-03-31 25.6
## 4 FB 2013-04-30 27.8
## 5 FB 2013-05-31 24.4
## 6 FB 2013-06-30 24.9
## 7 FB 2013-07-31 36.8
## 8 FB 2013-08-31 41.3
## 9 FB 2013-09-30 50.2
## 10 FB 2013-10-31 50.2
## # … with 182 more rows
ngas_price_usd <- tq_get("PNGASEUUSDM", get = "economic.data")
ngas_price_usd %>% tq_transmute(mutate_fun = to.period,
period = "months",
col_rename = "Natural Gas Price")
## # A tibble: 128 × 2
## date `Natural Gas Price`
## <date> <dbl>
## 1 2012-01-01 12.3
## 2 2012-02-01 12.2
## 3 2012-03-01 12.5
## 4 2012-04-01 12.6
## 5 2012-05-01 12.6
## 6 2012-06-01 12.6
## 7 2012-07-01 11.4
## 8 2012-08-01 11.4
## 9 2012-09-01 11.4
## 10 2012-10-01 11.6
## # … with 118 more rows
tq_mutate_fun_options()
## $zoo
## [1] "rollapply" "rollapplyr" "rollmax"
## [4] "rollmax.default" "rollmaxr" "rollmean"
## [7] "rollmean.default" "rollmeanr" "rollmedian"
## [10] "rollmedian.default" "rollmedianr" "rollsum"
## [13] "rollsum.default" "rollsumr"
##
## $xts
## [1] "apply.daily" "apply.monthly" "apply.quarterly" "apply.weekly"
## [5] "apply.yearly" "diff.xts" "lag.xts" "period.apply"
## [9] "period.max" "period.min" "period.prod" "period.sum"
## [13] "periodicity" "to_period" "to.daily" "to.hourly"
## [17] "to.minutes" "to.minutes10" "to.minutes15" "to.minutes3"
## [21] "to.minutes30" "to.minutes5" "to.monthly" "to.period"
## [25] "to.quarterly" "to.weekly" "to.yearly"
##
## $quantmod
## [1] "allReturns" "annualReturn" "ClCl" "dailyReturn"
## [5] "Delt" "HiCl" "Lag" "LoCl"
## [9] "LoHi" "monthlyReturn" "Next" "OpCl"
## [13] "OpHi" "OpLo" "OpOp" "periodReturn"
## [17] "quarterlyReturn" "seriesAccel" "seriesDecel" "seriesDecr"
## [21] "seriesHi" "seriesIncr" "seriesLo" "weeklyReturn"
## [25] "yearlyReturn"
##
## $TTR
## [1] "adjRatios" "ADX" "ALMA"
## [4] "aroon" "ATR" "BBands"
## [7] "CCI" "chaikinAD" "chaikinVolatility"
## [10] "CLV" "CMF" "CMO"
## [13] "CTI" "DEMA" "DonchianChannel"
## [16] "DPO" "DVI" "EMA"
## [19] "EMV" "EVWMA" "GMMA"
## [22] "growth" "HMA" "keltnerChannels"
## [25] "KST" "lags" "MACD"
## [28] "MFI" "momentum" "OBV"
## [31] "PBands" "ROC" "rollSFM"
## [34] "RSI" "runCor" "runCov"
## [37] "runMAD" "runMax" "runMean"
## [40] "runMedian" "runMin" "runPercentRank"
## [43] "runSD" "runSum" "runVar"
## [46] "SAR" "SMA" "SMI"
## [49] "SNR" "stoch" "TDI"
## [52] "TRIX" "ultimateOscillator" "VHF"
## [55] "VMA" "volatility" "VWAP"
## [58] "VWMA" "wilderSum" "williamsAD"
## [61] "WMA" "WPR" "ZigZag"
## [64] "ZLEMA"
##
## $PerformanceAnalytics
## [1] "Return.annualized" "Return.annualized.excess"
## [3] "Return.clean" "Return.cumulative"
## [5] "Return.excess" "Return.Geltner"
## [7] "zerofill"
#MACD is an option - Moving average convergence divergence (MACD, or MAC-D) is a trend-following momentum indicator that shows the relationship between two exponential moving averages (EMA's) of a security's price.
FANG %>%
group_by(symbol) %>%
tq_mutate(select = close,
mutate_fun = MACD,
col_rename = c("MACD", "Signal"))
## # A tibble: 4,032 × 10
## # Groups: symbol [4]
## symbol date open high low close volume adjusted MACD Signal
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 FB 2013-01-02 27.4 28.2 27.4 28 69846400 28 NA NA
## 2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600 27.8 NA NA
## 3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400 28.8 NA NA
## 4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800 29.4 NA NA
## 5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300 29.1 NA NA
## 6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700 30.6 NA NA
## 7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400 31.3 NA NA
## 8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000 31.7 NA NA
## 9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800 31.0 NA NA
## 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600 30.1 NA NA
## # … with 4,022 more rows
#Change FB to META because of updated ticker
meta_returns <- tq_get("META", get = "stock.prices", from = "2016-01-01", to = "2016-12-31") %>% tq_transmute(adjusted, periodReturn, period = "weekly", col_rename = "meta.returns")
xlk_returns <- tq_get("XLK", from = "2016-01-01", to = "2016-12-31") %>%
tq_transmute(adjusted, periodReturn, period = "weekly", col_rename = "xlk.returns")
returns_combined <- left_join(meta_returns, xlk_returns, by = "date")
returns_combined
## # A tibble: 52 × 3
## date meta.returns xlk.returns
## <date> <dbl> <dbl>
## 1 2016-01-08 -0.0478 -0.0516
## 2 2016-01-15 -0.0242 -0.0187
## 3 2016-01-22 0.0313 0.0264
## 4 2016-01-29 0.146 0.0213
## 5 2016-02-05 -0.0725 -0.0422
## 6 2016-02-12 -0.0198 -0.00582
## 7 2016-02-19 0.0251 0.0354
## 8 2016-02-26 0.0320 0.0148
## 9 2016-03-04 0.00436 0.0281
## 10 2016-03-11 0.00941 0.0106
## # … with 42 more rows
regress <- function(data) {
coef(lm(meta.returns ~ xlk.returns, data = timetk::tk_tbl(data, silent = TRUE)))
}
returns_combined %>%
tq_mutate(mutate_fun = rollapply,
width = 12,
FUN = regress,
by.column = FALSE,
col_rename = c("coef.0", "coef.1"))
## # A tibble: 52 × 5
## date meta.returns xlk.returns coef.0 coef.1
## <date> <dbl> <dbl> <dbl> <dbl>
## 1 2016-01-08 -0.0478 -0.0516 NA NA
## 2 2016-01-15 -0.0242 -0.0187 NA NA
## 3 2016-01-22 0.0313 0.0264 NA NA
## 4 2016-01-29 0.146 0.0213 NA NA
## 5 2016-02-05 -0.0725 -0.0422 NA NA
## 6 2016-02-12 -0.0198 -0.00582 NA NA
## 7 2016-02-19 0.0251 0.0354 NA NA
## 8 2016-02-26 0.0320 0.0148 NA NA
## 9 2016-03-04 0.00436 0.0281 NA NA
## 10 2016-03-11 0.00941 0.0106 NA NA
## # … with 42 more rows
print(returns_combined)
## # A tibble: 52 × 3
## date meta.returns xlk.returns
## <date> <dbl> <dbl>
## 1 2016-01-08 -0.0478 -0.0516
## 2 2016-01-15 -0.0242 -0.0187
## 3 2016-01-22 0.0313 0.0264
## 4 2016-01-29 0.146 0.0213
## 5 2016-02-05 -0.0725 -0.0422
## 6 2016-02-12 -0.0198 -0.00582
## 7 2016-02-19 0.0251 0.0354
## 8 2016-02-26 0.0320 0.0148
## 9 2016-03-04 0.00436 0.0281
## 10 2016-03-11 0.00941 0.0106
## # … with 42 more rows
#EVWMA (exponential volume-weighted moving average) requires two inputs, price and volume. To work with these columns, we can switch to the xy variants, tq_transmute_xy() and tq_mutate_xy().
FANG %>%
group_by(symbol) %>%
tq_mutate_xy(x = close, y = volume,
mutate_fun = EVWMA, col_rename = "EVWMA")
## # A tibble: 4,032 × 9
## # Groups: symbol [4]
## symbol date open high low close volume adjusted EVWMA
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 FB 2013-01-02 27.4 28.2 27.4 28 69846400 28 NA
## 2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600 27.8 NA
## 3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400 28.8 NA
## 4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800 29.4 NA
## 5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300 29.1 NA
## 6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700 30.6 NA
## 7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400 31.3 NA
## 8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000 31.7 NA
## 9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800 31.0 NA
## 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600 30.1 30.1
## # … with 4,022 more rows
https://business-science.github.io/tidyquant/articles/TQ02-quant-integrations-in-tidyquant.html
tq_transmute_fun_options() %>% str()
## List of 5
## $ zoo : chr [1:14] "rollapply" "rollapplyr" "rollmax" "rollmax.default" ...
## $ xts : chr [1:27] "apply.daily" "apply.monthly" "apply.quarterly" "apply.weekly" ...
## $ quantmod : chr [1:25] "allReturns" "annualReturn" "ClCl" "dailyReturn" ...
## $ TTR : chr [1:64] "adjRatios" "ADX" "ALMA" "aroon" ...
## $ PerformanceAnalytics: chr [1:7] "Return.annualized" "Return.annualized.excess" "Return.clean" "Return.cumulative" ...
# Get zoo functions that work with tq_transmute and tq_mutate
tq_transmute_fun_options()$zoo
## [1] "rollapply" "rollapplyr" "rollmax"
## [4] "rollmax.default" "rollmaxr" "rollmean"
## [7] "rollmean.default" "rollmeanr" "rollmedian"
## [10] "rollmedian.default" "rollmedianr" "rollsum"
## [13] "rollsum.default" "rollsumr"
# Get xts functions that work with tq_transmute and tq_mutate
tq_transmute_fun_options()$xts
## [1] "apply.daily" "apply.monthly" "apply.quarterly" "apply.weekly"
## [5] "apply.yearly" "diff.xts" "lag.xts" "period.apply"
## [9] "period.max" "period.min" "period.prod" "period.sum"
## [13] "periodicity" "to_period" "to.daily" "to.hourly"
## [17] "to.minutes" "to.minutes10" "to.minutes15" "to.minutes3"
## [21] "to.minutes30" "to.minutes5" "to.monthly" "to.period"
## [25] "to.quarterly" "to.weekly" "to.yearly"
# Get quantmod functions that work with tq_transmute and tq_mutate
tq_transmute_fun_options()$quantmod
## [1] "allReturns" "annualReturn" "ClCl" "dailyReturn"
## [5] "Delt" "HiCl" "Lag" "LoCl"
## [9] "LoHi" "monthlyReturn" "Next" "OpCl"
## [13] "OpHi" "OpLo" "OpOp" "periodReturn"
## [17] "quarterlyReturn" "seriesAccel" "seriesDecel" "seriesDecr"
## [21] "seriesHi" "seriesIncr" "seriesLo" "weeklyReturn"
## [25] "yearlyReturn"
# Get TTR functions that work with tq_transmute and tq_mutate
tq_transmute_fun_options()$TTR
## [1] "adjRatios" "ADX" "ALMA"
## [4] "aroon" "ATR" "BBands"
## [7] "CCI" "chaikinAD" "chaikinVolatility"
## [10] "CLV" "CMF" "CMO"
## [13] "CTI" "DEMA" "DonchianChannel"
## [16] "DPO" "DVI" "EMA"
## [19] "EMV" "EVWMA" "GMMA"
## [22] "growth" "HMA" "keltnerChannels"
## [25] "KST" "lags" "MACD"
## [28] "MFI" "momentum" "OBV"
## [31] "PBands" "ROC" "rollSFM"
## [34] "RSI" "runCor" "runCov"
## [37] "runMAD" "runMax" "runMean"
## [40] "runMedian" "runMin" "runPercentRank"
## [43] "runSD" "runSum" "runVar"
## [46] "SAR" "SMA" "SMI"
## [49] "SNR" "stoch" "TDI"
## [52] "TRIX" "ultimateOscillator" "VHF"
## [55] "VMA" "volatility" "VWAP"
## [58] "VWMA" "wilderSum" "williamsAD"
## [61] "WMA" "WPR" "ZigZag"
## [64] "ZLEMA"
# Get PerformanceAnalytics functions that work with tq_transmute and tq_mutate
tq_transmute_fun_options()$PerformanceAnalytics
## [1] "Return.annualized" "Return.annualized.excess"
## [3] "Return.clean" "Return.cumulative"
## [5] "Return.excess" "Return.Geltner"
## [7] "zerofill"
data("FANG")
FANG
## # A tibble: 4,032 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 FB 2013-01-02 27.4 28.2 27.4 28 69846400 28
## 2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600 27.8
## 3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400 28.8
## 4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800 29.4
## 5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300 29.1
## 6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700 30.6
## 7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400 31.3
## 8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000 31.7
## 9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800 31.0
## 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600 30.1
## # … with 4,022 more rows
FANG_annual_returns <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
type = "arithmetic")
FANG_annual_returns
## # A tibble: 16 × 3
## # Groups: symbol [4]
## symbol date yearly.returns
## <chr> <date> <dbl>
## 1 FB 2013-12-31 0.952
## 2 FB 2014-12-31 0.428
## 3 FB 2015-12-31 0.341
## 4 FB 2016-12-30 0.0993
## 5 AMZN 2013-12-31 0.550
## 6 AMZN 2014-12-31 -0.222
## 7 AMZN 2015-12-31 1.18
## 8 AMZN 2016-12-30 0.109
## 9 NFLX 2013-12-31 3.00
## 10 NFLX 2014-12-31 -0.0721
## 11 NFLX 2015-12-31 1.34
## 12 NFLX 2016-12-30 0.0824
## 13 GOOG 2013-12-31 0.550
## 14 GOOG 2014-12-31 -0.0597
## 15 GOOG 2015-12-31 0.442
## 16 GOOG 2016-12-30 0.0171
FANG_annual_returns %>%
ggplot(aes(x = date, y = yearly.returns, fill = symbol)) +
geom_col() +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
scale_y_continuous(labels = scales::percent) +
labs(title = "FANG: Annual Returns",
subtitle = "Get annual returns quickly with tq_transmute!",
y = "Annual Returns", x = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
theme_tq() +
scale_fill_tq()
FANG_daily_log_returns <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
type = "log",
col_rename = "monthly.returns")
FANG_daily_log_returns %>%
ggplot(aes(x = monthly.returns, fill = symbol)) +
geom_density(alpha = 0.5) +
labs(title = "FANG: Charting the Daily Log Returns",
x = "Monthly Returns", y = "Density") +
theme_tq() +
scale_fill_tq() +
facet_wrap(~ symbol, ncol = 2)
FANG %>%
group_by(symbol) %>%
tq_transmute(select = open:volume,
mutate_fun = to.period,
period = "months")
## # A tibble: 192 × 7
## # Groups: symbol [4]
## symbol date open high low close volume
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 FB 2013-01-31 29.2 31.5 28.7 31.0 190744900
## 2 FB 2013-02-28 26.8 27.3 26.3 27.2 83027800
## 3 FB 2013-03-28 26.1 26.2 25.5 25.6 28585700
## 4 FB 2013-04-30 27.1 27.8 27.0 27.8 36245700
## 5 FB 2013-05-31 24.6 25.0 24.3 24.4 35925000
## 6 FB 2013-06-28 24.7 25.0 24.4 24.9 96778900
## 7 FB 2013-07-31 38.0 38.3 36.3 36.8 154828700
## 8 FB 2013-08-30 42.0 42.3 41.1 41.3 67735100
## 9 FB 2013-09-30 50.1 51.6 49.8 50.2 100095000
## 10 FB 2013-10-31 47.2 52 46.5 50.2 248809000
## # … with 182 more rows
FANG_daily <- FANG %>%
group_by(symbol)
FANG_daily %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line(size = 1) +
labs(title = "Daily Stock Prices",
x = "", y = "Adjusted Prices", color = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
FANG_monthly <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = to.period,
period = "months")
FANG_monthly %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line(size = 1) +
labs(title = "Monthly Stock Prices",
x = "", y = "Adjusted Prices", color = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
# Asset Returns
FANG_returns_monthly <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly")
# Baseline Returns
baseline_returns_monthly <- "XLK" %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2016-12-31") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly")
returns_joined <- left_join(FANG_returns_monthly,
baseline_returns_monthly,
by = "date")
returns_joined
## # A tibble: 192 × 4
## # Groups: symbol [4]
## symbol date monthly.returns.x monthly.returns.y
## <chr> <date> <dbl> <dbl>
## 1 FB 2013-01-31 0.106 -0.0138
## 2 FB 2013-02-28 -0.120 0.00782
## 3 FB 2013-03-28 -0.0613 0.0258
## 4 FB 2013-04-30 0.0856 0.0175
## 5 FB 2013-05-31 -0.123 0.0279
## 6 FB 2013-06-28 0.0218 -0.0289
## 7 FB 2013-07-31 0.479 0.0373
## 8 FB 2013-08-30 0.122 -0.0104
## 9 FB 2013-09-30 0.217 0.0253
## 10 FB 2013-10-31 -0.000398 0.0502
## # … with 182 more rows
FANG_rolling_corr <- returns_joined %>%
tq_transmute_xy(x = monthly.returns.x,
y = monthly.returns.y,
mutate_fun = runCor,
n = 6,
col_rename = "rolling.corr.6")
FANG_rolling_corr %>%
ggplot(aes(x = date, y = rolling.corr.6, color = symbol)) +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
geom_line(size = 1) +
labs(title = "FANG: Six Month Rolling Correlation to XLK",
x = "", y = "Correlation", color = "") +
facet_wrap(~ symbol, ncol = 2) +
theme_tq() +
scale_color_tq()
## Warning: Removed 20 row(s) containing missing values (geom_path).
FANG_macd <- FANG %>%
group_by(symbol) %>%
tq_mutate(select = close,
mutate_fun = MACD,
nFast = 12,
nSlow = 26,
nSig = 9,
maType = SMA) %>%
mutate(diff = macd - signal) %>%
select(-(open:volume))
FANG_macd
## # A tibble: 4,032 × 6
## # Groups: symbol [4]
## symbol date adjusted macd signal diff
## <chr> <date> <dbl> <dbl> <dbl> <dbl>
## 1 FB 2013-01-02 28 NA NA NA
## 2 FB 2013-01-03 27.8 NA NA NA
## 3 FB 2013-01-04 28.8 NA NA NA
## 4 FB 2013-01-07 29.4 NA NA NA
## 5 FB 2013-01-08 29.1 NA NA NA
## 6 FB 2013-01-09 30.6 NA NA NA
## 7 FB 2013-01-10 31.3 NA NA NA
## 8 FB 2013-01-11 31.7 NA NA NA
## 9 FB 2013-01-14 31.0 NA NA NA
## 10 FB 2013-01-15 30.1 NA NA NA
## # … with 4,022 more rows
FANG_macd %>%
filter(date >= as_date("2016-10-01")) %>%
ggplot(aes(x = date)) +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
geom_line(aes(y = macd, col = symbol)) +
geom_line(aes(y = signal), color = "blue", linetype = 2) +
geom_bar(aes(y = diff), stat = "identity", color = palette_light()[[1]]) +
facet_wrap(~ symbol, ncol = 2, scale = "free_y") +
labs(title = "FANG: Moving Average Convergence Divergence",
y = "MACD", x = "", color = "") +
theme_tq() +
scale_color_tq()
FANG_max_by_qtr <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = apply.quarterly,
FUN = max,
col_rename = "max.close") %>%
mutate(year.qtr = paste0(year(date), "-Q", quarter(date))) %>%
select(-date)
FANG_max_by_qtr
## # A tibble: 64 × 3
## # Groups: symbol [4]
## symbol max.close year.qtr
## <chr> <dbl> <chr>
## 1 FB 32.5 2013-Q1
## 2 FB 29.0 2013-Q2
## 3 FB 51.2 2013-Q3
## 4 FB 58.0 2013-Q4
## 5 FB 72.0 2014-Q1
## 6 FB 67.6 2014-Q2
## 7 FB 79.0 2014-Q3
## 8 FB 81.4 2014-Q4
## 9 FB 85.3 2015-Q1
## 10 FB 88.9 2015-Q2
## # … with 54 more rows
FANG_min_by_qtr <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = apply.quarterly,
FUN = min,
col_rename = "min.close") %>%
mutate(year.qtr = paste0(year(date), "-Q", quarter(date))) %>%
select(-date)
FANG_by_qtr <- left_join(FANG_max_by_qtr, FANG_min_by_qtr,
by = c("symbol" = "symbol",
"year.qtr" = "year.qtr"))
FANG_by_qtr
## # A tibble: 64 × 4
## # Groups: symbol [4]
## symbol max.close year.qtr min.close
## <chr> <dbl> <chr> <dbl>
## 1 FB 32.5 2013-Q1 25.1
## 2 FB 29.0 2013-Q2 22.9
## 3 FB 51.2 2013-Q3 24.4
## 4 FB 58.0 2013-Q4 44.8
## 5 FB 72.0 2014-Q1 53.5
## 6 FB 67.6 2014-Q2 56.1
## 7 FB 79.0 2014-Q3 62.8
## 8 FB 81.4 2014-Q4 72.6
## 9 FB 85.3 2015-Q1 74.1
## 10 FB 88.9 2015-Q2 77.5
## # … with 54 more rows
FANG_by_qtr %>%
ggplot(aes(x = year.qtr, color = symbol)) +
geom_segment(aes(xend = year.qtr, y = min.close, yend = max.close),
size = 1) +
geom_point(aes(y = max.close), size = 2) +
geom_point(aes(y = min.close), size = 2) +
facet_wrap(~ symbol, ncol = 2, scale = "free_y") +
labs(title = "FANG: Min/Max Price By Quarter",
y = "Stock Price", color = "") +
theme_tq() +
scale_color_tq() +
scale_y_continuous(labels = scales::dollar) +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.title.x = element_blank())
# Get stock pairs
stock_prices <- c("MA", "V") %>%
tq_get(get = "stock.prices",
from = "2015-01-01",
to = "2016-12-31") %>%
group_by(symbol)
stock_pairs <- stock_prices %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
type = "log",
col_rename = "returns") %>%
spread(key = symbol, value = returns)
stock_pairs %>%
ggplot(aes(x = V, y = MA)) +
geom_point(color = palette_light()[[1]], alpha = 0.5) +
geom_smooth(method = "lm") +
labs(title = "Visualizing Returns Relationship of Stock Pairs") +
theme_tq()
## `geom_smooth()` using formula 'y ~ x'
lm(MA ~ V, data = stock_pairs) %>%
summary()
##
## Call:
## lm(formula = MA ~ V, data = stock_pairs)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.026957 -0.003966 0.000215 0.003965 0.028947
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0001130 0.0003097 0.365 0.715
## V 0.8133668 0.0226394 35.927 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.00695 on 502 degrees of freedom
## Multiple R-squared: 0.72, Adjusted R-squared: 0.7194
## F-statistic: 1291 on 1 and 502 DF, p-value: < 2.2e-16
regr_fun <- function(data) {
coef(lm(MA ~ V, data = timetk::tk_tbl(data, silent = TRUE)))
}
regr_fun
## function(data) {
## coef(lm(MA ~ V, data = timetk::tk_tbl(data, silent = TRUE)))
## }
##There is something wrong with the coef specification - need to fix this
#stock_pairs %>%
#ggplot(aes(x = date, y = coef.1)) +
#geom_line(size = 1, color = palette_light()[[1]]) +
#geom_hline(yintercept = 0.8134, size = 1, color = palette_light()[[2]]) +
#labs(title = "MA ~ V: Visualizing Rolling Regression Coefficient", x = "") +
#theme_tq
stock_prices %>%
tq_transmute(adjusted,
periodReturn,
period = "daily",
type = "log",
col_rename = "returns") %>%
mutate(wealth.index = 100 * cumprod(1 + returns)) %>%
ggplot(aes(x = date, y = wealth.index, color = symbol)) +
geom_line(size = 1) +
labs(title = "MA and V: Stock Prices") +
theme_tq() +
scale_color_tq()
FANG %>%
group_by(symbol) %>%
tq_transmute(adjusted, periodReturn, period = "daily") %>%
tq_transmute(daily.returns, Return.clean, alpha = 0.05) %>%
tq_transmute(daily.returns, Return.excess, Rf = 0.03 / 252)
## # A tibble: 4,032 × 3
## # Groups: symbol [4]
## symbol date `daily.returns > Rf`
## <chr> <date> <dbl>
## 1 FB 2013-01-02 -0.000119
## 2 FB 2013-01-03 -0.00833
## 3 FB 2013-01-04 0.0355
## 4 FB 2013-01-07 0.0228
## 5 FB 2013-01-08 -0.0124
## 6 FB 2013-01-09 0.0525
## 7 FB 2013-01-10 0.0231
## 8 FB 2013-01-11 0.0133
## 9 FB 2013-01-14 -0.0244
## 10 FB 2013-01-15 -0.0276
## # … with 4,022 more rows
#Scaling and Modeling
c("AAPL", "GOOG", "META") %>%
tq_get(get = "stock.prices", from = "2016-01-01", to = "2017-01-01")
## # A tibble: 756 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2016-01-04 25.7 26.3 25.5 26.3 270597600 24.2
## 2 AAPL 2016-01-05 26.4 26.5 25.6 25.7 223164000 23.5
## 3 AAPL 2016-01-06 25.1 25.6 25.0 25.2 273829600 23.1
## 4 AAPL 2016-01-07 24.7 25.0 24.1 24.1 324377600 22.1
## 5 AAPL 2016-01-08 24.6 24.8 24.2 24.2 283192000 22.2
## 6 AAPL 2016-01-11 24.7 24.8 24.3 24.6 198957600 22.6
## 7 AAPL 2016-01-12 25.1 25.2 24.7 25.0 196616800 22.9
## 8 AAPL 2016-01-13 25.1 25.3 24.3 24.3 249758400 22.3
## 9 AAPL 2016-01-14 24.5 25.1 23.9 24.9 252680400 22.8
## 10 AAPL 2016-01-15 24.0 24.4 23.8 24.3 319335600 22.3
## # … with 746 more rows
stock_list <- tibble(stocks = c("AAPL", "JPM", "CVX"),
industry = c("Technology", "Financial", "Energy"))
stock_list
## # A tibble: 3 × 2
## stocks industry
## <chr> <chr>
## 1 AAPL Technology
## 2 JPM Financial
## 3 CVX Energy
stock_list %>%
tq_get(get = "stock.prices", from = "2016-01-01", to = "2017-01-01")
## # A tibble: 756 × 9
## stocks industry date open high low close volume adjusted
## <chr> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL Technology 2016-01-04 25.7 26.3 25.5 26.3 270597600 24.2
## 2 AAPL Technology 2016-01-05 26.4 26.5 25.6 25.7 223164000 23.5
## 3 AAPL Technology 2016-01-06 25.1 25.6 25.0 25.2 273829600 23.1
## 4 AAPL Technology 2016-01-07 24.7 25.0 24.1 24.1 324377600 22.1
## 5 AAPL Technology 2016-01-08 24.6 24.8 24.2 24.2 283192000 22.2
## 6 AAPL Technology 2016-01-11 24.7 24.8 24.3 24.6 198957600 22.6
## 7 AAPL Technology 2016-01-12 25.1 25.2 24.7 25.0 196616800 22.9
## 8 AAPL Technology 2016-01-13 25.1 25.3 24.3 24.3 249758400 22.3
## 9 AAPL Technology 2016-01-14 24.5 25.1 23.9 24.9 252680400 22.8
## 10 AAPL Technology 2016-01-15 24.0 24.4 23.8 24.3 319335600 22.3
## # … with 746 more rows
tq_index("DOW")
## Getting holdings for DOW
## # A tibble: 30 × 8
## symbol company ident…¹ sedol weight sector share…² local…³
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 UNH UnitedHealth Group Incorp… 91324P… 2917… 0.113 Healt… 5744835 USD
## 2 GS Goldman Sachs Group Inc. 38141G… 2407… 0.0678 Finan… 5744835 USD
## 3 HD Home Depot Inc. 437076… 2434… 0.0616 Consu… 5744835 USD
## 4 AMGN Amgen Inc. 031162… 2023… 0.0544 Healt… 5744835 USD
## 5 MCD McDonald's Corporation 580135… 2550… 0.0538 Consu… 5744835 USD
## 6 MSFT Microsoft Corporation 594918… 2588… 0.0515 Infor… 5744835 USD
## 7 V Visa Inc. Class A 92826C… B2PZ… 0.0404 Infor… 5744835 USD
## 8 CAT Caterpillar Inc. 149123… 2180… 0.0396 Indus… 5744835 USD
## 9 HON Honeywell International I… 438516… 2020… 0.0388 Indus… 5744835 USD
## 10 TRV Travelers Companies Inc. 89417E… 2769… 0.0360 Finan… 5744835 USD
## # … with 20 more rows, and abbreviated variable names ¹identifier,
## # ²shares_held, ³local_currency
#tq_exchange("NYSE")
tq_index("DOW") %>%
slice(1:3) %>%
tq_get(get = "stock.prices")
## Getting holdings for DOW
## # A tibble: 8,151 × 15
## symbol company ident…¹ sedol weight sector share…² local…³ date open
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr> <date> <dbl>
## 1 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-03 51.2
## 2 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-04 51.3
## 3 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-05 51.9
## 4 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-06 52.8
## 5 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-09 52.6
## 6 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-10 53.0
## 7 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-11 52.5
## 8 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-12 53.2
## 9 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-13 52.7
## 10 UNH UnitedHe… 91324P… 2917… 0.113 Healt… 5744835 USD 2012-01-17 53.0
## # … with 8,141 more rows, 5 more variables: high <dbl>, low <dbl>, close <dbl>,
## # volume <dbl>, adjusted <dbl>, and abbreviated variable names ¹identifier,
## # ²shares_held, ³local_currency
data("FANG")
FANG
## # A tibble: 4,032 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 FB 2013-01-02 27.4 28.2 27.4 28 69846400 28
## 2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600 27.8
## 3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400 28.8
## 4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800 29.4
## 5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300 29.1
## 6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700 30.6
## 7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400 31.3
## 8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000 31.7
## 9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800 31.0
## 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600 30.1
## # … with 4,022 more rows
FANG_returns_yearly <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
col_rename = "yearly.returns")
AAPL <- tq_get("AAPL", from = "2007-01-01", to = "2016-12-31")
AAPL
## # A tibble: 2,518 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2007-01-03 3.08 3.09 2.92 2.99 1238319600 2.56
## 2 AAPL 2007-01-04 3.00 3.07 2.99 3.06 847260400 2.61
## 3 AAPL 2007-01-05 3.06 3.08 3.01 3.04 834741600 2.59
## 4 AAPL 2007-01-08 3.07 3.09 3.05 3.05 797106800 2.61
## 5 AAPL 2007-01-09 3.09 3.32 3.04 3.31 3349298400 2.82
## 6 AAPL 2007-01-10 3.38 3.49 3.34 3.46 2952880000 2.96
## 7 AAPL 2007-01-11 3.43 3.46 3.40 3.42 1440252800 2.92
## 8 AAPL 2007-01-12 3.38 3.40 3.33 3.38 1312690400 2.89
## 9 AAPL 2007-01-16 3.42 3.47 3.41 3.47 1244076400 2.96
## 10 AAPL 2007-01-17 3.48 3.49 3.39 3.39 1646260000 2.90
## # … with 2,508 more rows
get_annual_returns <- function(stock.returns) {
stock.returns %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
type = "log",
period = "yearly")
}
AAPL_annual_log_returns <- get_annual_returns(AAPL)
AAPL_annual_log_returns
## # A tibble: 10 × 2
## date yearly.returns
## <date> <dbl>
## 1 2007-12-31 0.860
## 2 2008-12-31 -0.842
## 3 2009-12-31 0.904
## 4 2010-12-31 0.426
## 5 2011-12-30 0.228
## 6 2012-12-31 0.282
## 7 2013-12-31 0.0776
## 8 2014-12-31 0.341
## 9 2015-12-31 -0.0306
## 10 2016-12-30 0.118
AAPL_annual_log_returns %>%
ggplot(aes(x = year(date), y = yearly.returns)) +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
geom_point(size = 2, color = palette_light()[[3]]) +
geom_line(size = 1, color = palette_light()[[3]]) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "AAPL: Visualizing Trends in Annual Returns",
x = "", y = "Annual Returns", color = "") +
theme_tq()
## `geom_smooth()` using formula 'y ~ x'
library(broom)
get_model <- function(stock_data) {
annual_returns <- get_annual_returns(stock_data)
mod <- lm(yearly.returns ~ year(date), data = annual_returns)
tidy(mod)
}
get_model(AAPL)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 58.9 113. 0.520 0.617
## 2 year(date) -0.0291 0.0562 -0.518 0.618
#Scale to Many Stocks
set.seed(10)
stocks_tbl <- tq_index("SP500") %>%
sample_n(5)
## Getting holdings for SP500
stocks_tbl
## # A tibble: 5 × 8
## symbol company ident…¹ sedol weight sector share…² local…³
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 BEN Franklin Resources Inc. 354613… 2350… 1.96e-4 Finan… 3015902 USD
## 2 EL Estee Lauder Companies In… 518439… 2320… 1.59e-3 Consu… 2557081 USD
## 3 NTRS Northern Trust Corporation 665859… 2648… 5.79e-4 Finan… 2284849 USD
## 4 FMC FMC Corporation 302491… 2328… 4.75e-4 Mater… 1393953 USD
## 5 JNPR Juniper Networks Inc. 48203R… 2431… 2.83e-4 Infor… 3563904 USD
## # … with abbreviated variable names ¹identifier, ²shares_held, ³local_currency
stocks_model_stats <- stocks_tbl %>%
select(symbol, company) %>%
tq_get(from = "2007-01-01", to = "2016-12-31") %>%
# Nest
group_by(symbol, company) %>%
nest() %>%
# Apply the get_model() function to the new "nested" data column
mutate(model = map(data, get_model)) %>%
# Unnest and collect slope
unnest(model) %>%
filter(term == "year(date)") %>%
arrange(desc(estimate)) %>%
select(-term)
stocks_model_stats
## # A tibble: 5 × 7
## # Groups: symbol, company [5]
## symbol company data estimate std.e…¹ stati…² p.value
## <chr> <chr> <list> <dbl> <dbl> <dbl> <dbl>
## 1 NTRS Northern Trust Corporation <tibble> 0.0276 0.0243 1.13 0.290
## 2 BEN Franklin Resources Inc. <tibble> 0.00223 0.0389 0.0573 0.956
## 3 JNPR Juniper Networks Inc. <tibble> -0.00368 0.0460 -0.0800 0.938
## 4 EL Estee Lauder Companies Inc. … <tibble> -0.0104 0.0300 -0.347 0.738
## 5 FMC FMC Corporation <tibble> -0.0219 0.0326 -0.672 0.520
## # … with abbreviated variable names ¹std.error, ²statistic
tq_get("XYZ", "stock.prices")
## Warning: x = 'XYZ', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "XYZ", env = <environment>, verbose = FALSE, : Unable to import "XYZ".
## XYZ download failed after two attempts. Error message:
## HTTP error 404.
## [1] NA
c("AAPL", "GOOG", "BAD APPLE") %>%
tq_get(get = "stock.prices", complete_cases = TRUE)
## Warning: x = 'BAD APPLE', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "BAD APPLE", env = <environment>, : Unable to import "BAD APPLE".
## BAD APPLE download failed after two attempts. Error message:
## HTTP error 400.
## Removing BAD APPLE.
## # A tibble: 5,434 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2012-01-03 14.6 14.7 14.6 14.7 302220800 12.5
## 2 AAPL 2012-01-04 14.6 14.8 14.6 14.8 260022000 12.6
## 3 AAPL 2012-01-05 14.8 14.9 14.7 14.9 271269600 12.7
## 4 AAPL 2012-01-06 15.0 15.1 15.0 15.1 318292800 12.9
## 5 AAPL 2012-01-09 15.2 15.3 15.0 15.1 394024400 12.9
## 6 AAPL 2012-01-10 15.2 15.2 15.1 15.1 258196400 12.9
## 7 AAPL 2012-01-11 15.1 15.1 15.0 15.1 215084800 12.9
## 8 AAPL 2012-01-12 15.1 15.1 15.0 15.0 212587200 12.8
## 9 AAPL 2012-01-13 15.0 15.0 15.0 15.0 226021600 12.8
## 10 AAPL 2012-01-17 15.2 15.2 15.1 15.2 242897200 13.0
## # … with 5,424 more rows
c("AAPL", "GOOG", "BAD APPLE") %>%
tq_get(get = "stock.prices", complete_cases = FALSE)
## Warning: x = 'BAD APPLE', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "BAD APPLE", env = <environment>, : Unable to import "BAD APPLE".
## BAD APPLE download failed after two attempts. Error message:
## HTTP error 400.
## # A tibble: 5,435 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2012-01-03 14.6 14.7 14.6 14.7 302220800 12.5
## 2 AAPL 2012-01-04 14.6 14.8 14.6 14.8 260022000 12.6
## 3 AAPL 2012-01-05 14.8 14.9 14.7 14.9 271269600 12.7
## 4 AAPL 2012-01-06 15.0 15.1 15.0 15.1 318292800 12.9
## 5 AAPL 2012-01-09 15.2 15.3 15.0 15.1 394024400 12.9
## 6 AAPL 2012-01-10 15.2 15.2 15.1 15.1 258196400 12.9
## 7 AAPL 2012-01-11 15.1 15.1 15.0 15.1 215084800 12.9
## 8 AAPL 2012-01-12 15.1 15.1 15.0 15.0 212587200 12.8
## 9 AAPL 2012-01-13 15.0 15.0 15.0 15.0 226021600 12.8
## 10 AAPL 2012-01-17 15.2 15.2 15.1 15.2 242897200 13.0
## # … with 5,425 more rows
#Charting
# Loads tidyquant, lubridate, xts, quantmod, TTR, and PerformanceAnalytics
library(tidyverse)
library(tidyquant)
# Use FANG data set
data("FANG")
# Get AAPL and AMZN Stock Prices
AAPL <- tq_get("AAPL", get = "stock.prices", from = "2015-09-01", to = "2016-12-31")
AMZN <- tq_get("AMZN", get = "stock.prices", from = "2000-01-01", to = "2016-12-31")
end <- as_date("2016-12-31")
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_line() +
labs(title = "AAPL Line Chart", y = "Closing Price", x = "") +
theme_tq()
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close)) +
labs(title = "AAPL Bar Chart", y = "Closing Price", x = "") +
theme_tq()
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close)) +
labs(title = "AAPL Bar Chart",
subtitle = "Zoomed in using coord_x_date",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(end - weeks(6), end),
ylim = c(100, 120)) +
theme_tq()
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close),
colour_up = "darkgreen", colour_down = "darkred", size = 1) +
labs(title = "AAPL Bar Chart",
subtitle = "Zoomed in, Experimenting with Formatting",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(end - weeks(6), end),
ylim = c(100, 120)) +
theme_tq()
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
labs(title = "AAPL Candlestick Chart", y = "Closing Price", x = "") +
theme_tq()
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
labs(title = "AAPL Candlestick Chart",
subtitle = "Zoomed in using coord_x_date",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(end - weeks(6), end),
ylim = c(100, 120)) +
theme_tq()
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close),
colour_up = "darkgreen", colour_down = "darkred",
fill_up = "darkgreen", fill_down = "darkred") +
labs(title = "AAPL Candlestick Chart",
subtitle = "Zoomed in, Experimenting with Formatting",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(end - weeks(6), end),
ylim = c(100, 120)) +
theme_tq()
##Charting Multiple Secutiries at Once
start <- end - weeks(6)
FANG %>%
filter(date >= start - days(2 * 15)) %>%
ggplot(aes(x = date, y = close, group = symbol)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
labs(title = "FANG Candlestick Chart",
subtitle = "Experimenting with Mulitple Stocks",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2, scale = "free_y") +
theme_tq()
start <- end - weeks(6)
FANG %>%
filter(date >= start - days(2 * 15)) %>%
ggplot(aes(x = date, y = close, group = symbol)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
geom_ma(ma_fun = SMA, n = 15, color = "darkblue", size = 1) +
labs(title = "FANG Candlestick Chart",
subtitle = "Experimenting with Mulitple Stocks",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2, scale = "free_y") +
theme_tq()
Example 1
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
geom_ma(ma_fun = SMA, n = 50, linetype = 5, size = 1.25) +
geom_ma(ma_fun = SMA, n = 200, color = "red", size = 1.25) +
labs(title = "AAPL Candlestick Chart",
subtitle = "50 and 200-Day SMA",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(end - weeks(24), end),
ylim = c(100, 120)) +
theme_tq()
Example 2
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close)) +
geom_ma(ma_fun = EMA, n = 50, wilder = TRUE, linetype = 5, size = 1.25) +
geom_ma(ma_fun = EMA, n = 200, wilder = TRUE, color = "red", size = 1.25) +
labs(title = "AAPL Bar Chart",
subtitle = "50 and 200-Day EMA",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(end - weeks(24), end),
ylim = c(100, 120)) +
theme_tq()
Example 3
start <- end - weeks(6)
FANG %>%
filter(date >= start - days(2 * 50)) %>%
ggplot(aes(x = date, y = close, volume = volume, group = symbol)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
geom_ma(ma_fun = VWMA, n = 15, wilder = TRUE, linetype = 5) +
geom_ma(ma_fun = VWMA, n = 50, wilder = TRUE, color = "red") +
labs(title = "FANG Bar Chart",
subtitle = "50 and 200-Day EMA, Experimenting with Multiple Stocks",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
theme_tq()
##Bollinger Bands Example 1
AAPL %>%
ggplot(aes(x = date, y = close, open = open,
high = high, low = low, close = close)) +
geom_candlestick() +
geom_bbands(ma_fun = SMA, sd = 2, n = 20) +
labs(title = "AAPL Candlestick Chart",
subtitle = "BBands with SMA Applied",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(end - weeks(24), end),
ylim = c(100, 120)) +
theme_tq()
Example 2
AAPL %>%
ggplot(aes(x = date, y = close, open = open,
high = high, low = low, close = close)) +
geom_candlestick() +
geom_bbands(ma_fun = SMA, sd = 2, n = 20) +
labs(title = "AAPL Candlestick Chart",
subtitle = "BBands with SMA Applied",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(end - weeks(24), end),
ylim = c(100, 120)) +
theme_tq()
Example 3
start <- end - weeks(24)
FANG %>%
filter(date >= start - days(2 * 20)) %>%
ggplot(aes(x = date, y = close,
open = open, high = high, low = low, close = close,
group = symbol)) +
geom_barchart() +
geom_bbands(ma_fun = SMA, sd = 2, n = 20, linetype = 5) +
labs(title = "FANG Bar Chart",
subtitle = "BBands with SMA Applied, Experimenting with Multiple Stocks",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
theme_tq()
##ggplot2
Example 1
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line(color = palette_light()[[1]]) +
scale_y_continuous() +
labs(title = "AMZN Line Chart",
subtitle = "Continuous Scale",
y = "Closing Price", x = "") +
theme_tq()
##Log Scale
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line(color = palette_light()[[1]]) +
scale_y_log10() +
labs(title = "AMZN Line Chart",
subtitle = "Log Scale",
y = "Closing Price", x = "") +
theme_tq()
Example 2
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line(color = palette_light()[[1]]) +
scale_y_log10() +
labs(title = "AMZN Line Chart",
subtitle = "Log Scale",
y = "Closing Price", x = "") +
theme_tq()
Example 3
AMZN %>%
ggplot(aes(x = date, y = volume)) +
geom_segment(aes(xend = date, yend = 0, color = volume)) +
geom_smooth(method = "loess", se = FALSE) +
labs(title = "AMZN Volume Chart",
subtitle = "Charting Daily Volume",
y = "Volume", x = "") +
theme_tq() +
theme(legend.position = "none")
## `geom_smooth()` using formula 'y ~ x'
start <- end - weeks(24)
AMZN %>%
filter(date >= start - days(50)) %>%
ggplot(aes(x = date, y = volume)) +
geom_segment(aes(xend = date, yend = 0, color = volume)) +
geom_smooth(method = "loess", se = FALSE) +
labs(title = "AMZN Bar Chart",
subtitle = "Charting Daily Volume, Zooming In",
y = "Volume", x = "") +
coord_x_date(xlim = c(start, end)) +
scale_color_gradient(low = "red", high = "darkblue") +
theme_tq() +
theme(legend.position = "none")
## `geom_smooth()` using formula 'y ~ x'
##Dark Mode
start <- end - weeks(24)
AMZN %>%
filter(date >= start - days(50)) %>%
ggplot(aes(x = date, y = volume)) +
geom_segment(aes(xend = date, yend = 0, color = volume)) +
geom_smooth(method = "loess", se = FALSE) +
labs(title = "AMZN Bar Chart",
subtitle = "Charting Daily Volume, Zooming In",
y = "Volume", x = "") +
coord_x_date(xlim = c(start, end)) +
scale_color_gradient(low = "red", high = "darkblue") +
theme_tq() +
theme(legend.position = "none")
## `geom_smooth()` using formula 'y ~ x'
#Performance Analysis
Ra <- c("AAPL", "GOOG", "NFLX") %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
Ra
## # A tibble: 216 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 AAPL 2010-01-29 -0.103
## 2 AAPL 2010-02-26 0.0654
## 3 AAPL 2010-03-31 0.148
## 4 AAPL 2010-04-30 0.111
## 5 AAPL 2010-05-28 -0.0161
## 6 AAPL 2010-06-30 -0.0208
## 7 AAPL 2010-07-30 0.0227
## 8 AAPL 2010-08-31 -0.0550
## 9 AAPL 2010-09-30 0.167
## 10 AAPL 2010-10-29 0.0607
## # … with 206 more rows
Rb <- "XLK" %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
Rb
## # A tibble: 72 × 2
## date Rb
## <date> <dbl>
## 1 2010-01-29 -0.0993
## 2 2010-02-26 0.0348
## 3 2010-03-31 0.0684
## 4 2010-04-30 0.0126
## 5 2010-05-28 -0.0748
## 6 2010-06-30 -0.0540
## 7 2010-07-30 0.0745
## 8 2010-08-31 -0.0561
## 9 2010-09-30 0.117
## 10 2010-10-29 0.0578
## # … with 62 more rows
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 216 × 4
## # Groups: symbol [3]
## symbol date Ra Rb
## <chr> <date> <dbl> <dbl>
## 1 AAPL 2010-01-29 -0.103 -0.0993
## 2 AAPL 2010-02-26 0.0654 0.0348
## 3 AAPL 2010-03-31 0.148 0.0684
## 4 AAPL 2010-04-30 0.111 0.0126
## 5 AAPL 2010-05-28 -0.0161 -0.0748
## 6 AAPL 2010-06-30 -0.0208 -0.0540
## 7 AAPL 2010-07-30 0.0227 0.0745
## 8 AAPL 2010-08-31 -0.0550 -0.0561
## 9 AAPL 2010-09-30 0.167 0.117
## 10 AAPL 2010-10-29 0.0607 0.0578
## # … with 206 more rows
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 13
## # Groups: symbol [3]
## symbol ActivePr…¹ Alpha Annua…² Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 0.119 0.0089 0.112 1.11 0.578 1.04 0.659 0 0.628
## 2 GOOG 0.034 0.0028 0.034 1.14 1.39 1.16 0.644 0 0.168
## 3 NFLX 0.447 0.053 0.859 0.384 -1.52 0.0045 0.0817 0.495 0.637
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## # TreynorRatio <dbl>, and abbreviated variable names ¹ActivePremium,
## # ²AnnualizedAlpha, ³Correlation, ⁴`Correlationp-value`, ⁵InformationRatio
RaRb_capm %>% select(symbol, Alpha, Beta)
## # A tibble: 3 × 3
## # Groups: symbol [3]
## symbol Alpha Beta
## <chr> <dbl> <dbl>
## 1 AAPL 0.0089 1.11
## 2 GOOG 0.0028 1.14
## 3 NFLX 0.053 0.384
##Individual Analysis
args(SharpeRatio)
## function (R, Rf = 0, p = 0.95, FUN = c("StdDev", "VaR", "ES"),
## weights = NULL, annualize = FALSE, SE = FALSE, SE.control = NULL,
## ...)
## NULL
##Stock Prices
stock_prices <- c("AAPL", "GOOG", "NFLX") %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31")
stock_prices
## # A tibble: 4,527 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2010-01-04 7.62 7.66 7.58 7.64 493729600 6.53
## 2 AAPL 2010-01-05 7.66 7.70 7.62 7.66 601904800 6.54
## 3 AAPL 2010-01-06 7.66 7.69 7.53 7.53 552160000 6.43
## 4 AAPL 2010-01-07 7.56 7.57 7.47 7.52 477131200 6.42
## 5 AAPL 2010-01-08 7.51 7.57 7.47 7.57 447610800 6.46
## 6 AAPL 2010-01-11 7.6 7.61 7.44 7.50 462229600 6.41
## 7 AAPL 2010-01-12 7.47 7.49 7.37 7.42 594459600 6.33
## 8 AAPL 2010-01-13 7.42 7.53 7.29 7.52 605892000 6.42
## 9 AAPL 2010-01-14 7.50 7.52 7.46 7.48 432894000 6.39
## 10 AAPL 2010-01-15 7.53 7.56 7.35 7.35 594067600 6.28
## # … with 4,517 more rows
##Returns
stock_returns_monthly <- stock_prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
stock_returns_monthly
## # A tibble: 216 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 AAPL 2010-01-29 -0.103
## 2 AAPL 2010-02-26 0.0654
## 3 AAPL 2010-03-31 0.148
## 4 AAPL 2010-04-30 0.111
## 5 AAPL 2010-05-28 -0.0161
## 6 AAPL 2010-06-30 -0.0208
## 7 AAPL 2010-07-30 0.0227
## 8 AAPL 2010-08-31 -0.0550
## 9 AAPL 2010-09-30 0.167
## 10 AAPL 2010-10-29 0.0607
## # … with 206 more rows
stock_returns_monthly %>%
tq_performance(
Ra = Ra,
Rb = NULL,
performance_fun = SharpeRatio
)
## # A tibble: 3 × 4
## # Groups: symbol [3]
## symbol `ESSharpe(Rf=0%,p=95%)` `StdDevSharpe(Rf=0%,p=95%)` VaRSharpe(Rf=0%,p…¹
## <chr> <dbl> <dbl> <dbl>
## 1 AAPL 0.173 0.292 0.218
## 2 GOOG 0.129 0.203 0.157
## 3 NFLX 0.237 0.284 0.272
## # … with abbreviated variable name ¹`VaRSharpe(Rf=0%,p=95%)`
stock_returns_monthly %>%
tq_performance(
Ra = Ra,
Rb = NULL,
performance_fun = SharpeRatio,
Rf = 0.03 / 12,
p = 0.99
)
## # A tibble: 3 × 4
## # Groups: symbol [3]
## symbol `ESSharpe(Rf=0.2%,p=99%)` `StdDevSharpe(Rf=0.2%,p=99%)` VaRSharpe(Rf=…¹
## <chr> <dbl> <dbl> <dbl>
## 1 AAPL 0.116 0.258 0.134
## 2 GOOG 0.0826 0.170 0.0998
## 3 NFLX 0.115 0.272 0.142
## # … with abbreviated variable name ¹`VaRSharpe(Rf=0.2%,p=99%)`
##Portfolios
stock_returns_monthly <- c("AAPL", "GOOG", "NFLX") %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
stock_returns_monthly
## # A tibble: 216 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 AAPL 2010-01-29 -0.103
## 2 AAPL 2010-02-26 0.0654
## 3 AAPL 2010-03-31 0.148
## 4 AAPL 2010-04-30 0.111
## 5 AAPL 2010-05-28 -0.0161
## 6 AAPL 2010-06-30 -0.0208
## 7 AAPL 2010-07-30 0.0227
## 8 AAPL 2010-08-31 -0.0550
## 9 AAPL 2010-09-30 0.167
## 10 AAPL 2010-10-29 0.0607
## # … with 206 more rows
baseline_returns_monthly <- "XLK" %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 72 × 2
## date Rb
## <date> <dbl>
## 1 2010-01-29 -0.0993
## 2 2010-02-26 0.0348
## 3 2010-03-31 0.0684
## 4 2010-04-30 0.0126
## 5 2010-05-28 -0.0748
## 6 2010-06-30 -0.0540
## 7 2010-07-30 0.0745
## 8 2010-08-31 -0.0561
## 9 2010-09-30 0.117
## 10 2010-10-29 0.0578
## # … with 62 more rows
baseline_returns_monthly <- "XLK" %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 72 × 2
## date Rb
## <date> <dbl>
## 1 2010-01-29 -0.0993
## 2 2010-02-26 0.0348
## 3 2010-03-31 0.0684
## 4 2010-04-30 0.0126
## 5 2010-05-28 -0.0748
## 6 2010-06-30 -0.0540
## 7 2010-07-30 0.0745
## 8 2010-08-31 -0.0561
## 9 2010-09-30 0.117
## 10 2010-10-29 0.0578
## # … with 62 more rows
wts <- c(0.5, 0.0, 0.5)
portfolio_returns_monthly <- stock_returns_monthly %>%
tq_portfolio(assets_col = symbol,
returns_col = Ra,
weights = wts,
col_rename = "Ra")
## Warning: `spread_()` was deprecated in tidyr 1.2.0.
## Please use `spread()` instead.
portfolio_returns_monthly
## # A tibble: 72 × 2
## date Ra
## <date> <dbl>
## 1 2010-01-29 0.0307
## 2 2010-02-26 0.0629
## 3 2010-03-31 0.130
## 4 2010-04-30 0.239
## 5 2010-05-28 0.0682
## 6 2010-06-30 -0.0219
## 7 2010-07-30 -0.0272
## 8 2010-08-31 0.116
## 9 2010-09-30 0.251
## 10 2010-10-29 0.0674
## # … with 62 more rows
wts_map <- tibble(
symbols = c("AAPL", "NFLX"),
weights = c(0.5, 0.5)
)
wts_map
## # A tibble: 2 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.5
## 2 NFLX 0.5
stock_returns_monthly %>%
tq_portfolio(assets_col = symbol,
returns_col = Ra,
weights = wts_map,
col_rename = "Ra_using_wts_map")
## # A tibble: 72 × 2
## date Ra_using_wts_map
## <date> <dbl>
## 1 2010-01-29 0.0307
## 2 2010-02-26 0.0629
## 3 2010-03-31 0.130
## 4 2010-04-30 0.239
## 5 2010-05-28 0.0682
## 6 2010-06-30 -0.0219
## 7 2010-07-30 -0.0272
## 8 2010-08-31 0.116
## 9 2010-09-30 0.251
## 10 2010-10-29 0.0674
## # … with 62 more rows
RaRb_single_portfolio <- left_join(portfolio_returns_monthly,
baseline_returns_monthly,
by = "date")
RaRb_single_portfolio
## # A tibble: 72 × 3
## date Ra Rb
## <date> <dbl> <dbl>
## 1 2010-01-29 0.0307 -0.0993
## 2 2010-02-26 0.0629 0.0348
## 3 2010-03-31 0.130 0.0684
## 4 2010-04-30 0.239 0.0126
## 5 2010-05-28 0.0682 -0.0748
## 6 2010-06-30 -0.0219 -0.0540
## 7 2010-07-30 -0.0272 0.0745
## 8 2010-08-31 0.116 -0.0561
## 9 2010-09-30 0.251 0.117
## 10 2010-10-29 0.0674 0.0578
## # … with 62 more rows
RaRb_single_portfolio <- left_join(portfolio_returns_monthly,
baseline_returns_monthly,
by = "date")
RaRb_single_portfolio
## # A tibble: 72 × 3
## date Ra Rb
## <date> <dbl> <dbl>
## 1 2010-01-29 0.0307 -0.0993
## 2 2010-02-26 0.0629 0.0348
## 3 2010-03-31 0.130 0.0684
## 4 2010-04-30 0.239 0.0126
## 5 2010-05-28 0.0682 -0.0748
## 6 2010-06-30 -0.0219 -0.0540
## 7 2010-07-30 -0.0272 0.0745
## 8 2010-08-31 0.116 -0.0561
## 9 2010-09-30 0.251 0.117
## 10 2010-10-29 0.0674 0.0578
## # … with 62 more rows
stock_returns_monthly <- c("AAPL", "GOOG", "NFLX") %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
baseline_returns_monthly <- "XLK" %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
stock_returns_monthly_multi <- stock_returns_monthly %>%
tq_repeat_df(n = 3)
## Ungrouping data frame groups: symbol
stock_returns_monthly_multi
## # A tibble: 648 × 4
## # Groups: portfolio [3]
## portfolio symbol date Ra
## <int> <chr> <date> <dbl>
## 1 1 AAPL 2010-01-29 -0.103
## 2 1 AAPL 2010-02-26 0.0654
## 3 1 AAPL 2010-03-31 0.148
## 4 1 AAPL 2010-04-30 0.111
## 5 1 AAPL 2010-05-28 -0.0161
## 6 1 AAPL 2010-06-30 -0.0208
## 7 1 AAPL 2010-07-30 0.0227
## 8 1 AAPL 2010-08-31 -0.0550
## 9 1 AAPL 2010-09-30 0.167
## 10 1 AAPL 2010-10-29 0.0607
## # … with 638 more rows
weights <- c(
0.50, 0.25, 0.25,
0.25, 0.50, 0.25,
0.25, 0.25, 0.50
)
stocks <- c("AAPL", "GOOG", "NFLX")
weights_table <- tibble(stocks) %>%
tq_repeat_df(n = 3) %>%
bind_cols(tibble(weights)) %>%
group_by(portfolio)
weights_table
## # A tibble: 9 × 3
## # Groups: portfolio [3]
## portfolio stocks weights
## <int> <chr> <dbl>
## 1 1 AAPL 0.5
## 2 1 GOOG 0.25
## 3 1 NFLX 0.25
## 4 2 AAPL 0.25
## 5 2 GOOG 0.5
## 6 2 NFLX 0.25
## 7 3 AAPL 0.25
## 8 3 GOOG 0.25
## 9 3 NFLX 0.5
portfolio_returns_monthly_multi <- stock_returns_monthly_multi %>%
tq_portfolio(assets_col = symbol,
returns_col = Ra,
weights = weights_table,
col_rename = "Ra")
portfolio_returns_monthly_multi
## # A tibble: 216 × 3
## # Groups: portfolio [3]
## portfolio date Ra
## <int> <date> <dbl>
## 1 1 2010-01-29 -0.0489
## 2 1 2010-02-26 0.0482
## 3 1 2010-03-31 0.123
## 4 1 2010-04-30 0.145
## 5 1 2010-05-28 0.0245
## 6 1 2010-06-30 -0.0308
## 7 1 2010-07-30 0.000600
## 8 1 2010-08-31 0.0474
## 9 1 2010-09-30 0.222
## 10 1 2010-10-29 0.0789
## # … with 206 more rows
RaRb_multiple_portfolio <- left_join(portfolio_returns_monthly_multi,
baseline_returns_monthly,
by = "date")
RaRb_multiple_portfolio
## # A tibble: 216 × 4
## # Groups: portfolio [3]
## portfolio date Ra Rb
## <int> <date> <dbl> <dbl>
## 1 1 2010-01-29 -0.0489 -0.0993
## 2 1 2010-02-26 0.0482 0.0348
## 3 1 2010-03-31 0.123 0.0684
## 4 1 2010-04-30 0.145 0.0126
## 5 1 2010-05-28 0.0245 -0.0748
## 6 1 2010-06-30 -0.0308 -0.0540
## 7 1 2010-07-30 0.000600 0.0745
## 8 1 2010-08-31 0.0474 -0.0561
## 9 1 2010-09-30 0.222 0.117
## 10 1 2010-10-29 0.0789 0.0578
## # … with 206 more rows
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM)
## # A tibble: 3 × 13
## # Groups: portfolio [3]
## portfolio Activ…¹ Alpha Annua…² Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.231 0.0193 0.258 0.908 0.312 0.741 0.472 0 0.917
## 2 2 0.219 0.0192 0.256 0.886 0.436 0.660 0.438 0.0001 0.809
## 3 3 0.319 0.0308 0.439 0.721 -0.179 0.394 0.252 0.0325 0.774
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## # TreynorRatio <dbl>, and abbreviated variable names ¹ActivePremium,
## # ²AnnualizedAlpha, ³Correlation, ⁴`Correlationp-value`, ⁵InformationRatio
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio `ESSharpe(Rf=0%,p=95%)` `StdDevSharpe(Rf=0%,p=95%)` VaRSharpe(Rf=0…¹
## <int> <dbl> <dbl> <dbl>
## 1 1 0.172 0.355 0.263
## 2 2 0.146 0.334 0.236
## 3 3 0.150 0.317 0.238
## # … with abbreviated variable name ¹`VaRSharpe(Rf=0%,p=95%)`
##Available Functions
tq_performance_fun_options()
## $table.funs
## [1] "table.AnnualizedReturns" "table.Arbitrary"
## [3] "table.Autocorrelation" "table.CAPM"
## [5] "table.CaptureRatios" "table.Correlation"
## [7] "table.Distributions" "table.DownsideRisk"
## [9] "table.DownsideRiskRatio" "table.DrawdownsRatio"
## [11] "table.HigherMoments" "table.InformationRatio"
## [13] "table.RollingPeriods" "table.SFM"
## [15] "table.SpecificRisk" "table.Stats"
## [17] "table.TrailingPeriods" "table.UpDownRatios"
## [19] "table.Variability"
##
## $CAPM.funs
## [1] "CAPM.alpha" "CAPM.beta" "CAPM.beta.bear" "CAPM.beta.bull"
## [5] "CAPM.CML" "CAPM.CML.slope" "CAPM.dynamic" "CAPM.epsilon"
## [9] "CAPM.jensenAlpha" "CAPM.RiskPremium" "CAPM.SML.slope" "TimingRatio"
## [13] "MarketTiming"
##
## $SFM.funs
## [1] "SFM.alpha" "SFM.beta" "SFM.CML" "SFM.CML.slope"
## [5] "SFM.dynamic" "SFM.epsilon" "SFM.jensenAlpha"
##
## $descriptive.funs
## [1] "mean" "sd" "min" "max"
## [5] "cor" "mean.geometric" "mean.stderr" "mean.LCL"
## [9] "mean.UCL"
##
## $annualized.funs
## [1] "Return.annualized" "Return.annualized.excess"
## [3] "sd.annualized" "SharpeRatio.annualized"
##
## $VaR.funs
## [1] "VaR" "ES" "ETL" "CDD" "CVaR"
##
## $moment.funs
## [1] "var" "cov" "skewness" "kurtosis"
## [5] "CoVariance" "CoSkewness" "CoSkewnessMatrix" "CoKurtosis"
## [9] "CoKurtosisMatrix" "M3.MM" "M4.MM" "BetaCoVariance"
## [13] "BetaCoSkewness" "BetaCoKurtosis"
##
## $drawdown.funs
## [1] "AverageDrawdown" "AverageLength" "AverageRecovery"
## [4] "DrawdownDeviation" "DrawdownPeak" "maxDrawdown"
##
## $Bacon.risk.funs
## [1] "MeanAbsoluteDeviation" "Frequency" "SharpeRatio"
## [4] "MSquared" "MSquaredExcess" "HurstIndex"
##
## $Bacon.regression.funs
## [1] "CAPM.alpha" "CAPM.beta" "CAPM.epsilon" "CAPM.jensenAlpha"
## [5] "SystematicRisk" "SpecificRisk" "TotalRisk" "TreynorRatio"
## [9] "AppraisalRatio" "FamaBeta" "Selectivity" "NetSelectivity"
##
## $Bacon.relative.risk.funs
## [1] "ActivePremium" "ActiveReturn" "TrackingError" "InformationRatio"
##
## $Bacon.drawdown.funs
## [1] "PainIndex" "PainRatio" "CalmarRatio" "SterlingRatio"
## [5] "BurkeRatio" "MartinRatio" "UlcerIndex"
##
## $Bacon.downside.risk.funs
## [1] "DownsideDeviation" "DownsidePotential" "DownsideFrequency"
## [4] "SemiDeviation" "SemiVariance" "UpsideRisk"
## [7] "UpsidePotentialRatio" "UpsideFrequency" "BernardoLedoitRatio"
## [10] "DRatio" "Omega" "OmegaSharpeRatio"
## [13] "OmegaExcessReturn" "SortinoRatio" "M2Sortino"
## [16] "Kappa" "VolatilitySkewness" "AdjustedSharpeRatio"
## [19] "SkewnessKurtosisRatio" "ProspectRatio"
##
## $misc.funs
## [1] "KellyRatio" "Modigliani" "UpDownRatios"
##Table Stats
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = table.Stats)
## # A tibble: 3 × 17
## # Groups: portfolio [3]
## portfolio Arith…¹ Geome…² Kurto…³ LCLMe…⁴ Maximum Median Minimum NAs Obser…⁵
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.0293 0.0259 1.14 0.0099 0.222 0.0307 -0.266 0 72
## 2 2 0.029 0.0252 1.65 0.0086 0.227 0.037 -0.3 0 72
## 3 3 0.0388 0.0313 1.81 0.01 0.370 0.046 -0.403 0 72
## # … with 7 more variables: Quartile1 <dbl>, Quartile3 <dbl>, SEMean <dbl>,
## # Skewness <dbl>, Stdev <dbl>, `UCLMean(0.95)` <dbl>, Variance <dbl>, and
## # abbreviated variable names ¹ArithmeticMean, ²GeometricMean, ³Kurtosis,
## # ⁴`LCLMean(0.95)`, ⁵Observations
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM)
## # A tibble: 3 × 13
## # Groups: portfolio [3]
## portfolio Activ…¹ Alpha Annua…² Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.231 0.0193 0.258 0.908 0.312 0.741 0.472 0 0.917
## 2 2 0.219 0.0192 0.256 0.886 0.436 0.660 0.438 0.0001 0.809
## 3 3 0.319 0.0308 0.439 0.721 -0.179 0.394 0.252 0.0325 0.774
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## # TreynorRatio <dbl>, and abbreviated variable names ¹ActivePremium,
## # ²AnnualizedAlpha, ³Correlation, ⁴`Correlationp-value`, ⁵InformationRatio
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = table.AnnualizedReturns)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio AnnualizedReturn `AnnualizedSharpe(Rf=0%)` AnnualizedStdDev
## <int> <dbl> <dbl> <dbl>
## 1 1 0.360 1.26 0.286
## 2 2 0.348 1.16 0.301
## 3 3 0.448 1.06 0.424
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.Correlation)
## # A tibble: 3 × 5
## # Groups: portfolio [3]
## portfolio `p-value` `Lower CI` `Upper CI` to.Rb
## <int> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.0000284 0.270 0.634 0.472
## 2 2 0.000122 0.229 0.608 0.438
## 3 3 0.0325 0.0220 0.457 0.252
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = table.DownsideRisk)
## # A tibble: 3 × 12
## # Groups: portfolio [3]
## portfolio DownsideDe…¹ Downs…² Downs…³ GainD…⁴ Histo…⁵ Histo…⁶ LossD…⁷ Maxim…⁸
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.045 0.0488 0.045 0.0538 -0.141 -0.0811 0.0517 0.402
## 2 2 0.0501 0.0538 0.0501 0.0528 -0.162 -0.0842 0.0588 0.438
## 3 3 0.0684 0.0721 0.0684 0.0831 -0.222 -0.131 0.0813 0.595
## # … with 3 more variables: `ModifiedES(95%)` <dbl>, `ModifiedVaR(95%)` <dbl>,
## # SemiDeviation <dbl>, and abbreviated variable names
## # ¹`DownsideDeviation(0%)`, ²`DownsideDeviation(MAR=10%)`,
## # ³`DownsideDeviation(Rf=0%)`, ⁴GainDeviation, ⁵`HistoricalES(95%)`,
## # ⁶`HistoricalVaR(95%)`, ⁷LossDeviation, ⁸MaximumDrawdown
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = table.DownsideRiskRatio)
## # A tibble: 3 × 9
## # Groups: portfolio [3]
## portfolio Annualiseddo…¹ Downs…² month…³ Omega Omega…⁴ Sorti…⁵ Upsid…⁶ Upsid…⁷
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.156 0.0198 0.045 2.48 1.48 0.652 0.0491 0.986
## 2 2 0.173 0.0217 0.0501 2.34 1.34 0.579 0.0507 0.952
## 3 3 0.237 0.0294 0.0684 2.32 1.32 0.567 0.0681 0.937
## # … with abbreviated variable names ¹Annualiseddownsiderisk,
## # ²Downsidepotential, ³monthlydownsiderisk, ⁴`Omega-sharperatio`,
## # ⁵Sortinoratio, ⁶Upsidepotential, ⁷Upsidepotentialratio
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.HigherMoments)
## Warning in bcosk(Ra[, n[1]], Rb[, n[2]]): skewness is close to zero. The
## classical definition of the coskewness statistic is not applicable and one
## should normalize using the comoment without standardization.
## Warning in bcosk(Ra[, n[1]], Rb[, n[2]]): skewness is close to zero. The
## classical definition of the coskewness statistic is not applicable and one
## should normalize using the comoment without standardization.
## Warning in bcosk(Ra[, n[1]], Rb[, n[2]]): skewness is close to zero. The
## classical definition of the coskewness statistic is not applicable and one
## should normalize using the comoment without standardization.
## # A tibble: 3 × 6
## # Groups: portfolio [3]
## portfolio BetaCoKurtosis BetaCoSkewness BetaCoVariance CoKurtosis CoSkewness
## <int> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.756 0.196 0.908 0 0
## 2 2 0.772 1.71 0.886 0 0
## 3 3 0.455 0.369 0.721 0 0
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.InformationRatio)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio AnnualisedTrackingError InformationRatio TrackingError
## <int> <dbl> <dbl> <dbl>
## 1 1 0.252 0.917 0.0728
## 2 2 0.271 0.809 0.0782
## 3 3 0.412 0.774 0.119
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = table.Variability)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio AnnualizedStdDev MeanAbsolutedeviation monthlyStdDev
## <int> <dbl> <dbl> <dbl>
## 1 1 0.286 0.0658 0.0825
## 2 2 0.301 0.0679 0.0868
## 3 3 0.424 0.091 0.122
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = VaR)
## # A tibble: 3 × 2
## # Groups: portfolio [3]
## portfolio VaR
## <int> <dbl>
## 1 1 -0.111
## 2 2 -0.123
## 3 3 -0.163
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio `ESSharpe(Rf=0%,p=95%)` `StdDevSharpe(Rf=0%,p=95%)` VaRSharpe(Rf=0…¹
## <int> <dbl> <dbl> <dbl>
## 1 1 0.172 0.355 0.263
## 2 2 0.146 0.334 0.236
## 3 3 0.150 0.317 0.238
## # … with abbreviated variable name ¹`VaRSharpe(Rf=0%,p=95%)`
##Customization
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio `ESSharpe(Rf=0%,p=95%)` `StdDevSharpe(Rf=0%,p=95%)` VaRSharpe(Rf=0…¹
## <int> <dbl> <dbl> <dbl>
## 1 1 0.172 0.355 0.263
## 2 2 0.146 0.334 0.236
## 3 3 0.150 0.317 0.238
## # … with abbreviated variable name ¹`VaRSharpe(Rf=0%,p=95%)`
wts <- c(0.5, 0.0, 0.5)
portfolio_returns_monthly <- stock_returns_monthly %>%
tq_portfolio(assets_col = symbol,
returns_col = Ra,
weights = wts,
col_rename = "Ra")
portfolio_returns_monthly %>%
ggplot(aes(x = date, y = Ra)) +
geom_bar(stat = "identity", fill = palette_light()[[1]]) +
labs(title = "Portfolio Returns",
subtitle = "50% AAPL, 0% GOOG, and 50% NFLX",
caption = "Shows an above-zero trend meaning positive returns",
x = "", y = "Monthly Returns") +
geom_smooth(method = "lm") +
theme_tq() +
scale_color_tq() +
scale_y_continuous(labels = scales::percent)
## `geom_smooth()` using formula 'y ~ x'
portfolio_returns_monthly %>%
ggplot(aes(x = date, y = Ra)) +
geom_bar(stat = "identity", fill = palette_light()[[1]]) +
labs(title = "Portfolio Returns",
subtitle = "50% AAPL, 0% GOOG, and 50% NFLX",
caption = "Shows an above-zero trend meaning positive returns",
x = "", y = "Monthly Returns") +
geom_smooth(method = "lm") +
theme_tq() +
scale_color_tq() +
scale_y_continuous(labels = scales::percent)
## `geom_smooth()` using formula 'y ~ x'
wts <- c(0.5, 0, 0.5)
portfolio_growth_monthly <- stock_returns_monthly %>%
tq_portfolio(assets_col = symbol,
returns_col = Ra,
weights = wts,
col_rename = "investment.growth",
wealth.index = TRUE) %>%
mutate(investment.growth = investment.growth * 10000)
portfolio_growth_monthly %>%
ggplot(aes(x = date, y = investment.growth)) +
geom_line(size = 2, color = palette_light()[[1]]) +
labs(title = "Portfolio Growth",
subtitle = "50% AAPL, 0% GOOG, and 50% NFLX",
caption = "Now we can really visualize performance!",
x = "", y = "Portfolio Value") +
geom_smooth(method = "loess") +
theme_tq() +
scale_color_tq() +
scale_y_continuous(labels = scales::dollar)
## `geom_smooth()` using formula 'y ~ x'
portfolio_growth_monthly_multi <- stock_returns_monthly_multi %>%
tq_portfolio(assets_col = symbol,
returns_col = Ra,
weights = weights_table,
col_rename = "investment.growth",
wealth.index = TRUE) %>%
mutate(investment.growth = investment.growth * 10000)
portfolio_growth_monthly_multi %>%
ggplot(aes(x = date, y = investment.growth, color = factor(portfolio))) +
geom_line(size = 2) +
labs(title = "Portfolio Growth",
subtitle = "Comparing Multiple Portfolios",
caption = "Portfolio 3 is a Standout!",
x = "", y = "Portfolio Value",
color = "Portfolio") +
geom_smooth(method = "loess") +
theme_tq() +
scale_color_tq() +
scale_y_continuous(labels = scales::dollar)
## `geom_smooth()` using formula 'y ~ x'
args(SharpeRatio)
## function (R, Rf = 0, p = 0.95, FUN = c("StdDev", "VaR", "ES"),
## weights = NULL, annualize = FALSE, SE = FALSE, SE.control = NULL,
## ...)
## NULL
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra,
performance_fun = SharpeRatio)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio `ESSharpe(Rf=0%,p=95%)` `StdDevSharpe(Rf=0%,p=95%)` VaRSharpe(Rf=0…¹
## <int> <dbl> <dbl> <dbl>
## 1 1 0.172 0.355 0.263
## 2 2 0.146 0.334 0.236
## 3 3 0.150 0.317 0.238
## # … with abbreviated variable name ¹`VaRSharpe(Rf=0%,p=95%)`
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra,
performance_fun = SharpeRatio,
Rf = 0.03 / 12)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio `ESSharpe(Rf=0.2%,p=95%)` `StdDevSharpe(Rf=0.2%,p=95%)` VaRSharpe(…¹
## <int> <dbl> <dbl> <dbl>
## 1 1 0.157 0.325 0.241
## 2 2 0.134 0.305 0.216
## 3 3 0.141 0.296 0.222
## # … with abbreviated variable name ¹`VaRSharpe(Rf=0.2%,p=95%)`
RaRb_multiple_portfolio %>%
tq_performance(Ra = Ra,
performance_fun = SharpeRatio,
Rf = 0.03 / 12,
p = 0.99)
## # A tibble: 3 × 4
## # Groups: portfolio [3]
## portfolio `ESSharpe(Rf=0.2%,p=99%)` `StdDevSharpe(Rf=0.2%,p=99%)` VaRSharpe(…¹
## <int> <dbl> <dbl> <dbl>
## 1 1 0.105 0.325 0.134
## 2 2 0.0952 0.305 0.115
## 3 3 0.0915 0.296 0.117
## # … with abbreviated variable name ¹`VaRSharpe(Rf=0.2%,p=99%)`