Savitzky-Golay Real-Time Filter by Ensar

Savitzky-Golay Real-Time Filter by Ensar
OVERVIEW
A Savitzky-Golay (SG) filter fits a polynomial to the last N bars by least squares and reads the fitted curve. This script computes the SG weights directly from the normal equations for any window length and polynomial order, and lets you read the fit either at the newest bar (Real-time) or at the middle of the window (Centered). It also outputs the fitted slope, and a small table shows the price you pay for smoothness with the current settings: delay, noise and overshoot.
WHY THIS VERSION
Most SG implementations use the symmetric (centered) weights from the original tables. Centered weights describe the middle of the window, so the curve either lags by half a window or, when it is drawn shifted back to the middle, it uses bars that did not exist yet at that time. A shifted curve looks perfect on history but cannot be reproduced live. This script never shifts the line back. In Real-time mode the polynomial is evaluated at the newest bar: the line is causal and has zero delay on straight trends. The cost is more noise and overshoot at turning points, and the table shows exactly how much.
HOW IT WORKS
1. Design matrix: for every bar i of the window (0 = newest), x = (e - i) / N, where e is the evaluation point (0 for Real-time, (N - 1) / 2 for Centered). The columns are 1, x, x^2, ..., x^p.
2. Weights: H = (X'X)^-1 X'. Row 0 of H holds the weights of the fitted value at the evaluation point. Row 1 divided by N holds the weights of the fitted slope in price per bar. Both are computed once, on the first bar.
3. On every bar: line = sum of weight x source over the window; slope = sum of slope weight x source.
4. Info table: delay = e bars; noise vs SMA(N) = N x (sum of squared weights), the variance of the line on white noise relative to a simple moving average of the same length; step overshoot = how far the line overshoots after a permanent price jump before settling.
Checks you can reproduce:
- Order 0 is a simple moving average.
- Order 1 in Real-time mode is the least squares moving average (ta.linreg with offset 0).
- Order 2, Centered, N = 5 gives the classic weights (-3, 12, 17, 12, -3) / 35.
- With the default window (N = 21): Real-time order 2 has 7.5x the noise variance of SMA(21) and about 30% step overshoot; Centered order 2 has 2.3x the noise of SMA(21) but is 10 bars late. Real-time is 3.3x noisier than Centered for order 2 and 4.2x for order 4.
HOW TO USE
- Real-time mode is the version you can build on: what you see on history is what you would have seen live.
- Centered mode is a smoother description of where price was (N - 1) / 2 bars ago. Treat it as a delayed reference.
- The slope (in the data window, and in ATR per bar in the table) is the fitted first derivative. It turns earlier than a moving average of similar smoothness, but it is noisier.
- Line color: price above or below the line, or the sign of the slope.
- Alerts: source crossing above or below the line; slope turning up or down.
SETTINGS
- Source
- Window length (bars): odd number from 3 to 201; an even value is raised by one.
- Polynomial order: 0 to 6, capped at window length - 1.
- Evaluation point: Real-time (endpoint) or Centered (delayed).
- Line color: Price vs line or Slope.
- Info table on or off, and its position.
LIMITATIONS
- No smoother removes noise without delay or overshoot. Real-time endpoints overshoot at turning points; the table quantifies it for your settings.
- In my own tests (daily bars, 8 Borsa Istanbul stocks and 10 US large caps, 10-bar forward returns against the market index, separate in-sample and out-of-sample periods), price crossing the Real-time line and slope turns did not show a directional edge after costs. In the US stocks, slope turns were if anything followed by small reversals. Use the filter as a smoother or as an input to other tools, not as a stand-alone entry or exit rule.
- High orders with short windows are unstable; keep the order well below the window length.
- Bars are treated as evenly spaced; session breaks and holidays count as one bar.
BACKGROUND
Savitzky, A. and Golay, M. J. E. (1964), "Smoothing and Differentiation of Data by Simplified Least Squares Procedures", Analytical Chemistry 36(8). This script computes the weights for any order and evaluation point from the normal equations instead of fixed tables, returns the fitted slope, and reports the delay, noise and overshoot of the chosen settings. Thanks to mihakralj, whose open-source Savitzky-Golay Filter was the starting point of my earlier version; the code published here is a new implementation.
OVERVIEW
A Savitzky-Golay (SG) filter fits a polynomial to the last N bars by least squares and reads the fitted curve. This script computes the SG weights directly from the normal equations for any window length and polynomial order, and lets you read the fit either at the newest bar (Real-time) or at the middle of the window (Centered). It also outputs the fitted slope, and a small table shows the price you pay for smoothness with the current settings: delay, noise and overshoot.
WHY THIS VERSION
Most SG implementations use the symmetric (centered) weights from the original tables. Centered weights describe the middle of the window, so the curve either lags by half a window or, when it is drawn shifted back to the middle, it uses bars that did not exist yet at that time. A shifted curve looks perfect on history but cannot be reproduced live. This script never shifts the line back. In Real-time mode the polynomial is evaluated at the newest bar: the line is causal and has zero delay on straight trends. The cost is more noise and overshoot at turning points, and the table shows exactly how much.
HOW IT WORKS
1. Design matrix: for every bar i of the window (0 = newest), x = (e - i) / N, where e is the evaluation point (0 for Real-time, (N - 1) / 2 for Centered). The columns are 1, x, x^2, ..., x^p.
2. Weights: H = (X'X)^-1 X'. Row 0 of H holds the weights of the fitted value at the evaluation point. Row 1 divided by N holds the weights of the fitted slope in price per bar. Both are computed once, on the first bar.
3. On every bar: line = sum of weight x source over the window; slope = sum of slope weight x source.
4. Info table: delay = e bars; noise vs SMA(N) = N x (sum of squared weights), the variance of the line on white noise relative to a simple moving average of the same length; step overshoot = how far the line overshoots after a permanent price jump before settling.
Checks you can reproduce:
- Order 0 is a simple moving average.
- Order 1 in Real-time mode is the least squares moving average (ta.linreg with offset 0).
- Order 2, Centered, N = 5 gives the classic weights (-3, 12, 17, 12, -3) / 35.
- With the default window (N = 21): Real-time order 2 has 7.5x the noise variance of SMA(21) and about 30% step overshoot; Centered order 2 has 2.3x the noise of SMA(21) but is 10 bars late. Real-time is 3.3x noisier than Centered for order 2 and 4.2x for order 4.
HOW TO USE
- Real-time mode is the version you can build on: what you see on history is what you would have seen live.
- Centered mode is a smoother description of where price was (N - 1) / 2 bars ago. Treat it as a delayed reference.
- The slope (in the data window, and in ATR per bar in the table) is the fitted first derivative. It turns earlier than a moving average of similar smoothness, but it is noisier.
- Line color: price above or below the line, or the sign of the slope.
- Alerts: source crossing above or below the line; slope turning up or down.
SETTINGS
- Source
- Window length (bars): odd number from 3 to 201; an even value is raised by one.
- Polynomial order: 0 to 6, capped at window length - 1.
- Evaluation point: Real-time (endpoint) or Centered (delayed).
- Line color: Price vs line or Slope.
- Info table on or off, and its position.
LIMITATIONS
- No smoother removes noise without delay or overshoot. Real-time endpoints overshoot at turning points; the table quantifies it for your settings.
- In my own tests (daily bars, 8 Borsa Istanbul stocks and 10 US large caps, 10-bar forward returns against the market index, separate in-sample and out-of-sample periods), price crossing the Real-time line and slope turns did not show a directional edge after costs. In the US stocks, slope turns were if anything followed by small reversals. Use the filter as a smoother or as an input to other tools, not as a stand-alone entry or exit rule.
- High orders with short windows are unstable; keep the order well below the window length.
- Bars are treated as evenly spaced; session breaks and holidays count as one bar.
BACKGROUND
Savitzky, A. and Golay, M. J. E. (1964), "Smoothing and Differentiation of Data by Simplified Least Squares Procedures", Analytical Chemistry 36(8). This script computes the weights for any order and evaluation point from the normal equations instead of fixed tables, returns the fitted slope, and reports the delay, noise and overshoot of the chosen settings. Thanks to mihakralj, whose open-source Savitzky-Golay Filter was the starting point of my earlier version; the code published here is a new implementation.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.