Learn R Programming

PortfolioTesteR (version 0.1.4)

csv_adapter: Load Price Data from CSV File

Description

Reads stock price data from CSV files with flexible column naming. Automatically standardizes to library format.

Usage

csv_adapter(
  file_path,
  date_col = "Date",
  symbol_col = "Symbol",
  price_col = "Price",
  frequency = "daily",
  symbol_order = NULL
)

Value

Data.table with Date column and price columns

Arguments

file_path

Path to CSV file

date_col

Name of date column (default: "date")

symbol_col

Name of symbol column (default: "symbol")

price_col

Name of price column (default: "close")

frequency

Target frequency: "daily" or "weekly" (default: "daily")

symbol_order

Optional vector to order symbols

Examples

Run this code
# Create a temporary tidy CSV from included weekly sample data (offline, fast)
data("sample_prices_weekly")
PW <- as.data.frame(sample_prices_weekly)
syms <- setdiff(names(PW), "Date")[1:2]

stk  <- stack(PW[1:10, syms])
tidy <- data.frame(
  Date   = rep(PW$Date[1:10], times = length(syms)),
  Symbol = stk$ind,
  Price  = stk$values
)

tmp <- tempfile(fileext = ".csv")
write.csv(tidy, tmp, row.names = FALSE)
prices <- csv_adapter(tmp)
head(prices)
unlink(tmp)

Run the code above in your browser using DataLab