PineForge unreleased
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
pine_ta_series_ma.hpp
Go to the documentation of this file.
1#pragma once
2// TradingView's moving-average and band calls with a length that is not the
3// same on every bar: ta.wma, ta.vwma, ta.alma, ta.linreg, ta.bb and ta.bbw
4// with a series length, and the bands of ta.vwap with a series stdev
5// multiplier.
6//
7// TradingView compiles each of these calls with a series value in that
8// argument: observed on synthetic scripts, one call each, whose value is
9// `2 + bar_index % 3`. It refuses a series `offset` for ta.linreg and a
10// series `mult` for ta.bb. Which values TradingView computes when the length
11// changes has not been recorded: the rule below is the fixed-length class's
12// own window, taken at the call's length.
13//
14// The rule, for the six classes with a length:
15//
16// * A class keeps the samples its fixed-length class of <pineforge/ta.hpp>
17// records, in call order. ta::WMA, ta::SMA and ta::StdDev record a source
18// that is not na; ta::ALMA and ta::Linreg record every call, na included.
19// * A call with `length` builds the fixed-length class for that length, gives
20// it the last `length` samples recorded before the bar, oldest first, and
21// then makes the call on it. The following calls with the same length (and
22// the same other constructor arguments) are that object's own compute() and
23// recompute(). A length that never changes therefore answers the
24// fixed-length class's values bit for bit, warm-up included.
25// * A call with another length builds a new object in the same way. Where
26// the fixed-length class sums its window on every call (ta::WMA, ta::ALMA,
27// ta::Linreg) the answer is bit for bit what that class answers for the new
28// length after the whole history. Where it keeps running sums (ta::SMA and
29// ta::StdDev: SeriesVwma, SeriesBb and SeriesBbw) the sums start again at
30// the oldest sample given, so the answer can differ in the last place from
31// an object of that length that has run since the first bar.
32// * While fewer than `length` samples have been recorded the answer is na
33// (every band of ta.bb), as the fixed-length class answers before its
34// window is full, and no object of that length is built.
35// * recompute() replaces the call of the current bar: the bar is answered
36// again from the samples recorded before it, whatever the two lengths are.
37// A recompute() before the first compute() is that first compute().
38// * The length is checked before anything is recorded, as SeriesSma checks
39// its own (pine_ta_length.hpp): a length that is not finite stops the run
40// with "Invalid value of the 'length' argument in the '<function>'
41// function. It must not be na", a length below 1 with "Invalid value of the
42// 'length' argument (<value>) in the '<function>' function. It must be >
43// 0.", and a length whose window reaches further back than the host's
44// lookback limit with the max_bars_back text. A finite fraction truncates
45// toward zero. Without a limit every recorded sample is kept; with one, the
46// last limit + 1.
47//
48// The compute() and recompute() arguments are the Pine call's inputs in the
49// call's order, without the length, and then the length. An int or int64
50// length goes through ta_number (pine_ta_length.hpp), which keeps an integer
51// na as na.
53#include <pineforge/ta.hpp>
54#include <pineforge/na.hpp>
57
58#include <cmath>
59#include <cstddef>
60#include <cstdint>
61#include <cstdio>
62#include <limits>
63#include <optional>
64#include <stdexcept>
65#include <string>
66#include <type_traits>
67#include <utility>
68
69namespace pineforge {
70namespace source {
71
73
74// The stop of a window that reaches further back than the host's lookback
75// limit: the code, the arguments and the text SeriesSma stops with.
76[[noreturn]] inline void lookback_exceeded(std::uint64_t offset, std::int64_t maximum,
77 bool beyond_uint64) {
78 char text[384];
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 "
82 "%lld bars.",
83 run_error_bar_prefix().c_str(), beyond_uint64 ? "more than " : "",
84 static_cast<unsigned long long>(offset), static_cast<long long>(maximum));
85 pine_limit_stop("max_bars_back", maximum, text);
86}
87
88// A per-call length as the window size it asks for. The two argument stops
89// are simple_ta_length's own, so their code and text are the ones every
90// checked length of the source layer has.
91inline std::uint64_t checked_length(double current_length, const char* function,
92 std::int64_t maximum) {
93 if (!std::isfinite(current_length)) {
94 (void)simple_ta_length(na<double>(), function, "length");
95 pine_engine_invariant("a series length that is not finite passed its check");
96 }
97 if (!(current_length >= 1.0)) {
98 (void)simple_ta_length(current_length, function, "length");
99 pine_engine_invariant("a series length below one passed its check");
100 }
101 constexpr std::uint64_t last = std::numeric_limits<std::uint64_t>::max();
102 if (current_length >= 0x1p64) {
103 if (maximum != 0) lookback_exceeded(last, maximum, current_length > 0x1p64);
104 return last;
105 }
106 // The unsigned conversion keeps the integer part, including 2^63.
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))
109 lookback_exceeded(requested - 1, maximum, false);
110 return requested;
111}
112
113constexpr std::size_t kMaxSamples = std::numeric_limits<std::size_t>::max() / sizeof(double);
114constexpr std::size_t kFirstSamples = 1024;
115
116// How many recorded samples are kept: all of them without a lookback limit
117// (0), the limit plus one with it, which is the longest window a checked
118// length can ask for.
119inline std::size_t sample_limit(std::int64_t maximum) {
120 if (maximum == 0 || static_cast<std::uint64_t>(maximum) >= kMaxSamples) return 0;
121 return static_cast<std::size_t>(maximum) + 1;
122}
123
124inline bool same_number(double a, double b) { return a == b || (is_na(a) && is_na(b)); }
125
126// The recorded samples of one window, oldest to newest. Storage grows as
127// samples arrive; with a limit it becomes a ring of that many samples.
128// Numeric storage is copy-on-write, so a copy shares pages until it writes.
130public:
131 explicit RecordedSamples(WindowCacheCapacity cache) : values_(cache) {}
132
133 std::size_t count() const { return count_; }
134 // The sample recorded `back` samples ago, 1 being the newest. The caller
135 // keeps `back` in [1, count()].
136 double newest(std::size_t back) const {
137 return values_.read(back <= head_ ? head_ - back : values_.size() - (back - head_));
138 }
139 // Adds one sample. With a limit the oldest leaves once `limit` are held.
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;
146 if (!full) ++count_;
147 }
148 void observe_owned_storage(OwnedStorage& out) const noexcept {
149 values_.observe_owned_storage(out);
150 }
151
152private:
153 void ensure_room(std::size_t limit) {
154 const std::size_t size = values_.size();
155 if (count_ < size) return;
156 if (size >= kMaxSamples)
158 {{"resource", "layout"}}, "history byte extent is not representable");
159 std::size_t capacity = kFirstSamples;
160 if (size != 0) capacity = size > kMaxSamples / 2 ? kMaxSamples : 2 * size;
161 if (limit != 0 && capacity > limit) capacity = limit;
162 // Storage that still grows has never wrapped: the samples held are
163 // the first count_ slots.
165 values_.cache_capacity());
166 for (std::size_t i = 0; i < count_; ++i) grown.write(i, values_.read(i));
167 values_ = std::move(grown);
168 }
169
171 std::size_t head_ = 0; // the next write
172 std::size_t count_ = 0; // samples held
173};
174
175// One fixed-length object that follows the length (and the other constructor
176// arguments) of every call, by the rule of the file note. `Window` names the
177// fixed-length class and how it is built and fed.
178template <class Window>
180public:
181 using Fixed = typename Window::Fixed;
182 using Arguments = typename Window::Arguments;
183
184 explicit Rewindowed(WindowCacheCapacity cache) : samples_(cache), cache_(cache) {}
185 // Copy even from an rvalue: the counts still name the source's samples.
186 Rewindowed(const Rewindowed&) = default;
187 Rewindowed& operator=(const Rewindowed&) = default;
188
189 std::uint64_t checked(double current_length, const char* function) const {
190 return checked_length(current_length, function, max_lookback_);
191 }
192
193 // One call of the site with its checked length. `sample` is what the
194 // fixed-length class records for the call; `on_fixed(fixed, replace)`
195 // makes the call on it, as recompute() when `replace` is true and as
196 // compute() otherwise. Returns what `on_fixed` returns.
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_) {
203 // The previous bar is over: its sample joins the history.
204 samples_.append(pending_, sample_limit(max_lookback_));
205 pending_recorded_ = false;
206 }
207 called_ = true;
208 // A call that does not complete may leave the object holding part of
209 // it: the object is then dropped, and the next call builds anew.
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;
215 pending_ = sample;
216 pending_recorded_ = records;
217 return result;
218 }
219
220 void observe_owned_storage(OwnedStorage& out) const noexcept {
221 samples_.observe_owned_storage(out);
223 }
224
225private:
226 // Drops the fixed-length object when a call leaves by an exception.
227 struct Unfinished {
228 explicit Unfinished(std::optional<Fixed>& object) : fixed(object) {}
229 ~Unfinished() {
230 if (drop) fixed.reset();
231 }
232 Unfinished(const Unfinished&) = delete;
233 Unfinished& operator=(const Unfinished&) = delete;
234 std::optional<Fixed>& fixed;
235 bool drop = true;
236 };
237
238 // The call on a new object for `length`, built from the samples recorded
239 // before the bar.
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)) {
243 fixed_.reset();
244 const std::size_t held = samples_.count();
245 // The window cannot be full yet: na, and nothing of that size is built.
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()))
249 throw coded<std::length_error>(RunFailureCode::scratch_capacity_exceeded,
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);
257 }
258
259 RecordedSamples samples_; // the samples recorded before the current bar
260 WindowCacheCapacity cache_;
261 std::optional<Fixed> fixed_; // has taken those samples and the current bar's call
262 std::uint64_t fixed_length_ = 0;
263 Arguments fixed_arguments_{};
264 double pending_ = na<double>(); // the current bar's sample
265 bool pending_recorded_ = false; // whether it joins samples_ when the bar is over
266 bool called_ = false;
267 std::int64_t max_lookback_ = ::pineforge::detail::ta_lookback_limit();
268};
269
271 friend bool operator==(const NoArguments&, const NoArguments&) { return true; }
272};
273
274struct WmaWindow {
275 using Fixed = ta::WMA;
277 static constexpr bool kRecordsNa = false;
278 static void build(std::optional<Fixed>& out, int length, const Arguments&,
279 WindowCacheCapacity cache) {
280 out.emplace(length, cache);
281 }
282 static void feed(Fixed& fixed, double sample) { fixed.compute(sample); }
283 static double missing() { return na<double>(); }
284};
285
286struct SmaWindow {
287 using Fixed = ta::SMA;
289 static constexpr bool kRecordsNa = false;
290 static void build(std::optional<Fixed>& out, int length, const Arguments&,
292 out.emplace(length);
293 }
294 static void feed(Fixed& fixed, double sample) { fixed.compute(sample); }
295 static double missing() { return na<double>(); }
296};
297
300 struct Arguments {
301 double offset = 0.0;
302 double sigma = 0.0;
303 bool floor = false;
304 friend bool operator==(const Arguments& a, const Arguments& b) {
305 return same_number(a.offset, b.offset) && same_number(a.sigma, b.sigma) &&
306 a.floor == b.floor;
307 }
308 };
309 static constexpr bool kRecordsNa = true;
310 static void build(std::optional<Fixed>& out, int length, const Arguments& arguments,
312 out.emplace(length, arguments.offset, arguments.sigma, arguments.floor);
313 }
314 static void feed(Fixed& fixed, double sample) { fixed.compute(sample); }
315 static double missing() { return na<double>(); }
316};
317
321 static constexpr bool kRecordsNa = true;
322 static void build(std::optional<Fixed>& out, int length, const Arguments&,
324 out.emplace(length);
325 }
326 // The offset only shifts the answer of its own call; an earlier sample's
327 // answer is not read.
328 static void feed(Fixed& fixed, double sample) { fixed.compute(sample, 0.0); }
329 static double missing() { return na<double>(); }
330};
331
333 double mult = 0.0;
334 friend bool operator==(const BandArguments& a, const BandArguments& b) {
335 return same_number(a.mult, b.mult);
336 }
337};
338
339struct BbWindow {
340 using Fixed = ta::BB;
342 static constexpr bool kRecordsNa = false;
343 static void build(std::optional<Fixed>& out, int length, const Arguments& arguments,
345 out.emplace(length, arguments.mult);
346 }
347 static void feed(Fixed& fixed, double sample) { fixed.compute(sample); }
349 ta::BBResult result;
350 result.middle = na<double>();
351 result.upper = na<double>();
352 result.lower = na<double>();
353 return result;
354 }
355};
356
357struct BbwWindow {
358 using Fixed = ta::BBW;
360 static constexpr bool kRecordsNa = false;
361 static void build(std::optional<Fixed>& out, int length, const Arguments& arguments,
363 out.emplace(length, arguments.mult);
364 }
365 static void feed(Fixed& fixed, double sample) { fixed.compute(sample); }
366 static double missing() { return na<double>(); }
367};
368
369} // namespace series_ma_detail
370
371// ta.wma(source, length) with a series length: ta::WMA at the call's length.
372// An na source answers na and is in no window: the window is the last
373// `length` sources that were not na, weighted 1 (oldest) to `length`.
375public:
377 explicit SeriesWma(WindowCacheCapacity cache) : window_(cache) {}
378 double compute(double source, double current_length) {
379 return call(source, current_length, false);
380 }
381 double recompute(double source, double current_length) {
382 return call(source, current_length, true);
383 }
384 void observe_owned_storage(OwnedStorage& out) const noexcept {
385 window_.observe_owned_storage(out);
386 }
387
388private:
389 double call(double source, double current_length, bool again) {
390 const std::uint64_t length = window_.checked(current_length, "wma");
391 return window_.call(source, length, {}, again, [source](ta::WMA& fixed, bool replace) {
392 return replace ? fixed.recompute(source) : fixed.compute(source);
393 });
394 }
395
396 series_ma_detail::Rewindowed<series_ma_detail::WmaWindow> window_;
397};
398
399// ta.vwma(source, length) with a series length, as the Pine reference manual
400// defines the call:
401// ta.sma(source * volume, length) / ta.sma(volume, length)
402// with two ta::SMA windows that take their inputs separately, both at the
403// call's length. A bar whose product is na keeps the product window's last
404// mean, as ta::SMA does for an na input; a bar whose volume is not na enters
405// the volume window whatever the source is. A zero volume mean answers na.
406// This is not ta::VWMA, which keeps complete pairs and answers na on a bar
407// whose source or volume is na: on bars without na the two agree up to the
408// last place.
410public:
412 explicit SeriesVwma(WindowCacheCapacity cache) : product_(cache), volume_(cache) {}
413 double compute(double source, double volume, double current_length) {
414 return call(source, volume, current_length, false);
415 }
416 double recompute(double source, double volume, double current_length) {
417 return call(source, volume, current_length, true);
418 }
419 void observe_owned_storage(OwnedStorage& out) const noexcept {
420 product_.observe_owned_storage(out);
421 volume_.observe_owned_storage(out);
422 }
423
424private:
426
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);
430 });
431 }
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);
436 if (volume_mean == 0.0) return na<double>();
437 return product_mean / volume_mean;
438 }
439
440 Mean product_;
441 Mean volume_;
442};
443
444// ta.alma(series, length, offset, sigma[, floor]) with a series length:
445// ta::ALMA at the call's length. Every call holds a place in the window, an
446// na source included, so the answer is na while an na source is among the
447// last `length` calls. offset, sigma and floor are constructor arguments of
448// ta::ALMA: a call that changes one of them builds a new object, as a call
449// that changes the length does.
451public:
453 explicit SeriesAlma(WindowCacheCapacity cache) : window_(cache) {}
454 double compute(double source, double offset, double sigma, double current_length) {
455 return call(source, offset, sigma, false, current_length, false);
456 }
457 double compute(double source, double offset, double sigma, bool floor,
458 double current_length) {
459 return call(source, offset, sigma, floor, current_length, false);
460 }
461 double recompute(double source, double offset, double sigma, double current_length) {
462 return call(source, offset, sigma, false, current_length, true);
463 }
464 double recompute(double source, double offset, double sigma, bool floor,
465 double current_length) {
466 return call(source, offset, sigma, floor, current_length, true);
467 }
468 void observe_owned_storage(OwnedStorage& out) const noexcept {
469 window_.observe_owned_storage(out);
470 }
471
472private:
473 double call(double source, double offset, double sigma, bool floor, double current_length,
474 bool again) {
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,
481 [source](ta::ALMA& fixed, bool replace) {
482 return replace ? fixed.recompute(source) : fixed.compute(source);
483 });
484 }
485
486 series_ma_detail::Rewindowed<series_ma_detail::AlmaWindow> window_;
487};
488
489// ta.linreg(source, length, offset) with a series length: ta::Linreg at the
490// call's length. Every call holds a place in the window, an na source
491// included, so the answer is na while an na source is among the last `length`
492// calls. The offset is the call's own and is read on every call.
494public:
496 explicit SeriesLinreg(WindowCacheCapacity cache) : window_(cache) {}
497 double compute(double source, double offset, double current_length) {
498 return call(source, offset, current_length, false);
499 }
500 double recompute(double source, double offset, double current_length) {
501 return call(source, offset, current_length, true);
502 }
503 void observe_owned_storage(OwnedStorage& out) const noexcept {
504 window_.observe_owned_storage(out);
505 }
506
507private:
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,
511 [source, offset](ta::Linreg& fixed, bool replace) {
512 return replace ? fixed.recompute(source, offset)
513 : fixed.compute(source, offset);
514 });
515 }
516
517 series_ma_detail::Rewindowed<series_ma_detail::LinregWindow> window_;
518};
519
520// ta.bb(series, length, mult) with a series length: ta::BB at the call's
521// length. An na source is in no window; once `length` sources that were not
522// na exist, a bar with an na source answers the bands of the last `length` of
523// them again, as ta::BB does. mult is a constructor argument of ta::BB: a
524// call that changes it builds a new object, as a call that changes the length
525// does.
526class SeriesBb {
527public:
529 explicit SeriesBb(WindowCacheCapacity cache) : window_(cache) {}
530 ta::BBResult compute(double source, double mult, double current_length) {
531 return call(source, mult, current_length, false);
532 }
533 ta::BBResult recompute(double source, double mult, double current_length) {
534 return call(source, mult, current_length, true);
535 }
536 void observe_owned_storage(OwnedStorage& out) const noexcept {
537 window_.observe_owned_storage(out);
538 }
539
540private:
541 ta::BBResult call(double source, double mult, double current_length, bool again) {
542 const std::uint64_t length = window_.checked(current_length, "bb");
544 arguments.mult = mult;
545 return window_.call(source, length, arguments, again,
546 [source](ta::BB& fixed, bool replace) {
547 return replace ? fixed.recompute(source) : fixed.compute(source);
548 });
549 }
550
551 series_ma_detail::Rewindowed<series_ma_detail::BbWindow> window_;
552};
553
554// ta.bbw(series, length, mult) with a series length: ta::BBW at the call's
555// length, with SeriesBb's window and its treatment of an na source and of
556// mult.
558public:
560 explicit SeriesBbw(WindowCacheCapacity cache) : window_(cache) {}
561 double compute(double source, double mult, double current_length) {
562 return call(source, mult, current_length, false);
563 }
564 double recompute(double source, double mult, double current_length) {
565 return call(source, mult, current_length, true);
566 }
567 void observe_owned_storage(OwnedStorage& out) const noexcept {
568 window_.observe_owned_storage(out);
569 }
570
571private:
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,
577 [source](ta::BBW& fixed, bool replace) {
578 return replace ? fixed.recompute(source) : fixed.compute(source);
579 });
580 }
581
582 series_ma_detail::Rewindowed<series_ma_detail::BbwWindow> window_;
583};
584
585// ta.vwap(source, anchor, stdev_mult) with a series stdev multiplier. The
586// accumulation has no window: ta::VWAP and ta::AnchoredVWAP already take the
587// multiplier on every call (compute_bands), and the bands of a bar are its
588// mean plus and minus that bar's multiplier times its deviation. A multiplier
589// that never changes answers ta::VWAPBands / ta::AnchoredVWAPBands bit for
590// bit. The multiplier is the last argument of every form:
591//
592// (source, volume, timestamp_ms, stdev_mult) the UTC day
593// (source, volume, timestamp_ms, tz, session, stdev_mult) the session day
594// (source, volume, anchor, stdev_mult) the caller's anchor
595// (source, volume, timestamp_ms, tz, session, anchor, stdev_mult)
596// the caller's anchor again; the three clock arguments are not read
597//
598// `anchor` is taken only as a bool and `timestamp_ms` only as another
599// integer type, so the two four-argument forms never compete. One object
600// serves one call site, which uses one anchoring: the day forms and the
601// anchor forms keep separate sums. No argument is checked here: an na
602// multiplier answers the mean and na bands, as the two classes do.
604public:
605 SeriesVwapBands() = default;
607
608 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
609 !std::is_same<Time, bool>::value, int> = 0>
610 ta::VWAPBandsResult compute(double source, double volume, Time timestamp_ms,
611 double stdev_mult) {
612 return day_.compute_bands(source, volume, static_cast<std::int64_t>(timestamp_ms),
613 stdev_mult);
614 }
615 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
616 !std::is_same<Time, bool>::value, int> = 0>
617 ta::VWAPBandsResult recompute(double source, double volume, Time timestamp_ms,
618 double stdev_mult) {
619 return day_.recompute_bands(source, volume, static_cast<std::int64_t>(timestamp_ms),
620 stdev_mult);
621 }
622 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
623 !std::is_same<Time, bool>::value, int> = 0>
624 ta::VWAPBandsResult compute(double source, double volume, Time timestamp_ms,
625 const std::string& tz, const std::string& session,
626 double stdev_mult) {
627 return day_.compute_bands(source, volume, static_cast<std::int64_t>(timestamp_ms),
628 stdev_mult, tz, session);
629 }
630 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
631 !std::is_same<Time, bool>::value, int> = 0>
632 ta::VWAPBandsResult recompute(double source, double volume, Time timestamp_ms,
633 const std::string& tz, const std::string& session,
634 double stdev_mult) {
635 return day_.recompute_bands(source, volume, static_cast<std::int64_t>(timestamp_ms),
636 stdev_mult, tz, session);
637 }
638
639 template <class Anchor, std::enable_if_t<std::is_same<Anchor, bool>::value, int> = 0>
640 ta::VWAPBandsResult compute(double source, double volume, Anchor anchor,
641 double stdev_mult) {
642 return anchored_.compute_bands(source, volume, anchor, stdev_mult);
643 }
644 template <class Anchor, std::enable_if_t<std::is_same<Anchor, bool>::value, int> = 0>
645 ta::VWAPBandsResult recompute(double source, double volume, Anchor anchor,
646 double stdev_mult) {
647 return anchored_.recompute_bands(source, volume, anchor, stdev_mult);
648 }
649 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
650 !std::is_same<Time, bool>::value, int> = 0>
651 ta::VWAPBandsResult compute(double source, double volume, Time, const std::string&,
652 const std::string&, bool anchor, double stdev_mult) {
653 return anchored_.compute_bands(source, volume, anchor, stdev_mult);
654 }
655 template <class Time, std::enable_if_t<std::is_integral<Time>::value &&
656 !std::is_same<Time, bool>::value, int> = 0>
657 ta::VWAPBandsResult recompute(double source, double volume, Time, const std::string&,
658 const std::string&, bool anchor, double stdev_mult) {
659 return anchored_.recompute_bands(source, volume, anchor, stdev_mult);
660 }
661
662 void observe_owned_storage(OwnedStorage& out) const noexcept {
663 ::pineforge::observe_owned_members(out, day_, anchored_);
664 }
665
666private:
667 ta::VWAP day_;
668 ta::AnchoredVWAP anchored_;
669};
670
671} // namespace source
672} // namespace pineforge
673
674// Feature macros: one per class above.
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
681// SeriesVwapBands, under both spellings of its name in upper case.
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).
std::size_t size() const
Definition series.hpp:188
WindowCacheCapacity cache_capacity() const
Definition series.hpp:189
T read(std::size_t index) const
Definition series.hpp:138
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)
ta::VWAPBandsResult recompute(double source, double volume, Time timestamp_ms, double stdev_mult)
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)
void observe_owned_storage(OwnedStorage &out) const noexcept
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))
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
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)
std::size_t sample_limit(std::int64_t maximum)
int simple_ta_length(double value, const char *function, const char *argument)
double na< double >()
Definition na.hpp:64
void pine_limit_stop(const char *limit, std::int64_t max, const char *english)
bool is_na(const Line &h)
Definition drawing.hpp:58
WindowCacheCapacity & window_cache_capacity()
Definition series.hpp:40
std::string run_error_bar_prefix()
Stops using TradingView's text prepend "Error on bar N: " when a bar context is installed,...
T na()
Definition na.hpp:54
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)
friend bool operator==(const BandArguments &a, const BandArguments &b)
static void build(std::optional< Fixed > &out, int length, const Arguments &arguments, WindowCacheCapacity)
static void feed(Fixed &fixed, double sample)
static void feed(Fixed &fixed, double sample)
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)
friend bool operator==(const NoArguments &, const NoArguments &)
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)