52#include <pineforge/version.h>
56#if defined(_WIN32) || defined(__CYGWIN__)
57 #if defined(PINEFORGE_BUILD_SHARED)
58 #define PF_API __declspec(dllexport)
60 #define PF_API __declspec(dllimport)
62#elif defined(__GNUC__) || defined(__clang__)
63 #define PF_API __attribute__((visibility("default")))
78#define PF_ABI_VERSION 2
95typedef enum pf_magnifier_distribution_e {
107typedef struct pf_bar_s {
119typedef struct pf_trade_s {
147typedef struct pf_trade_stats_s {
210typedef struct pf_equity_stats_s {
248typedef struct pf_metrics_s {
257typedef struct pf_equity_point_s {
266typedef struct pf_security_diag_s {
278typedef struct pf_trace_entry_s {
297typedef struct pf_report_s {
381#ifndef PINEFORGE_NO_STRATEGY_DECLS
424 const char* input_tf,
425 const char* script_tf,
427 int magnifier_samples,
572typedef struct pf_version_s {
void strategy_set_syminfo_timezone(pf_strategy_t s, const char *tz)
Plumb the symbol's exchange timezone (IANA string) into syminfo.
void strategy_set_syminfo_mintick(pf_strategy_t s, double mintick)
Set the instrument tick size (syminfo.mintick, default 0.01).
void strategy_set_override(pf_strategy_t s, const char *key, const char *value)
Override a strategy(...) declaration parameter.
void strategy_set_input(pf_strategy_t s, const char *key, const char *value)
Override a Pine input.
const char * strategy_get_last_error(pf_strategy_t s)
Returns the error message captured by the most recent run_backtest / run_backtest_full call on this s...
void strategy_set_syminfo_session(pf_strategy_t s, const char *session)
Set the symbol's session string (e.g.
void strategy_set_syminfo_pointvalue(pf_strategy_t s, double pointvalue)
Set the instrument point value (syminfo.pointvalue, default 1.0) — the $-per-point-per-contract multi...
void strategy_set_chart_timezone(pf_strategy_t s, const char *tz)
Set the strategy's chart timezone (IANA / POSIX TZ string).
void strategy_set_magnifier_volume_weighted(pf_strategy_t s, int on)
Toggle volume-weighted bar-magnifier sampling.
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.
void strategy_set_syminfo_metadata(pf_strategy_t s, const char *key, double value)
Inject a fundamental/exchange metadata value by Pine member name (e.g.
void strategy_set_trace_enabled(pf_strategy_t s, int on)
Toggle per-bar trace recording.
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 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_distribution_t
Bar-magnifier sub-bar sampling distribution.
@ PF_MAGNIFIER_FRONT_LOADED
Sample density biased toward bar open.
@ PF_MAGNIFIER_COSINE
Cosine-tapered density.
@ PF_MAGNIFIER_ENDPOINTS
Default — exact O,H,L,C points plus uniform fill between.
@ PF_MAGNIFIER_BACK_LOADED
Sample density biased toward bar close.
@ PF_MAGNIFIER_TRIANGLE
Triangle-tapered density.
@ PF_MAGNIFIER_UNIFORM
Uniform spacing across the parent bar.
pf_version_t pf_version_get(void)
const char * pf_version_string(void)
Full git-derived version descriptor.
void * pf_strategy_t
Opaque handle to a compiled strategy instance.
Single OHLCV bar pushed into the engine.
int64_t timestamp
Bar open time, Unix milliseconds.
Single per-script-bar equity point.
double open_profit
Mark-to-market open P&L at bar close.
double equity
initial_capital + net_profit + open_profit.
int64_t time_ms
Script-bar OPEN timestamp (Unix ms).
Equity-curve-derived statistics (all-trades only, like TV).
double sharpe_tv
Month-end-resampled equity simple returns (chart timezone, open-time bucketing), risk-free 2%/yr (2/1...
double time_in_market_pct
PERCENT (0-100) of script bars with an open position at bar close.
double max_equity_drawdown_pct
max_equity_drawdown relative to the peak in effect (PERCENT 0-100).
double max_equity_drawdown
Peak-to-trough equity drop, positive currency magnitude.
double max_equity_runup_pct
max_equity_runup relative to that trough (PERCENT 0-100).
double buy_hold_return
initial_capital * (last_close/first_open - 1), currency.
double open_pl
Mark-to-market open profit at the final bar.
double max_equity_runup
Trough-to-peak rise where the trough resets on each new equity peak (mirrors the engine's intra-run e...
double sortino_tv
Same resampling as sharpe_tv; uses population downside deviation vs the monthly risk-free.
double cagr
PERCENT per year: 100*((final_equity/initial_capital)^(1/years)-1).
double recovery_factor
net_profit / max_equity_drawdown (currency / currency).
double calmar
cagr / max_equity_drawdown_pct — BOTH IN PERCENT, so the ratio is dimensionless.
double buy_hold_return_pct
buy_hold_return as PERCENT.
double sharpe_bar
Per-script-bar returns, annualized by observed bar density (bars per year = (len-1)/calendar span),...
double sortino_bar
Same construction as sharpe_bar over per-bar returns; uses population downside deviation.
Composite metrics container: trade stats (all / long / short) + equity-curve stats.
Backtest report filled by run_backtest / run_backtest_full.
int input_tf_seconds
Detected/configured input timeframe (seconds).
int security_diag_len
Length of security_diag.
int64_t security_feeds_total
Total higher-TF feed bars across all security sites.
int bar_magnifier_enabled
1 if magnifier was active for this run.
pf_trace_entry_t * trace
Per-bar trace records (empty unless tracing enabled).
int trace_names_len
Length of trace_names.
int64_t input_bars_processed
Source-feed bars consumed.
int script_tf_seconds
Script timeframe (seconds).
double net_profit
Sum of all closed-trade PnL.
int trades_len
Length of trades.
int trace_len
Length of trace.
int64_t script_bars_processed
Script-timeframe bars evaluated.
int64_t magnifier_sample_ticks_total
Sample ticks visited by the magnifier.
int total_trades
Closed-trade count (== trades_len).
int64_t security_partial_total
Total partial-bar evals across all security sites.
pf_security_diag_t * security_diag
One entry per request.security site.
int64_t magnifier_sub_bars_total
Sub-bars synthesized by the magnifier.
const char ** trace_names
Names indexed by pf_trace_entry_t::name_id.
pf_equity_point_t * equity_curve
int64_t security_complete_total
Total complete-bar evals across all security sites.
int script_tf_ratio
script_tf_seconds / input_tf_seconds.
int needs_aggregation
1 if input → script TF aggregation was performed.
pf_trade_t * trades
Heap array of closed trades.
Per-request.security() site diagnostic counters.
int sec_id
Stable id for the request.security site.
int64_t feed_count
Higher-TF feed bars consumed.
int64_t complete_count
Evaluations on completed parent bars.
int64_t partial_count
Evaluations on still-forming parent bars.
Single per-bar trace entry.
double value
Traced expression value on this bar.
int64_t timestamp
Bar timestamp (Unix ms).
int32_t name_id
Index into pf_report_t::trace_names.
int32_t bar_index
Zero-based bar index.
Trade-level statistics block — computed once each for all / long / short.
double avg_win_pct
Mean of per-trade pnl_pct over winning trades.
double avg_win
gross_profit / num_wins.
double net_profit_pct
net_profit as a percent of initial capital (0-100 scale).
double largest_loss_pct
Maximum of -pnl_pct over losing trades (positive magnitude) — an INDEPENDENT maximum,...
int32_t num_losses
Trades with pnl < 0.
double gross_profit
Sum of winning pnl.
int32_t max_consecutive_losses
Longest losing run; even trades reset both streaks.
double avg_loss_pct
Mean of the NEGATED pnl_pct of the losing trades.
double commission_paid
Sum of pf_trade_t::commission in the block.
double percent_profitable
100 * num_wins / num_trades, in PERCENT (0-100).
double largest_win_pct
Maximum pnl_pct over winning trades — an INDEPENDENT maximum, not the pct of the largest-USD win (TV ...
double gross_loss
Sum of |losing pnl| — POSITIVE magnitude (TV display convention).
double gross_profit_pct
gross_profit as a percent of initial capital (0-100 scale).
double largest_loss
Single largest |pnl| among losing trades (positive magnitude).
double avg_bars_in_wins
Mean bar duration of winning trades, inclusive of the entry bar (TV convention, validated 2026-06-12)...
double avg_bars_in_losses
Mean bar duration of losing trades, inclusive of the entry bar (TV convention, validated 2026-06-12).
double profit_factor
gross_profit / gross_loss.
double avg_loss
gross_loss / num_losses (positive magnitude).
double largest_win
Single largest pnl among winning trades.
double avg_bars_in_trade
Mean of (exit_bar_index - entry_bar_index + 1) in SCRIPT bars, over all trades — inclusive of the ent...
double gross_loss_pct
gross_loss as a percent of initial capital (0-100 scale).
int32_t max_consecutive_wins
Longest winning run; even trades reset both streaks.
double expectancy
(num_wins/num_trades)*avg_win - (num_losses/num_trades)*avg_loss, account currency per trade.
double net_profit
Sum of pnl (account currency, net of commission).
int32_t num_trades
Closed trades in this block (all / long-only / short-only).
double avg_trade_pct
Mean of per-trade pnl_pct over all trades.
double ratio_avg_win_avg_loss
avg_win / avg_loss.
double avg_trade
net_profit / num_trades.
int32_t num_even
Trades with pnl == 0.0 exactly; breaks both win and loss streaks; excluded from win/loss averages.
int32_t num_wins
Trades with pnl > 0.
Closed-trade record returned in pf_report_t::trades.
int32_t exit_bar_index
Script-bar index of the exit fill (0-based).
double pnl_pct
Net return-on-cost in percent: pnl (NET of commission) / entry cost (entry_price * qty * pointvalue) ...
double exit_price
Exit fill price (incl.
int32_t entry_bar_index
Script-bar index of the entry fill (0-based).
double commission
Entry+exit commission actually deducted from pnl (account currency).
double pnl
Net realized PnL in account currency (commission-inclusive).
int is_long
1 if long, 0 if short.
double max_drawdown
Peak adverse price travel during the trade ($/unit qty).
double qty
Filled quantity.
double max_runup
Peak favorable price travel during the trade ($/unit qty).
int64_t entry_time
Entry fill time (Unix ms).
double entry_price
Entry fill price (incl.
int64_t exit_time
Exit fill time (Unix ms).
Runtime version descriptor returned by pf_version_get.
const char * commit_sha
Short git commit SHA, or "" if unknown.