Skip to content

Commit d9e15ab

Browse files
committed
Merge remote-tracking branch 'origin/round7/sma-window-arithmetic' into round7/candidate-c
2 parents 607e742 + 506ca38 commit d9e15ab

11 files changed

Lines changed: 1384 additions & 243 deletions

‎include/pineforge/math.hpp‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "na.hpp"
3+
#include "window_sum.hpp"
34
#include <cstdint>
45
#include <deque>
56

@@ -25,21 +26,8 @@ namespace math {
2526
/// `math.sum(source, length)` (not a `ta.*` builtin). Returns na until seeded,
2627
/// then holds the seeded sum on na-input bars.
2728
class Sum {
29+
KahanWindowSum window_; // TradingView's sliding-window arithmetic (window_sum.hpp)
2830
int length_;
29-
std::deque<double> buffer_;
30-
double sum_;
31-
32-
// State needed to rewind the current bar before an intrabar recompute.
33-
// A valid input can append one value and evict at most one old value, so
34-
// restoring this delta stays O(1) without copying the rolling window.
35-
double saved_sum_;
36-
bool has_saved_state_;
37-
bool current_value_added_;
38-
bool current_value_evicted_;
39-
double current_evicted_value_;
40-
41-
double apply(double src);
42-
void restore();
4331

4432
public:
4533
explicit Sum(int length);

‎include/pineforge/ta.hpp‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include "na.hpp"
33
#include "series.hpp"
4+
#include "window_sum.hpp"
45
#include <deque>
56
#include <cmath>
67
#include <vector>
@@ -221,12 +222,9 @@ class Crossunder {
221222
// --- SMA ---
222223

223224
class SMA {
224-
DynamicRingBuffer<double> buffer;
225+
KahanWindowSum window_;
225226
int length;
226227
int bar_count;
227-
double running_sum;
228-
229-
double recalculate_exact_sum() const;
230228

231229
public:
232230
explicit SMA(int length);

‎include/pineforge/window_sum.hpp‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

‎src/math.cpp‎

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,41 @@ namespace pineforge {
44

55
namespace math {
66

7-
Sum::Sum(int length)
8-
: length_(length), sum_(0.0), saved_sum_(0.0),
9-
has_saved_state_(false), current_value_added_(false),
10-
current_value_evicted_(false), current_evicted_value_(0.0) {}
11-
12-
double Sum::apply(double src) {
13-
current_value_added_ = false;
14-
current_value_evicted_ = false;
7+
Sum::Sum(int length) : window_(length), length_(length) {}
158

9+
double Sum::compute(double src) {
1610
if (is_na(src)) {
1711
// Pine ignores na sources rather than consuming a window slot. Once
1812
// seeded, the last-N-valid sum is therefore held on na-input bars.
19-
if (length_ > 0 && static_cast<int>(buffer_.size()) >= length_) {
20-
return sum_;
13+
window_.note_no_push();
14+
if (length_ > 0 && window_.seeded()) {
15+
return window_.sum();
2116
}
2217
return na<double>();
2318
}
24-
25-
buffer_.push_back(src);
26-
sum_ += src;
27-
current_value_added_ = true;
28-
while ((int)buffer_.size() > length_) {
29-
current_value_evicted_ = true;
30-
current_evicted_value_ = buffer_.front();
31-
sum_ -= buffer_.front();
32-
buffer_.pop_front();
33-
}
34-
35-
if (static_cast<int>(buffer_.size()) < length_) {
19+
double sum = window_.push(src);
20+
if (!window_.seeded()) {
3621
return na<double>();
3722
}
38-
return sum_;
23+
return sum;
3924
}
4025

41-
void Sum::restore() {
42-
if (current_value_added_ && length_ > 0) {
43-
buffer_.pop_back();
44-
if (current_value_evicted_) {
45-
buffer_.push_front(current_evicted_value_);
26+
double Sum::recompute(double src) {
27+
if (is_na(src)) {
28+
// An na recompute rewinds the bar: the source it pushed leaves again
29+
// and the pre-bar seeded window is held (na before seeding).
30+
window_.unpush();
31+
window_.note_no_push();
32+
if (length_ > 0 && window_.seeded()) {
33+
return window_.sum();
4634
}
35+
return na<double>();
4736
}
48-
sum_ = saved_sum_;
49-
current_value_added_ = false;
50-
current_value_evicted_ = false;
51-
}
52-
53-
double Sum::compute(double src) {
54-
saved_sum_ = sum_;
55-
has_saved_state_ = true;
56-
return apply(src);
57-
}
58-
59-
double Sum::recompute(double src) {
60-
if (!has_saved_state_) {
61-
return compute(src);
37+
double sum = window_.repush(src);
38+
if (!window_.seeded()) {
39+
return na<double>();
6240
}
63-
restore();
64-
return apply(src);
41+
return sum;
6542
}
6643

6744
} // namespace math

0 commit comments

Comments
 (0)