The lag is not a flaw to tune away
A moving average is an average of past prices, so it necessarily trails them. An n-period average lags price by roughly half that window — and a crossover of two averages is therefore a statement about something that already finished happening.
That is not a defect to be optimised away. It is what an average is.
Why shortening the periods does not fix it
The obvious response to lag is shorter periods. The trade is much worse than it looks, because the two quantities do not move at the same rate:
| As you shorten the periods | Lag | Number of crossings |
|---|---|---|
| 50/200 → 20/100 | falls somewhat | rises |
| 20/100 → 10/30 | falls somewhat | rises sharply |
| 10/30 → 5/10 | falls a little more | rises very sharply |
Lag falls roughly in proportion to the period. The number of crossings rises much faster, because as the two averages converge they re-cross on noise. Every extra crossing is a round trip, and every round trip pays commission and slippage — so the cost of reducing lag is paid in fees, on every trade, forever.
This is the mechanism behind "whipsaw", and it is also why a crossover strategy is unusually sensitive to the cost settings. Run the doubling test from the production-ready checklist: double commission and slippage and see whether the edge survives. On a fast crossover it often does not.
The plateau test matters more here than anywhere
MA periods are the parameters people optimise hardest, which makes them the place a curve fit is most likely and easiest to detect.
Test a range of periods and line the results up in order:
- a peak — 21/55 works and 20/50 and 22/60 do not — is a fit. Nothing about markets makes 21 special and 20 useless.
- a plateau — everything from roughly 20/50 to 30/70 works acceptably — is a property of the instrument.
Pick the middle of the plateau, not the top of the peak. You give up a little reported performance and gain the only robustness evidence available without a second dataset. The reasoning, and why the count of variations you tried belongs with your result, is in testing ten variations.
Building it
| Field | Value | Note |
|---|---|---|
| Fast MA | 20 | pick from a plateau, not a peak |
| Slow MA | 50 | |
| Entry | fast crosses above slow | |
| Exit | fast crosses below slow, or an ATR stop | |
| Costs | commission and slippage set | non-negotiable for this strategy |
One thing to verify in the export
Both averages must be computed at global scope, not inside the condition. In v6
the and and or operators evaluate lazily, so a ta.sma() call placed to
the right of a false condition is skipped on that bar and its history breaks —
the correct and broken forms are shown side by side in
what each field becomes.
For a crossover strategy this matters more than usual, because a crossover is defined by comparing this bar's relationship to the previous bar's. An average with a gap in its history cannot answer that question correctly.
Tactix AI — Studio vs Guide
Tactix AI is AlfaTactix’s product assistant brand (open Tactix AI).
- Tactix Studio turns a one-sentence strategy description into a draft across Timeframe, Signals, Filters, and Risk in the visual Strategy Builder. You review and edit every field before Code Generator writes MQL5 or Pine Script.
- Tactix Guide explains the step you are on — what to fill, what a control means, or how to phrase a rule — without dumping untested source code.
That is form-first automation: the LLM never replaces Code Generator, and you keep plan limits and real-time validation.

