106 std::numeric_limits<std::size_t>::max() /
sizeof(double);
109 bool beyond_uint64) {
112 {{
"limit",
"max_bars_back"}, {
"max", maximum}},
114 "The script attempts to reference historical data that is too far from "
115 "the current bar (" + (beyond_uint64 ?
"more than " :
"") +
116 std::to_string(offset) +
" bars back). The historical buffer's limit is " +
117 std::to_string(maximum) +
" bars.");
124 std::int64_t maximum) {
125 if (!std::isfinite(length)) {
128 {{
"function", function}, {
"argument",
"length"}, {
"rule",
"must_not_be_na"}},
130 function +
"' function. It must not be na");
132 if (!(length >= 1.0)) {
134 const double truncated = std::trunc(length);
135 const long long shown = truncated < -1e15 ?
static_cast<long long>(-1e15)
136 :
static_cast<long long>(truncated);
139 {{
"function", function}, {
"argument",
"length"}, {
"rule",
"must_be_positive"}},
141 std::to_string(shown) +
") in the '" + function +
142 "' function. It must be > 0.");
144 if (length >= 0x1p64) {
145 constexpr auto last = std::numeric_limits<std::uint64_t>::max();
150 const auto requested =
static_cast<std::uint64_t
>(length);
151 if (maximum != 0 && requested - 1 >
static_cast<std::uint64_t
>(maximum))
161 const auto cap =
static_cast<std::uint64_t
>(maximum);
163 return static_cast<std::size_t
>(cap) + 1 + extra;
171 static constexpr std::size_t
kChunk = 1024;
178 std::size_t
size() const noexcept {
return count_; }
182 const std::size_t offset = back + 1;
183 return values_.read(offset <= next_ ? next_ - offset
184 : values_.size() - (offset - next_));
188 if (limit_ != 0 && count_ == limit_) {
190 const double oldest = values_.read(next_);
191 values_.write(next_, value);
192 evicted_value_ = oldest;
194 next_ = next_ + 1 == limit_ ? 0 : next_ + 1;
196 if (next_ == values_.size()) grow(count_ + 1);
197 values_.write(next_, value);
201 if (count_ == limit_) next_ = 0;
207 void skip() noexcept { pushed_ =
false; }
210 if (!pushed_)
return;
212 const std::size_t slot = next_ == 0 ? limit_ - 1 : next_ - 1;
213 values_.write(slot, evicted_value_);
216 next_ = (next_ == 0 ? values_.size() : next_) - 1;
223 values_.observe_owned_storage(out);
229 void grow(std::size_t minimum) {
233 "history byte extent is not representable");
235 const std::size_t current = values_.
size();
236 std::size_t wanted =
kChunk;
239 wanted = std::max(wanted, minimum);
243 for (std::size_t i = 0; i < count_; ++i) next.write(i, values_.
read(i));
244 values_ = std::move(next);
247 ::pineforge::detail::WindowValues<double> values_;
249 std::size_t next_ = 0;
250 std::size_t count_ = 0;
251 bool pushed_ =
false;
252 bool evicted_ =
false;
261 if (replace) history.
undo();
263 if (history.
size() <= length)
return false;
264 previous = history.
newest(
static_cast<std::size_t
>(length));
272 if (replace) history.
undo();
278 return history.
size() >= count;
292 return update(
source, high, low, length,
true);
295 return update(
source, high, low, length,
false);
298 highest_.observe_owned_storage(out);
299 lowest_.observe_owned_storage(out);
303 double update(
double source,
double high,
double low,
double length,
bool advance) {
309 const double range = hi - lo;
311 return 100.0 * (
source - lo) / range;
327 double compute(
double close,
double high,
double low,
double length) {
328 return update(close, high, low, length,
true);
330 double recompute(
double close,
double high,
double low,
double length) {
331 return update(close, high, low, length,
false);
334 highest_.observe_owned_storage(out);
335 lowest_.observe_owned_storage(out);
339 double update(
double close,
double high,
double low,
double length,
bool advance) {
344 return (hh - close) / (hh - ll) * -100.0;
370 history_.observe_owned_storage(out);
374 double update(
double source,
double length,
bool replace) {
378 const auto count =
static_cast<std::size_t
>(n);
380 for (std::size_t back = count; back > 0; --back) sum += history_.
newest(back - 1);
381 const double mean = sum /
static_cast<double>(n);
383 for (std::size_t back = count; back > 0; --back)
384 md_sum += std::abs(history_.
newest(back - 1) - mean);
385 const double mean_dev = md_sum /
static_cast<double>(n);
387 return (
source - mean) / (0.015 * mean_dev);
417 return update(
source, volume, length,
false);
420 return update(
source, volume, length,
true);
423 sources_.observe_owned_storage(out);
424 volumes_.observe_owned_storage(out);
428 double update(
double source,
double volume,
double length,
bool replace) {
435 volumes_.
push(volume);
437 const auto count =
static_cast<std::size_t
>(n);
438 if (sources_.
size() == count)
return first_value(count);
439 double pos_sum = 0, neg_sum = 0;
440 double previous = sources_.
newest(count);
441 for (std::size_t back = count; back > 0; --back) {
442 const double src = sources_.
newest(back - 1);
443 double pos = 0, neg = 0;
444 if (!
is_na(previous)) {
445 const double flow = src * volumes_.
newest(back - 1);
453 if (neg_sum == 0)
return 100.0;
454 return 100.0 - 100.0 / (1.0 + pos_sum / neg_sum);
460 double first_value(std::size_t count)
const {
461 double previous = sources_.newest(count - 1);
462 double upper = previous * volumes_.newest(count - 1);
463 double lower = upper;
464 for (std::size_t back = count - 1; back > 0; --back) {
465 const double src = sources_.newest(back - 1);
466 const double flow = src * volumes_.newest(back - 1);
472 if (lower == 0)
return 100.0;
473 return 100.0 - 100.0 / (1.0 + upper / lower);
477 series_osc_detail::History sources_;
478 series_osc_detail::History volumes_;
491 history_.observe_owned_storage(out);
495 double update(
double source,
double length,
bool replace) {
506 series_osc_detail::History history_;
521 history_.observe_owned_storage(out);
525 double update(
double source,
double length,
bool replace) {
532 return (
source - previous) / previous * 100.0;
536 series_osc_detail::History history_;
553 history_.observe_owned_storage(out);
557 double update(
double source,
double length,
bool replace) {
559 if (replace) history_.
undo();
562 const auto count =
static_cast<std::size_t
>(n);
563 double up_sum = 0, down_sum = 0;
564 double previous = history_.
newest(count);
565 for (std::size_t back = count; back > 0; --back) {
566 const double src = history_.
newest(back - 1);
567 double up = 0, down = 0;
568 if (!
is_na(previous)) {
569 const double diff = src - previous;
570 if (diff > 0) up = diff;
577 const double denom = up_sum + down_sum;
579 return 100.0 * (up_sum - down_sum) / denom;
583 series_osc_detail::History history_;
598 history_.observe_owned_storage(out);
602 double update(
double source,
double length,
bool replace) {
604 if (replace) history_.
undo();
607 const auto count =
static_cast<std::size_t
>(n);
608 double num = 0, den = 0;
609 for (std::size_t i = 0; i < count; ++i) {
610 num += history_.
newest(i) *
static_cast<double>(i + 1);
611 den += history_.
newest(count - 1 - i);
618 series_osc_detail::History history_;
632 history_.observe_owned_storage(out);
636 double update(
double source,
double length,
bool replace) {
637 const std::uint64_t n =
641 history_.
size() <= n) {
644 for (
auto back =
static_cast<std::size_t
>(n); back > 0; --back) {
645 if (history_.
newest(back - 1) <= history_.
newest(back))
return 0.0;
651 series_osc_detail::History history_;
663 history_.observe_owned_storage(out);
667 double update(
double source,
double length,
bool replace) {
668 const std::uint64_t n =
671 history_.
size() <= n) {
674 for (
auto back =
static_cast<std::size_t
>(n); back > 0; --back) {
675 if (history_.
newest(back - 1) >= history_.
newest(back))
return 0.0;
681 series_osc_detail::History history_;
688#define PINEFORGE_HAS_PINE_SERIES_STOCH 1
689#define PINEFORGE_HAS_PINE_SERIES_CCI 1
690#define PINEFORGE_HAS_PINE_SERIES_MFI 1
691#define PINEFORGE_HAS_PINE_SERIES_MOM 1
692#define PINEFORGE_HAS_PINE_SERIES_ROC 1
693#define PINEFORGE_HAS_PINE_SERIES_CMO 1
694#define PINEFORGE_HAS_PINE_SERIES_WPR 1
695#define PINEFORGE_HAS_PINE_SERIES_COG 1
696#define PINEFORGE_HAS_PINE_SERIES_RISING 1
697#define PINEFORGE_HAS_PINE_SERIES_FALLING 1
Base with a code: throw coded<std::invalid_argument>(code, {args}, text).
WindowCacheCapacity cache_capacity() const
T read(std::size_t index) const
SeriesCci(WindowCacheCapacity cache)
double recompute(double source, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double source, double length)
double compute(double source, double length)
SeriesCmo(WindowCacheCapacity cache)
double recompute(double source, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
double recompute(double source, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesCog(WindowCacheCapacity cache)
double compute(double source, double length)
SeriesFalling(WindowCacheCapacity cache)
double recompute(double source, double length)
double compute(double source, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
void observe_owned_storage(OwnedStorage &out) const noexcept
double recompute(double source, double volume, double length)
SeriesMfi(WindowCacheCapacity cache)
double compute(double source, double volume, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
double recompute(double source, double length)
double compute(double source, double length)
SeriesMom(WindowCacheCapacity cache)
double recompute(double source, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double source, double length)
SeriesRising(WindowCacheCapacity cache)
double compute(double source, double length)
SeriesRoc(WindowCacheCapacity cache)
double recompute(double source, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double source, double high, double low, double length)
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesStoch(WindowCacheCapacity cache)
double recompute(double source, double high, double low, double length)
Result update_series(double src, double length, bool advance, bool want_max, const char *function)
void observe_owned_storage(OwnedStorage &out) const noexcept
SeriesWpr(WindowCacheCapacity cache)
double compute(double close, double high, double low, double length)
double recompute(double close, double high, double low, double length)
std::size_t size() const noexcept
void observe_owned_storage(OwnedStorage &out) const noexcept
double newest(std::size_t back) const
History(WindowCacheCapacity cache, std::size_t limit)
static constexpr std::size_t kChunk
History & operator=(const History &)=default
History(const History &)=default
std::int64_t ta_lookback_limit() noexcept
void lookback_exceeded(std::uint64_t offset, std::int64_t maximum, bool beyond_uint64)
bool lagged(History &history, double source, std::uint64_t length, bool replace, double &previous)
constexpr std::size_t kMaxHistorySlots
bool present_window(History &history, double source, std::uint64_t count, bool replace)
std::size_t history_slots(std::int64_t maximum, std::size_t extra)
std::uint64_t checked_length(double length, const char *function, std::int64_t maximum)
@ 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,...
bool float_band_lt(double a, double b)
bool float_band_eq(double a, double b)
bool float_band_gt(double a, double b)