Skip to contents

Package-prefixed convenience wrapper around tibble::as_tibble() for backtest result tables.

Usage

ledgr_results(bt, what = c("equity", "fills", "trades", "ledger"))

Arguments

bt

A ledgr_backtest object.

what

Result table to extract: "equity", "fills", "trades", or "ledger".

Value

A ledgr result table, which is a classed tibble with the requested result columns.

Details

The returned object is tibble-compatible. Its print method may compact all-midnight UTC timestamps for EOD output according to options(ledgr.print_ts_utc), but programmatic access keeps ts_utc as POSIXct UTC.

what = "fills" returns execution fill rows, including opening and closing actions. Fill rows include execution side, absolute qty, price, fee, derived action, and realized_pnl. Opening fills have action = "OPEN" and do not count as closed trades.

what = "trades" returns closed trade rows only. This table has the same zero-row schema as fills, but only rows with action = "CLOSE" are present. It is the source for n_trades, win_rate, and avg_trade.

what = "equity" returns the public equity curve used for return, drawdown, volatility, and exposure metrics. Open positions can affect equity through positions_value even when there are zero closed trade rows.

ledgr_results() does not support what = "metrics". Metrics are derived from the public result tables; use summary(bt) for printed interpretation or ledgr_compute_metrics(bt) for a named list.

Articles

Metrics and accounting: vignette("metrics-and-accounting", package = "ledgr") system.file("doc", "metrics-and-accounting.html", package = "ledgr")

Examples

bars <- data.frame(
  ts_utc = as.POSIXct("2020-01-01", tz = "UTC") + 86400 * 0:3,
  instrument_id = "AAA",
  open = c(100, 101, 102, 103),
  high = c(101, 102, 103, 104),
  low = c(99, 100, 101, 102),
  close = c(100, 101, 102, 103),
  volume = 1000
)
strategy <- function(ctx, params) {
  targets <- ctx$flat()
  targets["AAA"] <- 1
  targets
}
bt <- ledgr_backtest(data = bars, strategy = strategy, initial_cash = 1000)
ledgr_results(bt, what = "trades")
#> # A tibble: 0 × 9
#> # ℹ 9 variables: event_seq <int>, ts_utc <date>, instrument_id <chr>,
#> #   side <chr>, qty <dbl>, price <dbl>, fee <dbl>, realized_pnl <dbl>,
#> #   action <chr>
close(bt)