Import snapshot bars from CSV (v0.1.1)
Source:R/snapshots-import-bars.R
ledgr_snapshot_import_bars_csv.RdImports 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_pathis NULL, auto-generate instruments from bars.- encoding
File encoding (default
"UTF-8").- validate
Validation mode (default
"fail_fast").
Details
CSV contract (v0.1.1 spec section 6.1):
Required columns:
instrument_id,ts_utc,open,high,low,closeOptional columns:
volume(defaults toNA)Timestamp format: ISO8601 UTC with trailing
Z, e.g.2020-01-01T00:00:00ZEncoding: UTF-8 (BOM tolerated and stripped)
Rounding: OHLCV are rounded to 8 decimals on import
Errors:
LEDGR_SNAPSHOT_NOT_FOUNDifsnapshot_iddoes not exist.LEDGR_SNAPSHOT_NOT_MUTABLEif snapshot status is notCREATED.LEDGR_CSV_FORMAT_ERRORon 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)