Extract fill events from a backtest
Details
Fill rows describe execution events and may include both opening and
closing actions. Closed trades are exposed by ledgr_results(bt, what = "trades").
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_extract_fills(bt)
#> # A tibble: 1 × 9
#> event_seq ts_utc instrument_id side qty price fee
#> <int> <dttm> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 1 2020-01-02 00:00:00 AAA BUY 1 101 0
#> # ℹ 2 more variables: realized_pnl <dbl>, action <chr>
close(bt)