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
library(xts)
library(zoo)
library(dplyr)
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
start_date <- as.Date("2023-05-01")
end_date <- as.Date("2023-09-30")
table_data <- data.frame(
Category = character(),
Beginning_of_Term = character(),
End_of_Term = character(),
Value_Beg = character(),
Value_End = character(),
stringsAsFactors = FALSE
)
add_row <- function(category, value_beg, value_end = "") {
if (!is.null(value_beg) && !is.na(value_beg) && value_beg != "") {
value_beg_formatted <- format(value_beg, scientific = FALSE, trim = TRUE)
value_end_formatted <- format(value_end, scientific = FALSE, trim = TRUE)
new_row <- data.frame(
Category = category,
Beginning_of_Term = format(start_date, "%d %B %Y"),
End_of_Term = format(end_date, "%d %B %Y"),
Value_Beg = value_beg_formatted,
Value_End = value_end_formatted,
stringsAsFactors = FALSE
)
table_data <<- rbind(table_data, new_row) # Update the global table_data
} else {
cat("Warning: Data for", category, "is missing or incomplete.\n")
}
}
get_symbol_data <- function(symbol, src, from, to) {
tryCatch({
data <- getSymbols(symbol, src = src, from = from, to = to, auto.assign = FALSE, warnings = FALSE)
if (is.null(data) || nrow(data) == 0) {
warning(paste("No data returned for symbol:", symbol))
return(NULL)
}
return(data)
}, error = function(e) {
warning(paste("Error fetching data for symbol:", symbol, "-", e$message))
return(NULL)
})
}
update_data <- function() { ### Fetch S&P 500 and Nasdaq data
sp500_data <- get_symbol_data("^GSPC", "yahoo", start_date, end_date)
nasdaq_data <- get_symbol_data("^IXIC", "yahoo", start_date, end_date)
if (!is.null(sp500_data)) {
sp500_open <- round(as.numeric(first(Op(sp500_data))), 2)
sp500_close <- round(as.numeric(last(Cl(sp500_data))), 2)
add_row("S&P 500 Index Level", sp500_open, sp500_close)
}
if (!is.null(nasdaq_data)) {
nasdaq_open <- round(as.numeric(first(Op(nasdaq_data))), 2)
nasdaq_close <- round(as.numeric(last(Cl(nasdaq_data))), 2)
add_row("Nasdaq Composite Index Level", nasdaq_open, nasdaq_close)
}
prime_rate <- get_symbol_data("DPRIME", "FRED", start_date, end_date)
fed_funds_rate <- get_symbol_data("FEDFUNDS", "FRED", start_date, end_date)
commercial_paper_rate <- get_symbol_data("DCPN3M", "FRED", start_date, end_date) # 3-Month Nonfinancial Commercial Paper Rate
cd_rate <- get_symbol_data("TB3MS", "FRED", start_date, end_date) # 3-Month Treasury Bill Secondary Market Rate
tbill_13wk <- get_symbol_data("DTB3", "FRED", start_date, end_date)
tbill_26wk <- get_symbol_data("DTB6", "FRED", start_date, end_date)
if (!is.null(prime_rate)) {
add_row("Prime Rate", round(as.numeric(first(prime_rate)), 2), round(as.numeric(last(prime_rate)), 2))
}
if (!is.null(fed_funds_rate)) {
add_row("Federal Funds Rate", round(as.numeric(first(fed_funds_rate)), 2), round(as.numeric(last(fed_funds_rate)), 2))
}
if (!is.null(commercial_paper_rate)) {
add_row("Commercial Paper Rate (90 days)", round(as.numeric(first(commercial_paper_rate)), 2), round(as.numeric(last(commercial_paper_rate)), 2))
}
## Warning: Data for Commercial Paper Rate (90 days) is missing or incomplete.
if (!is.null(cd_rate)) {
add_row("Certificate of Deposit Rate (3-month)", round(as.numeric(first(cd_rate)), 2), round(as.numeric(last(cd_rate)), 2))
}
if (!is.null(tbill_13wk)) {
add_row("Treasury Bill Rate (13 weeks)", round(as.numeric(first(tbill_13wk)), 2), round(as.numeric(last(tbill_13wk)), 2))
}
if (!is.null(tbill_26wk)) {
add_row("Treasury Bill Rate (26 weeks)", round(as.numeric(first(tbill_26wk)), 2), round(as.numeric(last(tbill_26wk)), 2))
}
treasury_long_term_yield <- get_symbol_data("GS10", "FRED", start_date, end_date)
corporate_bond_yield <- get_symbol_data("BAA", "FRED", start_date, end_date)
high_yield_bond_yield <- get_symbol_data("BAMLH0A0HYM2EY", "FRED", start_date, end_date)
if (!is.null(treasury_long_term_yield)) {
add_row("Treasury Long-term Bond Yield", round(as.numeric(first(treasury_long_term_yield)), 2), round(as.numeric(last(treasury_long_term_yield)), 2))
}
if (!is.null(corporate_bond_yield)) {
add_row("Corporate (Master) Bond Yield", round(as.numeric(first(corporate_bond_yield)), 2), round(as.numeric(last(corporate_bond_yield)), 2))
}
if (!is.null(high_yield_bond_yield)) {
add_row("High-yield Corporate Bond Yield", round(as.numeric(first(high_yield_bond_yield)), 2), round(as.numeric(last(high_yield_bond_yield)), 2))
}
gbp_usd <- get_symbol_data("GBPUSD=X", "yahoo", start_date, end_date)
jpy_usd <- get_symbol_data("JPYUSD=X", "yahoo", start_date, end_date)
mxn_usd <- get_symbol_data("MXNUSD=X", "yahoo", start_date, end_date)
if (!is.null(gbp_usd)) {
gbp_beg <- round(as.numeric(first(Cl(gbp_usd))), 4)
gbp_end <- round(as.numeric(last(Cl(gbp_usd))), 4)
add_row("Exchange Rate of the British Pound (in $)", gbp_beg, gbp_end)
}
if (!is.null(jpy_usd)) {
jpy_beg <- round(as.numeric(first(Cl(jpy_usd))), 6)
jpy_end <- round(as.numeric(last(Cl(jpy_usd))), 6)
add_row("Exchange Rate of the Japanese Yen (in $)", jpy_beg, jpy_end)
}
if (!is.null(mxn_usd)) {
mxn_beg <- round(as.numeric(first(Cl(mxn_usd))), 6)
mxn_end <- round(as.numeric(last(Cl(mxn_usd))), 6)
add_row("Exchange Rate of the Mexican Peso (in $)", mxn_beg, mxn_end)
}
treasury_futures <- get_symbol_data("ZB=F", "yahoo", start_date, end_date)
if (!is.null(treasury_futures)) {
treasury_beg <- round(as.numeric(first(Cl(treasury_futures))), 2)
treasury_end <- round(as.numeric(last(Cl(treasury_futures))), 2)
add_row("Treasury Bond Futures", treasury_beg, treasury_end)
}
sp500_futures <- get_symbol_data("ES=F", "yahoo", start_date, end_date)
if (!is.null(sp500_futures)) {
sp500_fut_beg <- round(as.numeric(first(Cl(sp500_futures))), 2)
sp500_fut_end <- round(as.numeric(last(Cl(sp500_futures))), 2)
add_row("S&P 500 Index Futures", sp500_fut_beg, sp500_fut_end)
}
bp_futures <- get_symbol_data("6B=F", "yahoo", start_date, end_date)
if (!is.null(bp_futures)) {
bp_fut_beg <- round(as.numeric(first(Cl(bp_futures))), 4)
bp_fut_end <- round(as.numeric(last(Cl(bp_futures))), 4)
add_row("British Pound Futures", bp_fut_beg, bp_fut_end)
}
call_option_firm <- "AAPL"
call_option_expiration <- "2024-12-20"
call_option_strike <- 150 # Example strike price
call_option_premium <- 10 # Example premium
firm_data <- get_symbol_data(call_option_firm, "yahoo", Sys.Date() - 10, Sys.Date() - 1)
if (!is.null(firm_data)) {
current_price <- round(as.numeric(last(Cl(firm_data))), 2)
} else {
current_price <- "N/A"
}
add_row(paste("Call Option on", call_option_firm, "- Stock Price"), current_price)
add_row(paste("Call Option on", call_option_firm, "- Option Premium"), call_option_premium)
add_row(paste("Call Option on", call_option_firm, "- Strike Price"), call_option_strike)
add_row(paste("Call Option on", call_option_firm, "- Expiration"), call_option_expiration)
put_option_firm <- "INTC"
put_option_expiration <- "2024-12-20"
put_option_strike <- 50
put_option_premium <- 0.0005
firm_data <- get_symbol_data(put_option_firm, "yahoo", Sys.Date() - 10, Sys.Date() - 1)
if (!is.null(firm_data)) {
current_price <- round(as.numeric(last(Cl(firm_data))), 2)
} else {
current_price <- "N/A"
}
add_row(paste("Put Option on", put_option_firm, "- Stock Price"), current_price)
add_row(paste("Put Option on", put_option_firm, "- Option Premium"), put_option_premium)
add_row(paste("Put Option on", put_option_firm, "- Strike Price"), put_option_strike)
add_row(paste("Put Option on", put_option_firm, "- Expiration"), put_option_expiration)
currency_call = "Euro (EURUSD)"
currency_call_expiration = "2024-12-20"
currency_call_strike = 1.20
currency_call_premium = 0.05
eurusd_data <- get_symbol_data("EURUSD=X", "yahoo", Sys.Date() - 10, Sys.Date() - 1)
if (!is.null(eurusd_data)) {
eurusd_price <- round(as.numeric(last(Cl(eurusd_data))), 4)
} else {
eurusd_price <- "N/A"
}
add_row(paste("Call Option on", currency_call, "- Currency Value"), eurusd_price)
add_row(paste("Call Option on", currency_call, "- Option Premium"), currency_call_premium)
add_row(paste("Call Option on", currency_call, "- Strike Price"), currency_call_strike)
add_row(paste("Call Option on", currency_call, "- Expiration"), currency_call_expiration)
currency_put = "Japanese Yen (JPYUSD)"
currency_put_expiration = "2024-12-20"
currency_put_strike = 0.0090
currency_put_premium = 0.0005
jpyusd_data <- get_symbol_data("JPYUSD=X", "yahoo", Sys.Date() - 10, Sys.Date() - 1)
if (!is.null(jpyusd_data)) {
jpyusd_price <- round(as.numeric(last(Cl(jpyusd_data))), 6)
} else {
jpyusd_price <- "N/A"
}
add_row(paste("Put Option on", currency_put, "- Currency Value"), jpyusd_price)
add_row(paste("Put Option on", currency_put, "- Option Premium"), currency_put_premium)
add_row(paste("Put Option on", currency_put, "- Strike Price"), currency_put_strike)
add_row(paste("Put Option on", currency_put, "- Expiration"), currency_put_expiration)
print(table_data, right = FALSE)
## Category Beginning_of_Term
## 1 S&P 500 Index Level 01 May 2023
## 2 Nasdaq Composite Index Level 01 May 2023
## 3 Prime Rate 01 May 2023
## 4 Federal Funds Rate 01 May 2023
## 5 Certificate of Deposit Rate (3-month) 01 May 2023
## 6 Treasury Bill Rate (13 weeks) 01 May 2023
## 7 Treasury Bill Rate (26 weeks) 01 May 2023
## 8 Treasury Long-term Bond Yield 01 May 2023
## 9 Corporate (Master) Bond Yield 01 May 2023
## 10 High-yield Corporate Bond Yield 01 May 2023
## 11 Exchange Rate of the British Pound (in $) 01 May 2023
## 12 Exchange Rate of the Japanese Yen (in $) 01 May 2023
## 13 Exchange Rate of the Mexican Peso (in $) 01 May 2023
## 14 Treasury Bond Futures 01 May 2023
## 15 S&P 500 Index Futures 01 May 2023
## 16 British Pound Futures 01 May 2023
## 17 Call Option on AAPL - Stock Price 01 May 2023
## 18 Call Option on AAPL - Option Premium 01 May 2023
## 19 Call Option on AAPL - Strike Price 01 May 2023
## 20 Call Option on AAPL - Expiration 01 May 2023
## 21 Put Option on INTC - Stock Price 01 May 2023
## 22 Put Option on INTC - Option Premium 01 May 2023
## 23 Put Option on INTC - Strike Price 01 May 2023
## 24 Put Option on INTC - Expiration 01 May 2023
## 25 Call Option on Euro (EURUSD) - Currency Value 01 May 2023
## 26 Call Option on Euro (EURUSD) - Option Premium 01 May 2023
## 27 Call Option on Euro (EURUSD) - Strike Price 01 May 2023
## 28 Call Option on Euro (EURUSD) - Expiration 01 May 2023
## 29 Put Option on Japanese Yen (JPYUSD) - Currency Value 01 May 2023
## 30 Put Option on Japanese Yen (JPYUSD) - Option Premium 01 May 2023
## 31 Put Option on Japanese Yen (JPYUSD) - Strike Price 01 May 2023
## 32 Put Option on Japanese Yen (JPYUSD) - Expiration 01 May 2023
## End_of_Term Value_Beg Value_End
## 1 30 September 2023 4166.79 4288.05
## 2 30 September 2023 12210.05 13219.32
## 3 30 September 2023 8 8.5
## 4 30 September 2023 5.06 5.33
## 5 30 September 2023 5.14 5.32
## 6 30 September 2023 5.1 5.32
## 7 30 September 2023 4.91 5.32
## 8 30 September 2023 3.57 4.38
## 9 30 September 2023 5.77 6.16
## 10 30 September 2023 8.25 8.8
## 11 30 September 2023 1.2558 1.2205
## 12 30 September 2023 0.007332 0.006696
## 13 30 September 2023 0.055582 0.057016
## 14 30 September 2023 129.66 113.78
## 15 30 September 2023 4185.75 4325.5
## 16 30 September 2023 1.2491 1.2207
## 17 30 September 2023 255.59
## 18 30 September 2023 10
## 19 30 September 2023 150
## 20 30 September 2023 2024-12-20
## 21 30 September 2023 20.3
## 22 30 September 2023 0.0005
## 23 30 September 2023 50
## 24 30 September 2023 2024-12-20
## 25 30 September 2023 1.0423
## 26 30 September 2023 0.05
## 27 30 September 2023 1.2
## 28 30 September 2023 2024-12-20
## 29 30 September 2023 0.006341
## 30 30 September 2023 0.0005
## 31 30 September 2023 0.009
## 32 30 September 2023 2024-12-20