This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# Install and load necessary packages
install.packages("quantmod")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
install.packages("dplyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
install.packages("ggplot2")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
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(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
library(ggplot2)
# Define a function to fetch stock data and calculate returns
calculate_stock_return <- function(ticker, start_date, end_date) {
stock_data <- getSymbols(ticker, src = "yahoo", from = start_date, to = end_date, auto.assign = FALSE)
stock_prices <- Cl(stock_data) # Closing prices
start_price <- as.numeric(stock_prices[1])
end_price <- as.numeric(stock_prices[length(stock_prices)])
return((end_price - start_price) / start_price)
}
# Example data for Pelosi's and McConnell's trades
# Replace with actual transaction dates and tickers
pelosi_trades <- data.frame(
politician = "Nancy Pelosi",
ticker = c("AAPL", "NVDA", "PYPL"),
transaction_date = as.Date(c("2024-12-31", "2024-12-20", "2022-12-28"))
)
mcconnell_trades <- data.frame(
politician = "Mitch McConnell",
ticker = c("WFC", "VMC", "IR"),
transaction_date = as.Date(c("2024-12-02", "2019-06-03", "2019-06-03"))
)
# Combine the data
all_trades <- bind_rows(pelosi_trades, mcconnell_trades)
# Define the end date for performance calculation
end_date <- Sys.Date()
# Calculate returns for each trade
all_trades <- all_trades %>%
rowwise() %>%
mutate(
start_date = transaction_date,
return = calculate_stock_return(ticker, start_date, end_date)
)
# View the results
print(all_trades)
## # A tibble: 6 × 5
## # Rowwise:
## politician ticker transaction_date start_date return
## <chr> <chr> <date> <date> <dbl>
## 1 Nancy Pelosi AAPL 2024-12-31 2024-12-31 -0.0541
## 2 Nancy Pelosi NVDA 2024-12-20 2024-12-20 -0.0264
## 3 Nancy Pelosi PYPL 2022-12-28 2022-12-28 0.129
## 4 Mitch McConnell WFC 2024-12-02 2024-12-02 0.0527
## 5 Mitch McConnell VMC 2019-06-03 2019-06-03 1.06
## 6 Mitch McConnell IR 2019-06-03 2019-06-03 1.71
# Plot the returns
ggplot(all_trades, aes(x = ticker, y = return, fill = politician)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Stock Returns of Pelosi's and McConnell's Trades",
x = "Stock Ticker",
y = "Return",
fill = "Politician") +
theme_minimal()