PineForge v0.13.1-379-g9b50973
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
order_priority.cpp
Go to the documentation of this file.
2
3#include <limits>
4
6namespace {
7
8bool finite(double value) noexcept { return std::isfinite(value); }
9bool absent(double value) noexcept { return std::isnan(value); }
10
11} // namespace
12
13std::optional<OrderPriorityDecision> OrderPriority::select(
14 const OrderPriorityContext& ctx,
15 const std::vector<OrderPriorityCandidate>& candidates) const {
16 if (!attached_ || !retained_parent_first_ || !ctx.broker_flat
19 || ctx.stream_warmup_mode || !ctx.stream_idle || candidates.size() != 2) {
20 return std::nullopt;
21 }
22
23 const OrderPriorityCandidate* parent = nullptr;
24 const OrderPriorityCandidate* child = nullptr;
25 for (const auto& candidate : candidates) {
26 if (candidate.kind == OrderPriorityKind::Entry) parent = &candidate;
27 else if (candidate.kind == OrderPriorityKind::Exit) child = &candidate;
28 }
29 if (!parent || !child) return std::nullopt;
30
31 const bool exact_parent = parent->created_flat && parent->predecessor == 0
37 && parent->created_bar == ctx.bar_index - 1
38 && parent->default_quantity && !parent->birth_from_fill
39 && !parent->prior_close && !parent->at_entry_capacity
40 && !parent->stop_limit_activated && finite(parent->stop) && absent(parent->limit)
41 && absent(parent->trail_points) && absent(parent->trail_price)
42 && absent(parent->trail_offset) && parent->oca_name.empty() && parent->oca_type == 0;
43 const double child_percent = absent(child->qty_percent) ? 100.0 : child->qty_percent;
44 const bool exact_child = !child->from_entry.empty()
45 && child->predecessor == parent->named_cancel_surviving_exit
46 && child->created_flat && child->created_bar == ctx.bar_index - 1
47 && !child->birth_from_fill && !child->prior_close && !child->at_entry_capacity
48 && absent(child->requested_qty) && child_percent >= 100.0 - 1e-9
49 && finite(child->stop) && finite(child->limit)
50 && absent(child->profit_ticks) && absent(child->loss_ticks)
51 && absent(child->trail_points) && absent(child->trail_price)
52 && absent(child->trail_offset) && child->oca_name.empty() && child->oca_type == 0;
53 if (!exact_parent || !exact_child || child->from_entry != parent->id
54 || child->source_sequence >= parent->source_sequence
55 || parent->handle.incarnation == std::numeric_limits<std::uint64_t>::max()
56 || child->handle.incarnation != parent->handle.incarnation + 1) {
57 return std::nullopt;
58 }
59 return OrderPriorityDecision{parent->handle, child->handle};
60}
61
62} // namespace pineforge::compat::pine
std::optional< OrderPriorityDecision > select(const OrderPriorityContext &ctx, const std::vector< OrderPriorityCandidate > &candidates) const
bool finite(double value) noexcept