PineForge v0.13.1-379-g9b50973
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1#pragma once
2#include "na.hpp"
3#include "window_sum.hpp"
4#include <cstdint>
5#include <deque>
6
7namespace pineforge {
8
9inline double deterministic_random(double lo, uint32_t call_site, double hi, uint32_t seed, int bar_index) {
10 // SplitMix64-style deterministic mixer. This is a PineForge PRNG contract:
11 // stable across platforms/runs, intentionally not a TradingView PRNG clone.
12 uint64_t x = static_cast<uint64_t>(seed);
13 x ^= (static_cast<uint64_t>(call_site) + 0x9e3779b97f4a7c15ULL);
14 x ^= (static_cast<uint64_t>(bar_index) + 1ULL) * 0xbf58476d1ce4e5b9ULL;
15 x += 0x9e3779b97f4a7c15ULL;
16 x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
17 x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
18 x ^= (x >> 31);
19 double u = static_cast<double>(x >> 11) * (1.0 / 9007199254740992.0);
20 return lo + u * (hi - lo);
21}
22
23// Deprecated spelling of deterministic_random, kept for generated code.
24inline double pine_random(double lo, uint32_t call_site, double hi, uint32_t seed,
25 int bar_index) {
26 return deterministic_random(lo, call_site, hi, seed, bar_index);
27}
28
29namespace math {
30
31/// Rolling sum over the last `length` non-na sources for PineScript
32/// `math.sum(source, length)` (not one of the `ta` builtins). Returns na
33/// until seeded, then holds the seeded sum on na-input bars.
34class Sum {
35 KahanWindowSum window_; // TradingView's sliding-window arithmetic (window_sum.hpp)
36 int length_;
37
38public:
39 explicit Sum(int length);
40 double compute(double src);
41 double recompute(double src);
42};
43
44} // namespace math
45
46} // namespace pineforge
double compute(double src)
double recompute(double src)
double deterministic_random(double lo, uint32_t call_site, double hi, uint32_t seed, int bar_index)
Definition math.hpp:9
double pine_random(double lo, uint32_t call_site, double hi, uint32_t seed, int bar_index)
Definition math.hpp:24