PineForge v0.13.1-379-g9b50973
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
execution.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "order_action.hpp"
5#include <cstddef>
6#include <cstdint>
7#include <optional>
8#include <string>
9#include <variant>
10#include <vector>
11
13
14// Whole-book reduction is explicit: it never relies on a rounded aggregate
15// quantity being exactly equal to the sum of individual physical lots.
16struct Flatten {};
17using Action = std::variant<Flatten, order_action::Reduce, order_action::Transact>;
18
19// Why a closed row exited, as a value the closer records rather than a string
20// a reader decodes. The numbers ARE the public C contract
21// (strategy_closed_trade_close_cause, pineforge.h), so they are append-only:
22// 0 is the documented "no cause recorded" value, never an error.
23//
24// Unspecified leaves the classification to the generic facts already on the
25// row (open_at_end -> RangeEnd, exit_from_bracket -> Bracket, otherwise
26// Script). Liquidation and RiskLimit are what a kernel-originated request
27// carries (native_order::RequestOrigin::KernelLiquidation / KernelRisk); a
28// host that runs its own forced-close policy — the Pine adapter's margin call,
29// its strategy.risk.max_intraday_loss close and its filled-order cap close —
30// records its own cause on the row it produced.
31enum class CloseCause : std::uint8_t {
33 Script = 1,
39};
40
41// The caller has already matched/admitted the action and resolved its price.
42// Settlement does not apply another lot step, price grid, slippage or entry cap.
43struct Fill {
44 double price;
45 std::string id;
46 std::string comment;
47 uint64_t incarnation = 0;
48 // An observed execution charge in account currency, including rebates.
49 // Otherwise the engine's configured schedule quotes the current fill.
50 std::optional<double> commission_account = std::nullopt;
51 // Appended last so every existing aggregate initializer keeps its meaning.
53};
54
55enum class Status {
58 // Caller-supplied lifecycle targets/revisions/operations that cannot be
59 // applied to the current pending book. Not durable engine state.
61 // Invalid or unavailable run-local opening exposure on a scoped close.
62 // Existing whole-book entry points do not return this status.
64};
65
66struct Result {
68 // Aggregate projections of the physical effects; the lot/trade roster is
69 // authoritative. Opening units are signed; closing units are nonnegative.
70 double closed_units = 0.0;
71 double opened_units = 0.0;
72 // This execution's current ticket and committed-row references. Zero when
73 // the result is not Applied. Native terminal events copy these facts.
74 double current_ticket = 0.0;
75 std::size_t first_trade_index = 0;
76 std::size_t closed_trade_count = 0;
78};
79
80// Stack-bound physical coordinates for one settlement. Native supplies time
81// and index from the matching point and leaves both optionals empty. Legacy
82// callers copy current_bar_/bar_index_ and the current fold flags.
84 int64_t effective_time_ms = 0;
86 std::optional<bool> preceding_exit_path_prefix;
87 std::optional<double> preceding_exit_trail_peak;
88};
89
90// Stack-local inspect facts. Destroyed at the end of one matching step.
93 double closed_units = 0.0;
94 double opened_units = 0.0;
95 double resulting_abs_units = 0.0;
96 std::size_t resulting_lot_count = 0;
98 double current_ticket = 0.0;
99 bool would_open = false;
100 bool incoming_short = false;
101};
102
103// One pre-close operation on an exact pending identity. created_seq 0 and
104// Target{0,0} are actual expected values, not wildcards. The coordinator
105// allocates the batch cause and does not rewrite operation payloads.
113
114// Engaging this batch allocates one observation frame on a successful
115// commit even when operations is empty. Empty optional means no allocation.
116// Phase must be a valid exit_legs::Phase even for an empty operations list.
118 exit_legs::Phase phase = exit_legs::Phase::Observation;
119 std::vector<LifecycleIntent> operations;
120};
121
122// Exact pending EXIT to remove after old-cycle unbind and before any
123// quoted opening bind. Fields are the initial snapshot before this
124// execution's own effects; incarnation 0 is refused.
126 uint64_t incarnation = 0;
127 int64_t created_seq = 0;
129 uint64_t expected_revision = 0;
130};
131
132// Transient, stack-local effects for one native settlement preparation. Not
133// stored, hashed, replayed, or reusable execution authority.
135 std::optional<LifecycleBatch> pre_close;
136 std::vector<PendingRemoval> removals;
137};
138
139} // namespace pineforge::execution
std::variant< Flatten, order_action::Reduce, order_action::Transact > Action
Definition execution.hpp:17
std::variant< BindOwner, Suspend, StageReplacement, CancelDeferredActivation, Restore, CompleteBarrier, Observe, Cancel > Operation
std::optional< double > commission_account
Definition execution.hpp:50
std::vector< LifecycleIntent > operations
std::vector< PendingRemoval > removals
std::optional< LifecycleBatch > pre_close
std::optional< double > preceding_exit_trail_peak
Definition execution.hpp:87