Moving Average Crossover Pine Strategy Without Coding (2026)

Build SMA/EMA Golden Cross and Death Cross in Strategy Builder, export Pine v6, attach in Pine Editor, and test — VWAP crossover unsupported in Basic.

📖 4 min read

📝 789 words

🏷️ Pine Script and TradingView

Share this article:

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 periodsLagNumber of crossings
50/200 → 20/100falls somewhatrises
20/100 → 10/30falls somewhatrises sharply
10/30 → 5/10falls a little morerises 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

FieldValueNote
Fast MA20pick from a plateau, not a peak
Slow MA50
Entryfast crosses above slow
Exitfast crosses below slow, or an ATR stop
Costscommission and slippage setnon-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.

Frequently Asked Questions

Yes. In Strategy Builder add SMA (or EMA) on Step 3, set Golden Cross for buy and Death Cross for sell, Period 10 (slow = Period×2 → 10/20), risk on Step 5, export Pine v6 from Code Generator, then Pine Editor setup and Strategy Tester.

SMA Golden/Death Cross, Period 10, Confirmation 2, chart H1, symbol EURUSD (or your broker’s FX major). SL percentage or ATR — Pine risk.

Golden Cross: fast MA crosses above slow MA (long). Death Cross: fast crosses below slow (short). Period = fast length; slow = Period×2 in Basic export.

TradingView Pine Editor → paste → Save → Add to chart. Official: First steps [1]. Guide: Pine Editor setup.

Raise Confirmation to 2–3, add a Step 4 session filter, tighten risk. See market filters and Strategy Tester.

No. Basic: ≤2 indicators, one TF, four filters, AND/OR only. Hub: Pine Script. MT5 playbooks with those tools: MQL5 hub.