87 "The script attempts to reference historical data that is too far from "
88 "the current bar (" + (beyond_uint64 ?
"more than " :
"") + std::to_string(offset) +
89 " bars back). The historical buffer's limit is " + std::to_string(maximum) +
" bars.";
96 std::int64_t maximum) {
99 if (length >= 0x1p64) {
100 constexpr auto last = std::numeric_limits<std::uint64_t>::max();
104 const auto requested =
static_cast<std::uint64_t
>(length);
105 if (maximum != 0 && requested - 1 >
static_cast<std::uint64_t
>(maximum))
113 return length >
static_cast<std::uint64_t
>(INT_MAX) ? INT_MAX :
static_cast<int>(length);
120 constexpr std::size_t slots = std::numeric_limits<std::size_t>::max() /
sizeof(double);
121 if (maximum <= 0 ||
static_cast<std::uint64_t
>(maximum) >= slots - 2)
return 0;
122 return static_cast<std::size_t
>(maximum) + 2;
136 std::size_t
size()
const {
return stored_; }
138 if (keep_ != 0 && stored_ == keep_) {
139 oldest_ = oldest_ + 1 == values_.size() ? 0 : oldest_ + 1;
142 if (stored_ == values_.size()) grow();
143 values_.write(slot(stored_), value);
149 void replace_back(
double value) { values_.write(slot(stored_ - 1), value); }
152 double from_back(std::size_t back)
const {
return values_.read(slot(stored_ - 1 - back)); }
154 values_.observe_owned_storage(out);
160 std::size_t slot(std::size_t index)
const {
161 const std::size_t remaining = values_.
size() - oldest_;
162 return index >= remaining ? index - remaining : oldest_ + index;
166 constexpr std::size_t most = std::numeric_limits<std::size_t>::max() /
sizeof(double);
167 const std::size_t held = values_.
size();
168 std::size_t capacity = held == 0 ?
Values::kChunk : (held > most / 2 ? most : 2 * held);
170 if (capacity <= held) capacity = held + 1;
171 if (keep_ != 0 && capacity > keep_) capacity = keep_;
173 for (std::size_t index = 0; index < stored_; ++index)
174 next.write(index, values_.
read(slot(index)));
175 values_ = std::move(next);
181 std::size_t oldest_ = 0;
182 std::size_t stored_ = 0;
187template <
class Window>
188void build_window(std::optional<Window>& window,
int length,
bool option) {
189 if constexpr (std::is_constructible_v<Window, int, bool>) {
190 window.emplace(length, option);
192 static_cast<void>(option);
193 window.emplace(length);
206 window_.note_no_push();
207 return window_.seeded() ? window_.sum() :
na<double>();
209 const double sum = window_.push(src);
215 window_.note_no_push();
216 return window_.seeded() ? window_.sum() :
na<double>();
218 const double sum = window_.repush(src);
222 window_.observe_owned_storage(out);
234template <
class Window>
242 double update(
const char* function,
double source,
double length,
bool option,
244 const std::uint64_t requested =
checked_length(length, function, max_lookback_);
245 const bool again = replace && called_;
246 if (again && pushed_) history_.pop_back();
248 if (push) history_.append(
source);
251 if (requested > history_.size()) {
255 const bool carried = window_.has_value() && !(again && built_) &&
256 window_length_ == requested && window_option_ == option;
259 return again ? window_->recompute(
source) : window_->compute(
source);
263 window_length_ = requested;
264 window_option_ = option;
266 const std::size_t newest = push ? 1 : 0;
267 for (std::size_t earlier =
static_cast<std::size_t
>(requested) - newest; earlier != 0;
269 window_->compute(history_.from_back(newest + earlier - 1));
270 return window_->compute(
source);
279 std::optional<Window> window_;
280 std::uint64_t window_length_ = 0;
281 bool window_option_ =
false;
282 bool called_ =
false;
283 bool pushed_ =
false;
292template <
class Window>
300 template <
class Step>
301 double update(
const char* function,
double source,
double length, std::size_t extra,
302 bool replace, Step step) {
303 const std::uint64_t requested =
checked_length(length, function, max_lookback_);
304 const bool again = replace && called_;
305 if (again) history_.replace_back(
source);
306 else history_.append(
source);
308 const std::size_t held = history_.size();
309 if (held < extra || requested > held - extra) {
313 if (window_ && window_length_ == requested)
return step(*window_,
source, again);
315 window_length_ = requested;
316 for (std::size_t back =
static_cast<std::size_t
>(requested) + extra - 1; back != 0; --back)
317 step(*window_, history_.from_back(back),
false);
318 return step(*window_,
source,
false);
327 std::optional<Window> window_;
328 std::uint64_t window_length_ = 0;
329 bool called_ =
false;
342 return core_.update(
"sum",
source, length,
false,
false);
345 return core_.update(
"sum",
source, length,
false,
true);
348 core_.observe_owned_storage(out);
365 return core_.update(
"stdev",
source, length, biased,
false);
369 return core_.update(
"stdev",
source, length, biased,
true);
372 core_.observe_owned_storage(out);
390 return core_.update(
"variance",
source, length, biased,
false);
394 return core_.update(
"variance",
source, length, biased,
true);
397 core_.observe_owned_storage(out);
412 return core_.update(
"dev",
source, length,
false,
false);
415 return core_.update(
"dev",
source, length,
false,
true);
418 core_.observe_owned_storage(out);
433 return core_.update(
"median",
source, length,
false,
false);
436 return core_.update(
"median",
source, length,
false,
true);
439 core_.observe_owned_storage(out);
454 return core_.update(
"mode",
source, length,
false,
false);
457 return core_.update(
"mode",
source, length,
false,
true);
460 core_.observe_owned_storage(out);
477 const double high = highest_.compute(
source, length);
478 const double low = lowest_.compute(
source, length);
484 const double high = highest_.recompute(
source, length);
485 const double low = lowest_.recompute(
source, length);
490 highest_.observe_owned_storage(out);
491 lowest_.observe_owned_storage(out);
511 core_.observe_owned_storage(out);
515 double update(
double source,
double length,
bool replace) {
516 return core_.update(
"percentrank",
source, length, 1, replace,
522 series_stats_detail::EveryCall<ta::PercentRank> core_;
534 return update(
source, percentage, length,
false);
537 return update(
source, percentage, length,
true);
540 core_.observe_owned_storage(out);
544 double update(
double source,
double percentage,
double length,
bool replace) {
548 "percentile_linear_interpolation",
source, length, 0, replace,
550 return again ? window.
recompute(src, percentage)
551 : window.
compute(src, percentage);
555 series_stats_detail::EveryCall<ta::PercentileLinearInterpolation> core_;
568 double compute(
double source1,
double source2,
double length) {
569 return update(source1, source2, length,
false);
571 double recompute(
double source1,
double source2,
double length) {
572 return update(source1, source2, length,
true);
579 double update(
double source1,
double source2,
double length,
bool replace) {
580 const std::uint64_t requested =
582 const bool again = replace && called_;
591 if (requested > first_.
size()) {
596 if (window_ && window_length_ == requested)
597 return again ? window_->recompute(source1, source2)
598 : window_->compute(source1, source2);
601 for (std::size_t back =
static_cast<std::size_t
>(requested) - 1; back != 0; --back)
602 window_->compute(first_.from_back(back), second_.from_back(back));
603 return window_->compute(source1, source2);
607 series_stats_detail::Inputs first_;
608 series_stats_detail::Inputs second_;
609 std::optional<ta::Correlation> window_;
610 std::uint64_t window_length_ = 0;
611 bool called_ =
false;
618#define PINEFORGE_HAS_PINE_SERIES_STDEV 1
619#define PINEFORGE_HAS_PINE_SERIES_VARIANCE 1
620#define PINEFORGE_HAS_PINE_SERIES_DEV 1
621#define PINEFORGE_HAS_PINE_SERIES_RANGE 1
622#define PINEFORGE_HAS_PINE_SERIES_MEDIAN 1
623#define PINEFORGE_HAS_PINE_SERIES_MODE 1
624#define PINEFORGE_HAS_PINE_SERIES_PERCENTRANK 1
625#define PINEFORGE_HAS_PINE_SERIES_PERCENTILELINEAR 1
626#define PINEFORGE_HAS_PINE_SERIES_CORRELATION 1
627#define PINEFORGE_HAS_PINE_SERIES_MATHSUM 1
630#define PINEFORGE_HAS_PINE_SERIES_PERCENTILE_LINEAR 1
631#define PINEFORGE_HAS_PINE_SERIES_MATH_SUM 1
WindowCacheCapacity cache_capacity() const
static constexpr std::size_t kChunk
T read(std::size_t index) const
double recompute(double source1, double source2, double length)
SeriesCorrelation & operator=(const SeriesCorrelation &)=default
double compute(double source1, double source2, double length)
SeriesCorrelation(const SeriesCorrelation &)=default
SeriesCorrelation(WindowCacheCapacity cache)
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesDev & operator=(const SeriesDev &)=default
double recompute(double source, double length)
double compute(double source, double length)
SeriesDev(const SeriesDev &)=default
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesDev(WindowCacheCapacity cache)
SeriesMathSum & operator=(const SeriesMathSum &)=default
double compute(double source, double length)
SeriesMathSum(WindowCacheCapacity cache)
SeriesMathSum(const SeriesMathSum &)=default
double recompute(double source, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesMode(WindowCacheCapacity cache)
SeriesMode(const SeriesMode &)=default
double recompute(double source, double length)
double compute(double source, double length)
SeriesMode & operator=(const SeriesMode &)=default
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double source, double percentage, double length)
SeriesPercentileLinear & operator=(const SeriesPercentileLinear &)=default
SeriesPercentileLinear(const SeriesPercentileLinear &)=default
double recompute(double source, double percentage, double length)
SeriesPercentileLinear(WindowCacheCapacity cache)
SeriesPercentrank(const SeriesPercentrank &)=default
double compute(double source, double length)
double recompute(double source, double length)
SeriesPercentrank(WindowCacheCapacity cache)
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesPercentrank & operator=(const SeriesPercentrank &)=default
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double source, double length)
double recompute(double source, double length)
SeriesRange(const SeriesRange &)=default
SeriesRange & operator=(const SeriesRange &)=default
SeriesRange(WindowCacheCapacity cache)
SeriesStdev & operator=(const SeriesStdev &)=default
SeriesStdev(const SeriesStdev &)=default
SeriesStdev(WindowCacheCapacity cache)
double recompute(double source, bool biased, double length)
double recompute(double source, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double source, double length)
double compute(double source, bool biased, double length)
double recompute(double source, bool biased, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesVariance(const SeriesVariance &)=default
SeriesVariance & operator=(const SeriesVariance &)=default
SeriesVariance(WindowCacheCapacity cache)
double compute(double source, double length)
double compute(double source, bool biased, double length)
double recompute(double source, double length)
EveryCall(WindowCacheCapacity cache)
EveryCall & operator=(const EveryCall &)=default
void observe_owned_storage(OwnedStorage &out) const noexcept
double update(const char *function, double source, double length, std::size_t extra, bool replace, Step step)
EveryCall(const EveryCall &)=default
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double src)
double recompute(double src)
ValidSources & operator=(const ValidSources &)=default
ValidSources(const ValidSources &)=default
ValidSources(WindowCacheCapacity cache)
double update(const char *function, double source, double length, bool option, bool replace)
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double src)
double recompute(double src)
double recompute(double src, double percentage)
double compute(double src, double percentage)
std::int64_t ta_lookback_limit() noexcept
std::size_t retained_inputs(std::int64_t maximum)
std::uint64_t checked_length(double length, const char *function, std::int64_t maximum)
void build_window(std::optional< Window > &window, int length, bool option)
void stop_for_lookback(std::uint64_t offset, std::int64_t maximum, bool beyond_uint64)
void stop_for_length(double value, const char *function)
int fixed_length(std::uint64_t length)
int simple_ta_length(double value, const char *function, const char *argument)
void pine_limit_stop(const char *limit, std::int64_t max, const char *english)
bool is_na(const Line &h)
WindowCacheCapacity & window_cache_capacity()
std::string run_error_bar_prefix()
Stops using TradingView's text prepend "Error on bar N: " when a bar context is installed,...
void pine_engine_invariant(const char *english)
void observe_owned_members(OwnedStorage &out, const T &... values) noexcept