In MQL5, custom indicator price arrays default to chronological ordering (0 is the oldest historical bar, rates_total - 1 is the current forming tick). The indexing mirrors the C# logic perfectly.
To execute this calculation in $O(1)$ time per bar, we must eliminate the inner loop by using a sliding window technique.
Instead of recalculating the entire regression window on every bar, we can derive the current bar's values by looking at the previous bar's values, adding the newest price, and subtracting the oldest price falling out of the window.
Handling Intra-bar Ticks
A critical trap when using running sums in trading platforms is state corruption during the forming bar. If a live tick updates the running sum, the next live tick will inherit corrupted data.To solve this, we store the $S_y$ and $W$ values in hidden data buffers. This guarantees that every tick on the forming bar correctly references the finalized sums from the previous closed bar.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
In MQL5, we expand indicator_buffers to 3, but keep indicator_plots at 1. We map the calculation buffers using the INDICATOR_CALCULATIONS flag, which hides them from the Data Window and chart, but makes them persistent.
Your point about the slow line reacting faster holds, and the lag numbers show it plainly. On a clean linear trend an LSMA has no lag and an SMA lags by (P−1)/2 bars, so the blended line lags by (1−k)(P−1)/2. For 14/30 that's 0.12 × 6.5 ≈ 0.8 bars. For 50/100 it's 0.01 × 24.5 ≈ 0.25 bars. On a steady drift the "slow" line is the quicker of the two.
What actually separates them is noise and curvature. LSMA50 is still far smoother than LSMA14 because it fits across more points, but when price accelerates or turns, a 50-bar straight-line fit overshoots at the end. The cross tends to arrive late and then snap back.
For anyone keeping the crossover idea, I'd fix k per line instead of λ — something like k = 0.6 on the fast and 0.2 on the slow — so the slow line genuinely carries more SMA weight. Then run it on the same EURUSD M5 sample as a plain EMA 12/48 cross, costs included. I expect the gap to be small, but that's what the test is for.