Skip to content
GetProfitable
Search
Trend

SMA Golden Cross

The 50 and 200 period simple moving averages with the crossover and the crossunder marked on the chart, which is the plainest definition of a long term trend change.

indicator
//@version=6
indicator("SMA Golden Cross", "Golden X", overlay = true)

fastLen = input.int(50, "Fast length", minval = 1, group = "Settings")
slowLen = input.int(200, "Slow length", minval = 1, group = "Settings")
bull = input.color(#26A69A, "Fast above slow", group = "Style")
bear = input.color(#EF5350, "Fast below slow", group = "Style")
neutral = input.color(#787B86, "Slow line", group = "Style")

fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)
above = fast > slow

plot(fast, "Fast SMA", color = above ? bull : bear, linewidth = 2)
plot(slow, "Slow SMA", color = neutral, linewidth = 2)

crossUp = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)
plotshape(crossUp, "Golden cross", location = location.belowbar, style = shape.triangleup, color = bull, size = size.small)
plotshape(crossDown, "Death cross", location = location.abovebar, style = shape.triangledown, color = bear, size = size.small)
alertcondition(crossUp, "Golden cross", "The fast SMA crossed above the slow SMA")
alertcondition(crossDown, "Death cross", "The fast SMA crossed below the slow SMA")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.

What it will not do

  • A 200 period average needs 200 bars of history, so the cross does not exist early on a young chart.
  • The cross is late by construction. By the time two long averages swap places the move that caused it is well underway.
  • On a range bound chart the averages sit on top of each other and cross repeatedly, and none of those crosses mean anything.

Written from this description

Plot 50 and 200 period simple moving averages, colour the fast one by which side of the slow one it sits on, mark the golden cross and the death cross with arrows, and alert on both.

Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.