Skip to contents

Imports EOD bars into snapshot_bars for a snapshot in status CREATED (snapshot mutability rule). Optionally imports instruments from CSV, or auto-generates them from bars.

Usage

ledgr_snapshot_import_bars_csv(
  con,
  snapshot_id,
  bars_csv_path,
  instruments_csv_path = NULL,
  auto_generate_instruments = TRUE,
  encoding = "UTF-8",
  validate = c("fail_fast", "none")
)

Arguments

con

A DBI connection to DuckDB.

snapshot_id

Snapshot id (must exist and be status CREATED).

bars_csv_path

Path to bars CSV.

instruments_csv_path

Optional path to instruments CSV.

auto_generate_instruments

If TRUE and instruments_csv_path is NULL, auto-generate instruments from bars.

encoding

File encoding (default "UTF-8").

validate

Validation mode (default "fail_fast").

Value

Invisibly returns TRUE on success.

Details

CSV contract (v0.1.1 spec section 6.1):

  • Required columns: instrument_id, ts_utc, open, high, low, close

  • Optional columns: volume (defaults to NA)

  • Timestamp format: ISO8601 UTC with trailing Z, e.g. 2020-01-01T00:00:00Z

  • Encoding: UTF-8 (BOM tolerated and stripped)

  • Rounding: OHLCV are rounded to 8 decimals on import

Errors:

  • LEDGR_SNAPSHOT_NOT_FOUND if snapshot_id does not exist.

  • LEDGR_SNAPSHOT_NOT_MUTABLE if snapshot status is not CREATED.

  • LEDGR_CSV_FORMAT_ERROR on CSV contract/parse violations or duplicate PKs.

Articles

Durable experiment stores: vignette("experiment-store", package = "ledgr") system.file("doc", "experiment-store.html", package = "ledgr")

Examples

db_path <- tempfile(fileext = ".duckdb")
con <- ledgr_db_init(db_path)
snapshot_id <- ledgr_snapshot_create(
  con,
  snapshot_id = "snapshot_20200101_000000_abcd"
)
bars_csv <- tempfile(fileext = ".csv")
utils::write.csv(data.frame(
  instrument_id = "AAA",
  ts_utc = c("2020-01-01T00:00:00Z", "2020-01-02T00:00:00Z"),
  open = c(100, 101),
  high = c(101, 102),
  low = c(99, 100),
  close = c(100, 101),
  volume = 1000
), bars_csv, row.names = FALSE)
ledgr_snapshot_import_bars_csv(con, snapshot_id, bars_csv)
DBI::dbDisconnect(con, shutdown = TRUE)