PineForge unreleased
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
pine_position_tail_binding.hpp
Go to the documentation of this file.
1#pragma once
2
3// All L8c / L6b-4 binding lives here. Reconcile this file when those lanes
4// land; the footer codec and percentage-exit predicate have no dependency
5// on their implementation details.
7#include "../report_scratch.hpp"
8
11
12#include <limits>
13#include <stdexcept>
14
16
18 // L6b-4: empty directory uses disk-backed TMPDIR or /tmp, then /var/tmp.
19 class File {
20 public:
21 explicit File(const std::string& directory)
22 : file_(directory, pineforge::detail::ScratchStore::position_tail) {}
23 int descriptor() const noexcept { return file_.descriptor(); }
24 void write(std::uint64_t offset, const void* bytes, std::size_t count) {
25 file_.write(static_cast<std::int64_t>(offset), bytes, count);
26 }
27 void read(std::uint64_t offset, void* bytes, std::size_t count) const {
28 file_.read(static_cast<std::int64_t>(offset), bytes, count);
29 }
30 void clear() { file_.clear(); }
31 void release() noexcept { file_.release(); }
32 private:
33 report_sink::ScratchFile file_;
34 };
35
36 // L8c: call once on the approved resident prefix BEFORE trades.seal(end).
37 // A failure propagates before any row can be discarded.
38 static void preserve(std::shared_ptr<PositionTailSpill>& spill,
39 const TradeLedger& trades, std::size_t end,
40 std::int64_t position_open, const std::string& directory) {
41 if (end < trades.base() || end > trades.size()) {
43 "invalid Pine position-tail seal prefix");
44 }
45 if (position_open == std::numeric_limits<std::int64_t>::min()) {
46 spill.reset();
47 return;
48 }
49 for (std::size_t i = trades.base(); i < end; ++i) {
50 const Trade& trade = trades[i];
51 if (trade.exit_time < position_open) {
52 // A scan barrier cuts off all older rows even when exit
53 // times are not monotone in global trade-row order.
54 spill.reset();
55 } else {
56 if (!spill) spill = std::make_shared<PositionTailSpill>(directory);
57 spill->append(trade);
58 }
59 }
60 }
61};
62
63} // namespace pineforge::source::detail
Base with a code: throw coded<std::invalid_argument>(code, {args}, text).
void read(std::uint64_t offset, void *bytes, std::size_t count) const
void write(std::uint64_t offset, const void *bytes, std::size_t count)
int64_t exit_time
Definition engine.hpp:183
static void preserve(std::shared_ptr< PositionTailSpill > &spill, const TradeLedger &trades, std::size_t end, std::int64_t position_open, const std::string &directory)