R/processraw.R

Defines functions process_cleaned_export

Documented in process_cleaned_export

# This script cleans the exported Vanguard data and exports it to a csv file.
# It may take a minute or two to run.

#' Import, clean, and export Vanguard's transaction data for analysis.
#'
#' @param filename A string specifying the path to the exported data
#' from Vanguard (.xlsx file format).
process_cleaned_export <- function(filename) {
  if (file.exists('data/balancehistory.csv')) {
    return()
  }
  processed_transaction = VanguardData::clean_raw_export(filename)
  all_stocks <- levels(processed_transaction$symbol)
  all_accounts <- levels(processed_transaction$account_number)

  # Store each day's tibble of data in a list, before combining.
  daily_list <- list()

  start_date <-
    min(processed_transaction$trade_date)
  end_date <-
    max(processed_transaction$trade_date)

  all_dates <-  seq(as.Date(start_date),
                    to = as.Date(end_date),
                    by = "day")

  for (i in 1:length(all_dates)) {
    daily_list[[i]] <-
      day_holdings(all_dates[i],
                   processed_transaction,
                   all_stocks,
                   all_accounts)
  }

  # Collapse daily_list into a tibble and save to a csv file
  combined_holdings <- bind_rows(daily_list)
  write_csv(combined_holdings, '../data/balancehistory.csv')
}

process_cleaned_export('../vanguardexport.xlsx')
akprasadan/scrape_vanguard_transactions documentation built on Feb. 21, 2022, 12:04 a.m.