PineForge unreleased
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
native_calc_point.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5// The calculation points of a driving-event schedule: what the kernel derives,
6// for an instance that computes on a declared cadence, from the events it is
7// given and its own bar state (src/native_calc_plan.hpp holds the derivation).
8// Plain data only, so the boundary that exposes a plan and the code that checks
9// one read the same vocabulary.
10
12
13/// The driving events of a schedule. The numbers are the event-kind registry of
14/// the named-stream boundary; zero, and every value above AdvanceTime, name no
15/// event: the derivation refuses them rather than plan nothing for them.
16enum class EventKind : std::uint32_t {
18 Bar = 2,
19 Tick = 3,
21};
22
23/// Whether `kind` is in the registry: the one range check a raw number passes
24/// before the derivation treats it as an EventKind.
25constexpr bool is_known(EventKind kind) noexcept {
26 const auto raw = static_cast<std::uint32_t>(kind);
27 return raw >= static_cast<std::uint32_t>(EventKind::WarmupBar)
28 && raw <= static_cast<std::uint32_t>(EventKind::AdvanceTime);
29}
30
31/// One driving event: its kind and its time. `time_ms` is a tick's time, a
32/// bar's open time, or the instant a time advance moves the clock to.
33struct Event {
35 std::int64_t time_ms = 0;
36};
37
38/// Which driving events make an instance calculate, as a bit set. A cadence
39/// always contains kCadenceBarClose; the other two bits are optional.
40inline constexpr std::uint32_t kCadenceAdvanceTime = 1u << 0;
41inline constexpr std::uint32_t kCadenceBarClose = 1u << 1;
42inline constexpr std::uint32_t kCadenceTick = 1u << 2;
43inline constexpr std::uint32_t kCadenceMask =
45
46/// Whether a calculation ran before the payload of its driving event was
47/// applied (a bar closed only because time reached its end) or after it.
48enum class Phase : std::uint32_t {
49 Pre = 0,
50 Post = 1,
51};
52
53/// Provisional for an intrabar calculation, Confirmed for the calculation that
54/// closes a bar.
55enum class Finality : std::uint32_t {
58};
59
60/// One planned calculation.
61struct Point {
62 /// Zero-based index of the driving event among the events of the schedule.
63 std::uint64_t event_index = 0;
64 /// Trace ordinal of the driving event, one-based and independent of how the
65 /// trace is cut into schedules.
66 std::uint64_t event = 0;
67 /// Ordinal of the bar lifecycle the calculation belongs to, assigned by the
68 /// kernel: zero for the first lifecycle opened, one more for each next.
69 std::uint64_t bar_key = 0;
70 /// Ordinal of the calculation within its bar lifecycle.
71 std::uint64_t calc_ordinal = 0;
72 /// Ordinal of the calculation within its driving event.
73 std::uint64_t calc = 0;
74 /// Event-time position: the bar's exclusive end (never later than the event
75 /// time) for a Pre calculation, the event time itself for a Post one.
76 std::int64_t at_ms = 0;
79};
80
81/// How a derivation ended. Ok with a count of zero is a schedule that plans no
82/// calculation; every other status is a refusal and plans nothing.
83enum class Status : std::uint32_t {
84 Ok = 0,
85 /// The consumer's schedule group was not enabled for the current run.
87 /// A null event array with a nonzero count.
89 /// A cadence without the bar-close bit, or with a bit outside the set.
91 /// An event kind outside the registry.
93 /// A bar lifecycle had to open while the last ordinal assigned was already
94 /// the largest representable one: the boundary reports capacity exhausted.
96 /// The trace ordinal of the next event is the largest representable one.
98 /// The timebase has no bar interval at the time of an event that opens one.
100 /// The interval the timebase holds for an event ends no later than the
101 /// event: its end cannot be the exclusive end of a bar that event opens.
103 /// A position the kernel planned itself lies outside the window of its
104 /// driving event: an internal invariant failure, not an admission error.
106};
107
108} // namespace pineforge::native_calc
Phase
Whether a calculation ran before the payload of its driving event was applied (a bar closed only beca...
EventKind
The driving events of a schedule.
constexpr bool is_known(EventKind kind) noexcept
Whether kind is in the registry: the one range check a raw number passes before the derivation treats...
constexpr std::uint32_t kCadenceMask
constexpr std::uint32_t kCadenceBarClose
constexpr std::uint32_t kCadenceTick
constexpr std::uint32_t kCadenceAdvanceTime
Which driving events make an instance calculate, as a bit set.
Status
How a derivation ended.
@ BadCadence
A cadence without the bar-close bit, or with a bit outside the set.
@ BarEndNotAfterEvent
The interval the timebase holds for an event ends no later than the event: its end cannot be the excl...
@ NoBarSlot
The timebase has no bar interval at the time of an event that opens one.
@ BarKeyExhausted
A bar lifecycle had to open while the last ordinal assigned was already the largest representable one...
@ BadArguments
A null event array with a nonzero count.
@ UnknownEventKind
An event kind outside the registry.
@ PositionOutsideWindow
A position the kernel planned itself lies outside the window of its driving event: an internal invari...
@ TraceExhausted
The trace ordinal of the next event is the largest representable one.
@ NotEngaged
The consumer's schedule group was not enabled for the current run.
Finality
Provisional for an intrabar calculation, Confirmed for the calculation that closes a bar.
One driving event: its kind and its time.
std::uint64_t bar_key
Ordinal of the bar lifecycle the calculation belongs to, assigned by the kernel: zero for the first l...
std::int64_t at_ms
Event-time position: the bar's exclusive end (never later than the event time) for a Pre calculation,...
std::uint64_t calc_ordinal
Ordinal of the calculation within its bar lifecycle.
std::uint64_t event_index
Zero-based index of the driving event among the events of the schedule.
std::uint64_t calc
Ordinal of the calculation within its driving event.