|
| 1 | +#pragma once |
| 2 | +#include "na.hpp" |
| 3 | +#include <deque> |
| 4 | + |
| 5 | +namespace pineforge { |
| 6 | + |
| 7 | +// TradingView's sliding-window sum -- the arithmetic behind ta.sma and |
| 8 | +// math.sum, fitted bit-for-bit on the round-7 synthetic pins (see the note |
| 9 | +// above its implementation in src/ta_moving_averages.cpp): |
| 10 | +// |
| 11 | +// * a Kahan-compensated running sum S with compensation c; |
| 12 | +// * the ring stores the COMPENSATED addend y = x - c each source entered |
| 13 | +// with, and subtracts that y (through the same Kahan step) when the |
| 14 | +// source leaves the window `length` bars later -- sub before add; |
| 15 | +// * on the bar whose incoming source swallows the carried compensation |
| 16 | +// (c != 0 and fl(x - c) == x), the sum is re-summed newest-first over the |
| 17 | +// window's sources and c is reset. |
| 18 | +// |
| 19 | +// Both users divide S by `length` themselves (ta.sma) or emit S (math.sum). |
| 20 | +class KahanWindowSum { |
| 21 | + std::deque<double> values_; // window sources, front = newest |
| 22 | + std::deque<double> addends_; // the compensated addends, front = newest |
| 23 | + int length_; |
| 24 | + int count_; // valid sources pushed so far |
| 25 | + double sum_; // S |
| 26 | + double comp_; // c |
| 27 | + |
| 28 | + // Rewind state for the current bar: the pre-bar (S, c, count), the |
| 29 | + // source/addend the bar evicted (na when it evicted none), and whether |
| 30 | + // the bar pushed at all (an na input pushes nothing). |
| 31 | + double saved_sum_; |
| 32 | + double saved_comp_; |
| 33 | + int saved_count_; |
| 34 | + double saved_evicted_value_; |
| 35 | + double saved_evicted_addend_; |
| 36 | + bool saved_pushed_; |
| 37 | + |
| 38 | + void kahan_add(double v); |
| 39 | + void enter(double src, double comp_before); |
| 40 | + |
| 41 | +public: |
| 42 | + explicit KahanWindowSum(int length); |
| 43 | + |
| 44 | + // A valid source enters the window; returns S. Never pass na: both users |
| 45 | + // hold their seeded value on na input and call note_no_push() instead. |
| 46 | + double push(double src); |
| 47 | + // The current bar carried no source (na input): records that a later |
| 48 | + // repush() starts a fresh push and unpush() has nothing to rewind. |
| 49 | + void note_no_push(); |
| 50 | + // Replay the current bar with a different valid source (intrabar |
| 51 | + // recompute): same evicted addend, same pre-bar (S, c). Returns S. |
| 52 | + double repush(double src); |
| 53 | + // Rewind the current bar entirely (its source leaves, the evicted source |
| 54 | + // returns) so the window reads as it did before the bar. |
| 55 | + void unpush(); |
| 56 | + |
| 57 | + int count() const { return count_; } |
| 58 | + int length() const { return length_; } |
| 59 | + bool seeded() const { return count_ >= length_; } |
| 60 | + double sum() const { return sum_; } |
| 61 | +}; |
| 62 | + |
| 63 | +} // namespace pineforge |
0 commit comments