Indicator · Pine Script v6 · On the chart

Parabolic SAR

by mihakralj

This indicator calculates J.

View on GitHub

GitHub stars47on the repository
Pine versionv6current
Lines of code76
Settings3inputs
LicenceMIT
Last update8 months ago28 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

76 lines of Pine Script v6 from indicators/reversals/psar.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

psar.pine
// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Parabolic SAR", "PSAR", overlay=true)

//@function Calculates Parabolic Stop And Reverse (SAR)
//@param af_start Initial acceleration factor (Wilder's original: 0.02)
//@param af_increment Acceleration factor increment per new extreme (Wilder's original: 0.02)
//@param af_max Maximum acceleration factor (Wilder's original: 0.20)
//@returns SAR value (stop level for current trend)
//@optimized Minimal state variables, O(1) per bar
psar(simple float af_start=0.02, simple float af_increment=0.02, simple float af_max=0.20) =>
    if af_start <= 0 or af_start > af_max
        runtime.error("Start AF must be > 0 and <= Max AF")
    if af_increment <= 0
        runtime.error("AF increment must be > 0")
    if af_max <= af_start
        runtime.error("Max AF must be > Start AF")
    var bool is_long = true
    var float sar = low
    var float ep = high
    var float af = af_start
    if bar_index == 0
        is_long := close > open
        sar := is_long ? low : high
        ep := is_long ? high : low
        af := af_start
    else
        float new_sar = sar + af * (ep - sar)
        bool reverse = false
        if is_long
            new_sar := math.min(new_sar, low[1])
            if bar_index > 1
                new_sar := math.min(new_sar, low[2])
            if low < new_sar
                reverse := true
                is_long := false
                new_sar := ep
                ep := low
                af := af_start
            else
                if high > ep
                    ep := high
                    af := math.min(af + af_increment, af_max)
        else
            new_sar := math.max(new_sar, high[1])
            if bar_index > 1
                new_sar := math.max(new_sar, high[2])
            if high > new_sar
                reverse := true
                is_long := true
                new_sar := ep
                ep := high
                af := af_start
            else
                if low < ep
                    ep := low
                    af := math.min(af + af_increment, af_max)
        sar := new_sar
    sar

// ---------- Main loop ----------

// Inputs
i_af_start = input.float(0.02, "Start AF", minval=0.001, maxval=1.0, step=0.001)
i_af_increment = input.float(0.02, "AF Increment", minval=0.001, maxval=1.0, step=0.001)
i_af_max = input.float(0.20, "Max AF", minval=0.001, maxval=1.0, step=0.01)

// Calculation
psar = psar(i_af_start, i_af_increment, i_af_max)
psar_above = psar > close ? psar : na
psar_below = psar < close ? psar : na

// Plot
plot(psar_above, "PSAR Above", color=color.red,   style=plot.style_linebr, linewidth=2)
plot(psar_below, "PSAR Below", color=color.green, style=plot.style_linebr, linewidth=2)

Overview

Welles Wilder's classic Parabolic Stop and Reverse (PSAR) trailing stop levels to highlight trend direction and potential market reversals. It updates an acceleration factor whenever price makes new extreme highs or lows, bound by a maximum ceiling. When price crosses the current SAR value, the trend direction flips and the trailing level resets to the extreme price. The SAR level is plotted directly on the price chart in green when below the close price and in red when above it. Main settings allow customization of the starting acceleration factor, increment step, and maximum cap.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescriptpinescript-indicatorstechnical-analysistradingview-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Start AFfloat0.02i_af_start
AF Incrementfloat0.02i_af_increment
Max AFfloat0.20i_af_max
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

575 stars

This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult

Indicator v5 Support & resistanceTrend
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows with Screener

Function - Linear Regression

by ricardosantos79
261 stars

This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up

Indicator v5 TrendSupport & resistance
Updated 4 years ago · None stated GitHub repository of Function - Linear Regression

AI Trend

by dearvn
257 stars

AI Trend is an indicator that plots pivot points, trend lines, and support and resistance levels directly on the price chart. It marks high and low pivots based on user-defined length, draws trend lines that check for violations at specified points, and identi

Indicator v5 TrendSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of AI Trend

Ichimoku Cloud

by harryguiacorn
219 stars

This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ichimoku Cloud
219 stars

This indicator identifies local swing highs and swing lows to construct a Zig Zag pattern on the main price chart. It evaluates pivot points based on a customizable number of surrounding left and right bars. Using persistent variables, the script tracks previo

Indicator v4 Support & resistanceTrend Repaints
Updated 5 years ago · None stated GitHub repository of Teaching: Finding Highs and Lows 6

Dynamic Linear Regression

by TradersPost
218 stars

Dynamic Linear Regression calculates and plots linear regression trendlines and standard deviation channels directly on the price chart over a user-selected time period. Traders can configure static channels locked at the end time, dynamic channels that contin

Indicator v6 TrendSupport & resistance
Updated 3 months ago · None stated GitHub repository of Dynamic Linear Regression

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers