PineForge v0.1.2-11-ga87241d
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
Configuration

PineForge exposes three configuration surfaces:

  1. Pine inputs — anything declared with input.*() in the source script.
  2. Strategy declaration overrides — fields of the script's strategy(...) call.
  3. Runtime knobs — bar magnifier, trade-start gating, trace recording.

All three are set on the strategy handle, before calling run_backtest. Calls made after a run are queued for the next run.

Pine inputs

strategy_set_input(s, "Length", "21");
strategy_set_input(s, "Use Trend Filter","true");
strategy_set_input(s, "Source", "close");
void strategy_set_input(pf_strategy_t s, const char *key, const char *value)
Override a Pine input.
Pine type Serialized form
int decimal string, e.g. "21"
float decimal string, e.g. "0.04" (use . always — no locale)
bool "true" / "false"
string the string itself (no quoting)
source one of "open", "high", "low", "close", "hl2", "hlc3", "ohlc4", "hlcc4"
color hex "#RRGGBB" or "#AARRGGBB"
timeframe TV-style — "1", "5", "60", "1D", "1W"

The key is the input's title, exactly as it appears in the Pine title= argument. If no title was given, the runtime falls back to the variable identifier.

Note
Unknown keys are silently accepted and ignored. This lets harnesses set a superset of inputs across multiple strategies without per-strategy gating.

Strategy declaration overrides

strategy_set_override(s, "initial_capital", "100000");
strategy_set_override(s, "commission_value", "0.04");
strategy_set_override(s, "commission_type", "percent"); /* or "cash_per_contract", "cash_per_order" */
strategy_set_override(s, "slippage", "1");
strategy_set_override(s, "default_qty_value", "100");
strategy_set_override(s, "default_qty_type", "percent_of_equity"); /* or "fixed", "cash" */
strategy_set_override(s, "pyramiding", "0");
strategy_set_override(s, "process_orders_on_close","true");
strategy_set_override(s, "close_entries_rule", "FIFO"); /* or "ANY" */
void strategy_set_override(pf_strategy_t s, const char *key, const char *value)
Override a strategy(...) declaration parameter.

These mirror the parameters of Pine's strategy(...) declaration. Setting them via the C ABI overrides the script-defined defaults for this run.

Runtime knobs

Bar magnifier

Configured per-call on run_backtest_full:

run_backtest_full(s, bars, n, "5", "60",
/*magnifier=*/1, /*samples=*/4,
void run_backtest_full(pf_strategy_t s, pf_bar_t *bars, int n, const char *input_tf, const char *script_tf, int bar_magnifier, int magnifier_samples, pf_magnifier_distribution_t magnifier_dist, pf_report_t *out)
Run a backtest with explicit timeframe and magnifier configuration.
@ PF_MAGNIFIER_ENDPOINTS
Default — exact O,H,L,C points plus uniform fill between.
Definition pineforge.h:87

Volume-weighted sampling is a separate sticky toggle:

void strategy_set_magnifier_volume_weighted(pf_strategy_t s, int on)
Toggle volume-weighted bar-magnifier sampling.

See Bar magnifier for the sampling model.

Trace recording

void strategy_set_trace_enabled(pf_strategy_t s, int on)
Toggle per-bar trace recording.

Captures // @pf-trace name=expr pragma values per bar into pf_report_t::trace. Zero-cost when disabled. See Report schema § Trace records.

Trade start gate

/* Only allow strategy.entry/exit/close/order to fire on or after this bar. */
strategy_set_trade_start_time(s, 1700000000000LL);
void strategy_set_trade_start_time(pf_strategy_t s, int64_t timestamp_ms)
Set the earliest Unix-ms timestamp at which strategy order commands may fire.

Earlier bars still execute user code and warm TA / series state. Only order commands are ignored. Use this to leave room for indicators to stabilize before trades start.

Configuration is per-handle

Configuration values stick to the handle, not the run. They apply to every run_backtest on that handle until overwritten or strategy_free is called.

For parameter sweeps, create a fresh handle per run — a handle's trade history accumulates across runs (see Lifecycle):

for (int len = 10; len <= 30; len += 2) {
strategy_set_override(s, "initial_capital", "100000");
char buf[16]; snprintf(buf, sizeof buf, "%d", len);
strategy_set_input(s, "Length", buf);
pf_report_t r = {0};
run_backtest(s, bars, n, &r);
}
pf_strategy_t strategy_create(const char *params_json)
Allocate a new strategy instance.
void run_backtest(pf_strategy_t s, pf_bar_t *bars, int n, pf_report_t *out)
Run a backtest with auto-detected timeframe and no bar magnifier.
void strategy_free(pf_strategy_t s)
Release a strategy handle previously returned by strategy_create.
void report_free(pf_report_t *report)
Free heap arrays attached to a filled report.
void * pf_strategy_t
Opaque handle to a compiled strategy instance.
Definition pineforge.h:194
Backtest report filled by run_backtest / run_backtest_full.
Definition pineforge.h:153