Skip to contents

The strategy pulse context is the object passed to every function(ctx, params) strategy at one decision time. It exposes only data that is known at that pulse: current bars, registered current feature values, current simulated positions, cash, equity, and helpers for building target vectors.

Details

Common accessors:

  • ctx$ts_utc: current decision timestamp.

  • ctx$universe: character vector of instruments in the run.

  • ctx$open(id), ctx$high(id), ctx$low(id), ctx$close(id), ctx$volume(id): current bar values for one instrument.

  • ctx$bar(id): the current OHLCV row for one instrument.

  • ctx$feature(id, feature_id): one registered feature value for one instrument, addressed by ledgr's exact engine feature ID such as "return_5".

  • ctx$features(id, feature_map): mapped feature values for one instrument, returned as a named numeric vector keyed by feature-map aliases.

  • ctx$position(id): current simulated position for one instrument.

  • ctx$cash and ctx$equity: current simulated portfolio state.

  • ctx$flat(): full named target vector initialized to zero.

  • ctx$hold(): full named target vector initialized to current positions.

Feature IDs and feature-map aliases are different namespaces. ctx$feature() uses engine IDs from ledgr_feature_id(). ctx$features() uses a ledgr_feature_map object and returns values named by alias. Unknown engine feature IDs fail loudly; warmup for a known feature is represented by NA_real_.

Strategies should return full named numeric target vectors. The helpers ctx$flat() and ctx$hold() are the safest starting points because they already have one entry for every instrument in ctx$universe.

Feature Object Compatibility

ledgr_experiment(features = ...) accepts static indicators, lists of indicators, named lists, feature maps, and feature factories of the form function(params). Pulse inspection helpers accept static feature shapes. Inside the strategy context, ctx$feature() reads one engine feature ID and ctx$features() requires a ledgr_feature_map. Lower-level or legacy helpers may be narrower; the experiment-first workflow is the canonical public path for feature maps.

Articles

Strategy authoring: vignette("strategy-development", package = "ledgr") system.file("doc", "strategy-development.html", package = "ledgr")

Indicator IDs and feature maps: vignette("indicators", package = "ledgr") system.file("doc", "indicators.html", package = "ledgr")

Examples

features <- ledgr_feature_map(
  ret_5 = ledgr_ind_returns(5),
  sma_10 = ledgr_ind_sma(10)
)

strategy <- function(ctx, params) {
  targets <- ctx$flat()
  for (id in ctx$universe) {
    x <- ctx$features(id, features)
    if (passed_warmup(x) && x[["ret_5"]] > params$min_return) {
      targets[id] <- params$qty
    }
  }
  targets
}