Edgoro
Trend indicator

Parabolic SAR: formula, settings and Pine Script v6 code

How the Parabolic SAR trails price with an accelerating stop, how traders read its flips, which acceleration settings to use, and a free Pine Script.

What it measures

The Parabolic SAR is a trailing stop drawn as a row of dots. In an uptrend the dots sit below price and climb a little every bar; in a downtrend they sit above price and fall. The longer a trend runs, the faster the dots close in on price, which gives the line its curved, parabolic shape. When price reaches the dots, the indicator flips to the other side and starts again.

SAR stands for stop and reverse. J. Welles Wilder introduced it in 1978 as an always-in-the-market system: the dots are the stop for the current position and, once hit, the entry for the opposite one. Today it's mostly used as a trailing stop or a direction filter.

The formula in words

  1. Start with a trend direction and an extreme point (EP): the highest high of the current uptrend, or the lowest low of the current downtrend.
  2. Start the acceleration factor (AF) at 0.02.
  3. Each bar, the next SAR = current SAR + AF × (EP − current SAR). The dots move a fraction of the remaining distance towards the extreme point.
  4. Every time the trend makes a new extreme point, AF rises by 0.02, up to a maximum of 0.20. A long trend therefore pulls the SAR towards price faster and faster.
  5. In an uptrend the SAR may never sit above the lows of the last two bars (in a downtrend, below the last two highs). If it would, it's set to that low or high.
  6. When price trades through the SAR, the trend reverses: the new SAR starts at the old extreme point, AF resets to 0.02, and the process repeats on the other side.

Pine Script has this built in as ta.sar(start, increment, maximum). The reversal is triggered by the bar's high or low reaching the dots, not by the close, so a flip can happen in the middle of a bar.

How traders read it

  • Direction. Dots below price: uptrend. Dots above price: downtrend. The SAR never says "no trend", which is its main weakness.
  • Flips as signals. A flip marks the moment price reached the trailing stop. In steady trends this captures much of the move; in sideways markets the SAR flips back and forth and each flip is a small loss.
  • As a trailing stop. Traders in a position often trail their stop at the dots. Because AF accelerates, the stop tightens as the trend ages, locking in more of the move but also making an exit on a normal pullback more likely.
  • With a trend filter. Taking only flips in the direction of a long moving average, or only when ADX shows a trend, removes many of the range-bound flips. The script below has an optional 200 EMA filter.

Common settings

SettingTypical valueWhat changing it does
Start0.02Higher starts the stop closer to price.
Increment0.02Higher makes the stop accelerate sooner after each new extreme.
Maximum0.20Lower caps the speed and keeps the stop further away in long trends.
Slower variant0.01 / 0.01 / 0.10Fewer flips, more give-back before an exit.

Wilder's 0.02 / 0.02 / 0.20 is the default on almost every platform. Lowering all three together gives the trend more room; raising them makes the SAR hug price.

Pine Script v6 code

The complete, self-contained script is below. Copy it into a new indicator in TradingView's Pine Editor, or download the .pine file.

Pine Script v6
//@version=6
indicator("Parabolic SAR with flip signals", shorttitle = "PSAR", overlay = true)

afStart  = input.float(0.02, "Start", minval = 0.001, step = 0.001, group = "Acceleration factor")
afStep   = input.float(0.02, "Increment", minval = 0.001, step = 0.001, group = "Acceleration factor")
afMax    = input.float(0.2, "Maximum", minval = 0.01, step = 0.01, group = "Acceleration factor")
useTrend = input.bool(false, "Only mark flips in the direction of the trend EMA", group = "Filter")
trendLen = input.int(200, "Trend EMA length", minval = 2, group = "Filter")
upCol    = input.color(color.teal, "Uptrend", group = "Display")
dnCol    = input.color(color.red, "Downtrend", group = "Display")

sar   = ta.sar(afStart, afStep, afMax)
trend = ta.ema(close, trendLen)

// The SAR sits below price in an uptrend and above price in a downtrend.
upTrend  = sar < close
flipUp   = upTrend and not upTrend[1]
flipDown = not upTrend and upTrend[1]

buySignal  = flipUp and (not useTrend or close > trend)
sellSignal = flipDown and (not useTrend or close < trend)

plot(sar, "SAR", color = upTrend ? upCol : dnCol, style = plot.style_cross, linewidth = 2)
plot(useTrend ? trend : na, "Trend EMA", color = color.new(color.gray, 0))
plotshape(buySignal, "Flips up", shape.labelup, location.belowbar, upCol, text = "Up", textcolor = color.white, size = size.tiny)
plotshape(sellSignal, "Flips down", shape.labeldown, location.abovebar, dnCol, text = "Down", textcolor = color.white, size = size.tiny)

alertcondition(buySignal, "SAR flips up", "Parabolic SAR flipped below price on {{ticker}} ({{interval}}) at {{close}}")
alertcondition(sellSignal, "SAR flips down", "Parabolic SAR flipped above price on {{ticker}} ({{interval}}) at {{close}}")

The script uses the built-in ta.sar() and plots the SAR as crosses coloured by direction, with a label on every flip. An optional 200-period EMA filter keeps only flips that agree with the longer trend; it is off by default so you see the raw indicator first.

Two alert conditions fire on flips. A flip is triggered when the bar's high or low reaches the SAR, so it can appear before the bar closes, and once it has, it stays for that bar. Choose Once per bar close if you want the bar's final price in the alert message.

Add it to your chart

  1. Open any chart on TradingView and open the Pine Editor.
  2. Create a new indicator, delete the template code and paste the script.
  3. Click Save, then Add to chart. Change lengths and levels from the indicator's settings.

Stuck on an error? See how to add a Pine Script indicator to TradingView.

Pitfalls

  • Always in the market. The SAR has no neutral state. In sideways markets it flips repeatedly; pair it with a trend filter such as ADX or a long moving average.
  • Accelerating stops in long trends. After many new extremes AF reaches its maximum and the SAR hugs price. A routine pullback then ends the trade.
  • Intrabar flips. Reversals use the high and low, so a flip appears as soon as price touches the dots. Backtests that assume an exit exactly at the SAR level ignore gaps and slippage.
  • Gaps. A stop at the SAR is a level, not a guaranteed fill; price can open beyond it.
  • Tuning on one chart. Small changes to the three factors change the number of flips a lot. Check settings on several symbols and periods before relying on them.

The script is an educational example: test it on your own markets before relying on it. Indicators describe the past; they don't predict the future. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Make it yours with Edgoro

Describe the change in plain English and Edgoro writes the Pine Script v6 for you, ready to paste. For example:

“Plot the Parabolic SAR (0.02, 0.02, 0.2), mark only flips that agree with the 200 EMA while ADX(14) is above 20, and alert once per bar close.”

Build your Parabolic SAR indicator