79 std::snprintf(text,
sizeof text,
80 "%sThe script attempts to reference historical data that is too far from "
81 "the current bar (%s%llu bars back). The historical buffer's limit is "
84 static_cast<unsigned long long>(offset),
static_cast<long long>(maximum));
91inline std::uint64_t
checked_length(
double current_length,
const char* function,
92 std::int64_t maximum) {
93 if (!std::isfinite(current_length)) {
97 if (!(current_length >= 1.0)) {
101 constexpr std::uint64_t last = std::numeric_limits<std::uint64_t>::max();
102 if (current_length >= 0x1p64) {
107 const std::uint64_t requested =
static_cast<std::uint64_t
>(current_length);
108 if (maximum != 0 && requested - 1 >
static_cast<std::uint64_t
>(maximum))
113constexpr std::size_t
kMaxSamples = std::numeric_limits<std::size_t>::max() /
sizeof(double);
120 if (maximum == 0 ||
static_cast<std::uint64_t
>(maximum) >=
kMaxSamples)
return 0;
121 return static_cast<std::size_t
>(maximum) + 1;
133 std::size_t
count()
const {
return count_; }
137 return values_.read(back <= head_ ? head_ - back : values_.size() - (back - head_));
140 void append(
double value, std::size_t limit) {
141 const bool full = limit != 0 && count_ == limit;
142 if (!full) ensure_room(limit);
143 values_.write(head_, value);
144 const std::size_t next = head_ + 1;
145 head_ = next == limit ? 0 : next;
149 values_.observe_owned_storage(out);
153 void ensure_room(std::size_t limit) {
154 const std::size_t size = values_.
size();
155 if (count_ < size)
return;
158 {{
"resource",
"layout"}},
"history byte extent is not representable");
161 if (limit != 0 && capacity > limit) capacity = limit;
166 for (std::size_t i = 0; i < count_; ++i) grown.write(i, values_.
read(i));
167 values_ = std::move(grown);
171 std::size_t head_ = 0;
172 std::size_t count_ = 0;
178template <
class Window>
181 using Fixed =
typename Window::Fixed;
189 std::uint64_t
checked(
double current_length,
const char* function)
const {
197 template <
class Call>
198 auto call(
double sample, std::uint64_t length,
const Arguments& arguments,
bool again,
199 Call&& on_fixed) ->
decltype(on_fixed(std::declval<Fixed&>(),
false)) {
200 const bool replace = again && called_;
201 const bool records = Window::kRecordsNa || !
is_na(sample);
202 if (!replace && pending_recorded_) {
205 pending_recorded_ =
false;
210 Unfinished unfinished(fixed_);
211 auto result = fixed_ && fixed_length_ == length && fixed_arguments_ == arguments
212 ? on_fixed(*fixed_, replace)
213 : rebuilt(records, length, arguments, on_fixed);
214 unfinished.drop =
false;
216 pending_recorded_ = records;
221 samples_.observe_owned_storage(out);
228 explicit Unfinished(std::optional<Fixed>&
object) : fixed(object) {}
230 if (drop) fixed.reset();
232 Unfinished(
const Unfinished&) =
delete;
233 Unfinished& operator=(
const Unfinished&) =
delete;
234 std::optional<Fixed>& fixed;
240 template <
class Call>
241 auto rebuilt(
bool records, std::uint64_t length,
const Arguments& arguments,
242 Call& on_fixed) ->
decltype(on_fixed(std::declval<Fixed&>(),
false)) {
244 const std::size_t held = samples_.
count();
246 if (length >
static_cast<std::uint64_t
>(held) + (records ? 1u : 0u))
247 return Window::missing();
248 if (length >
static_cast<std::uint64_t
>(std::numeric_limits<int>::max()))
250 {{
"resource",
"layout"}},
"window length is not representable");
251 Window::build(fixed_,
static_cast<int>(length), arguments, cache_);
252 fixed_length_ = length;
253 fixed_arguments_ = arguments;
254 std::size_t back = held < length ? held : static_cast<std::size_t>(length);
255 for (; back != 0; --back) Window::feed(*fixed_, samples_.newest(back));
256 return on_fixed(*fixed_,
false);
259 RecordedSamples samples_;
260 WindowCacheCapacity cache_;
261 std::optional<Fixed> fixed_;
262 std::uint64_t fixed_length_ = 0;
265 bool pending_recorded_ =
false;
266 bool called_ =
false;
280 out.emplace(length, cache);
310 static void build(std::optional<Fixed>& out,
int length,
const Arguments& arguments,
343 static void build(std::optional<Fixed>& out,
int length,
const Arguments& arguments,
345 out.emplace(length, arguments.
mult);
361 static void build(std::optional<Fixed>& out,
int length,
const Arguments& arguments,
363 out.emplace(length, arguments.
mult);
379 return call(
source, current_length,
false);
382 return call(
source, current_length,
true);
385 window_.observe_owned_storage(out);
389 double call(
double source,
double current_length,
bool again) {
390 const std::uint64_t length = window_.checked(current_length,
"wma");
392 return replace ? fixed.recompute(
source) : fixed.compute(
source);
396 series_ma_detail::Rewindowed<series_ma_detail::WmaWindow> window_;
414 return call(
source, volume, current_length,
false);
417 return call(
source, volume, current_length,
true);
420 product_.observe_owned_storage(out);
421 volume_.observe_owned_storage(out);
427 static double mean(Mean& window,
double sample, std::uint64_t length,
bool again) {
428 return window.
call(sample, length, {}, again, [sample](
ta::SMA& fixed,
bool replace) {
429 return replace ? fixed.recompute(sample) : fixed.compute(sample);
432 double call(
double source,
double volume,
double current_length,
bool again) {
433 const std::uint64_t length = product_.checked(current_length,
"vwma");
434 const double product_mean = mean(product_, source * volume, length, again);
435 const double volume_mean = mean(volume_, volume, length, again);
437 return product_mean / volume_mean;
454 double compute(
double source,
double offset,
double sigma,
double current_length) {
455 return call(
source, offset, sigma,
false, current_length,
false);
458 double current_length) {
459 return call(
source, offset, sigma, floor, current_length,
false);
462 return call(
source, offset, sigma,
false, current_length,
true);
465 double current_length) {
466 return call(
source, offset, sigma, floor, current_length,
true);
469 window_.observe_owned_storage(out);
473 double call(
double source,
double offset,
double sigma,
bool floor,
double current_length,
475 const std::uint64_t length = window_.checked(current_length,
"alma");
477 arguments.
offset = offset;
478 arguments.
sigma = sigma;
479 arguments.
floor = floor;
480 return window_.call(
source, length, arguments, again,
486 series_ma_detail::Rewindowed<series_ma_detail::AlmaWindow> window_;
498 return call(
source, offset, current_length,
false);
501 return call(
source, offset, current_length,
true);
504 window_.observe_owned_storage(out);
508 double call(
double source,
double offset,
double current_length,
bool again) {
509 const std::uint64_t length = window_.checked(current_length,
"linreg");
510 return window_.call(
source, length, {}, again,
512 return replace ? fixed.recompute(
source, offset)
513 : fixed.compute(
source, offset);
517 series_ma_detail::Rewindowed<series_ma_detail::LinregWindow> window_;
531 return call(
source, mult, current_length,
false);
534 return call(
source, mult, current_length,
true);
537 window_.observe_owned_storage(out);
542 const std::uint64_t length = window_.checked(current_length,
"bb");
544 arguments.
mult = mult;
545 return window_.call(
source, length, arguments, again,
551 series_ma_detail::Rewindowed<series_ma_detail::BbWindow> window_;
562 return call(
source, mult, current_length,
false);
565 return call(
source, mult, current_length,
true);
568 window_.observe_owned_storage(out);
572 double call(
double source,
double mult,
double current_length,
bool again) {
573 const std::uint64_t length = window_.checked(current_length,
"bbw");
575 arguments.
mult = mult;
576 return window_.call(
source, length, arguments, again,
582 series_ma_detail::Rewindowed<series_ma_detail::BbwWindow> window_;
608 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
609 !std::is_same<Time, bool>::value,
int> = 0>
612 return day_.compute_bands(
source, volume,
static_cast<std::int64_t
>(timestamp_ms),
615 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
616 !std::is_same<Time, bool>::value,
int> = 0>
619 return day_.recompute_bands(
source, volume,
static_cast<std::int64_t
>(timestamp_ms),
622 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
623 !std::is_same<Time, bool>::value,
int> = 0>
625 const std::string& tz,
const std::string& session,
627 return day_.compute_bands(
source, volume,
static_cast<std::int64_t
>(timestamp_ms),
628 stdev_mult, tz, session);
630 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
631 !std::is_same<Time, bool>::value,
int> = 0>
633 const std::string& tz,
const std::string& session,
635 return day_.recompute_bands(
source, volume,
static_cast<std::int64_t
>(timestamp_ms),
636 stdev_mult, tz, session);
639 template <class Anchor, std::enable_if_t<std::is_same<Anchor, bool>::value,
int> = 0>
642 return anchored_.compute_bands(
source, volume, anchor, stdev_mult);
644 template <class Anchor, std::enable_if_t<std::is_same<Anchor, bool>::value,
int> = 0>
647 return anchored_.recompute_bands(
source, volume, anchor, stdev_mult);
649 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
650 !std::is_same<Time, bool>::value,
int> = 0>
652 const std::string&,
bool anchor,
double stdev_mult) {
653 return anchored_.compute_bands(
source, volume, anchor, stdev_mult);
655 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
656 !std::is_same<Time, bool>::value,
int> = 0>
658 const std::string&,
bool anchor,
double stdev_mult) {
659 return anchored_.recompute_bands(
source, volume, anchor, stdev_mult);
675#define PINEFORGE_HAS_PINE_SERIES_WMA 1
676#define PINEFORGE_HAS_PINE_SERIES_VWMA 1
677#define PINEFORGE_HAS_PINE_SERIES_ALMA 1
678#define PINEFORGE_HAS_PINE_SERIES_LINREG 1
679#define PINEFORGE_HAS_PINE_SERIES_BB 1
680#define PINEFORGE_HAS_PINE_SERIES_BBW 1
682#define PINEFORGE_HAS_PINE_SERIES_VWAPBANDS 1
683#define PINEFORGE_HAS_PINE_SERIES_VWAP_BANDS 1
Base with a code: throw coded<std::invalid_argument>(code, {args}, text).
WindowCacheCapacity cache_capacity() const
T read(std::size_t index) const
double recompute(double source, double offset, double sigma, bool floor, double current_length)
double recompute(double source, double offset, double sigma, double current_length)
double compute(double source, double offset, double sigma, double current_length)
double compute(double source, double offset, double sigma, bool floor, double current_length)
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesAlma(WindowCacheCapacity cache)
SeriesBb(WindowCacheCapacity cache)
void observe_owned_storage(OwnedStorage &out) const noexcept
ta::BBResult compute(double source, double mult, double current_length)
ta::BBResult recompute(double source, double mult, double current_length)
SeriesBbw(WindowCacheCapacity cache)
double recompute(double source, double mult, double current_length)
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double source, double mult, double current_length)
SeriesLinreg(WindowCacheCapacity cache)
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double source, double offset, double current_length)
double recompute(double source, double offset, double current_length)
ta::VWAPBandsResult recompute(double source, double volume, Time timestamp_ms, const std::string &tz, const std::string &session, double stdev_mult)
ta::VWAPBandsResult recompute(double source, double volume, Anchor anchor, double stdev_mult)
void observe_owned_storage(OwnedStorage &out) const noexcept
ta::VWAPBandsResult compute(double source, double volume, Anchor anchor, double stdev_mult)
ta::VWAPBandsResult compute(double source, double volume, Time, const std::string &, const std::string &, bool anchor, double stdev_mult)
ta::VWAPBandsResult compute(double source, double volume, Time timestamp_ms, const std::string &tz, const std::string &session, double stdev_mult)
ta::VWAPBandsResult recompute(double source, double volume, Time, const std::string &, const std::string &, bool anchor, double stdev_mult)
SeriesVwapBands(WindowCacheCapacity)
ta::VWAPBandsResult recompute(double source, double volume, Time timestamp_ms, double stdev_mult)
SeriesVwapBands()=default
ta::VWAPBandsResult compute(double source, double volume, Time timestamp_ms, double stdev_mult)
double recompute(double source, double volume, double current_length)
double compute(double source, double volume, double current_length)
SeriesVwma(WindowCacheCapacity cache)
void observe_owned_storage(OwnedStorage &out) const noexcept
double recompute(double source, double current_length)
double compute(double source, double current_length)
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesWma(WindowCacheCapacity cache)
std::size_t count() const
void append(double value, std::size_t limit)
void observe_owned_storage(OwnedStorage &out) const noexcept
RecordedSamples(WindowCacheCapacity cache)
double newest(std::size_t back) const
Rewindowed(const Rewindowed &)=default
typename Window::Arguments Arguments
void observe_owned_storage(OwnedStorage &out) const noexcept
auto call(double sample, std::uint64_t length, const Arguments &arguments, bool again, Call &&on_fixed) -> decltype(on_fixed(std::declval< Fixed & >(), false))
typename Window::Fixed Fixed
Rewindowed(WindowCacheCapacity cache)
Rewindowed & operator=(const Rewindowed &)=default
std::uint64_t checked(double current_length, const char *function) const
double compute(double src)
double recompute(double src)
double compute(double src)
double recompute(double src)
BBResult recompute(double src)
BBResult compute(double src)
double compute(double src, double offset)
double compute(double src)
double compute(double src)
std::int64_t ta_lookback_limit() noexcept
bool same_number(double a, double b)
std::uint64_t checked_length(double current_length, const char *function, std::int64_t maximum)
void lookback_exceeded(std::uint64_t offset, std::int64_t maximum, bool beyond_uint64)
constexpr std::size_t kMaxSamples
std::size_t sample_limit(std::int64_t maximum)
constexpr std::size_t kFirstSamples
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)
@ scratch_capacity_exceeded
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 observe_owned_storage(OwnedStorage &out, const T &value) noexcept
void pine_engine_invariant(const char *english)
void observe_owned_members(OwnedStorage &out, const T &... values) noexcept
friend bool operator==(const Arguments &a, const Arguments &b)
static void feed(Fixed &fixed, double sample)
static void build(std::optional< Fixed > &out, int length, const Arguments &arguments, WindowCacheCapacity)
static constexpr bool kRecordsNa
friend bool operator==(const BandArguments &a, const BandArguments &b)
static void build(std::optional< Fixed > &out, int length, const Arguments &arguments, WindowCacheCapacity)
static ta::BBResult missing()
static void feed(Fixed &fixed, double sample)
static constexpr bool kRecordsNa
static void feed(Fixed &fixed, double sample)
static constexpr bool kRecordsNa
static void build(std::optional< Fixed > &out, int length, const Arguments &arguments, WindowCacheCapacity)
static void build(std::optional< Fixed > &out, int length, const Arguments &, WindowCacheCapacity)
static void feed(Fixed &fixed, double sample)
static constexpr bool kRecordsNa
friend bool operator==(const NoArguments &, const NoArguments &)
static constexpr bool kRecordsNa
static void feed(Fixed &fixed, double sample)
static void build(std::optional< Fixed > &out, int length, const Arguments &, WindowCacheCapacity)
static void build(std::optional< Fixed > &out, int length, const Arguments &, WindowCacheCapacity cache)
static void feed(Fixed &fixed, double sample)
static constexpr bool kRecordsNa