Indicator · Pine Script v6 · On the chart

Parabolic SAR Extended (SAREXT)

by mihakralj

This indicator plots an extended Parabolic SAR on the price chart.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code124
Settings8inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 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

124 lines of Pine Script v6 from lib/reversals/sarext/sarext.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

sarext.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Parabolic SAR Extended (SAREXT)", "SAREXT", overlay=true)

//@function Parabolic SAR Extended — asymmetric PSAR with separate long/short AF parameters,
//          auto-detect or forced initial direction, and an offset applied on reversal.
//          Output is sign-encoded: positive SAR = long mode, negative SAR = short mode.
//@param start_value Initial direction: >0 force long, <0 force short, 0 auto-detect from DM
//@param offset_on_reverse Gap added to SAR on reversal (default 0)
//@param af_init_long Initial AF for long positions (default 0.02)
//@param af_long AF increment per new EP in long (default 0.02)
//@param af_max_long Maximum AF for long (default 0.20)
//@param af_init_short Initial AF for short positions (default 0.02)
//@param af_short AF increment per new EP in short (default 0.02)
//@param af_max_short Maximum AF for short (default 0.20)
//@returns Sign-encoded SAR value (positive = long, negative = short)
//@reference J. Welles Wilder Jr., "New Concepts in Technical Trading Systems" (1978)
//@optimized O(1) per bar, state-machine with asymmetric acceleration factors
sarext(simple float start_value = 0.0, simple float offset_on_reverse = 0.0,
       simple float af_init_long = 0.02, simple float af_long = 0.02, simple float af_max_long = 0.20,
       simple float af_init_short = 0.02, simple float af_short = 0.02, simple float af_max_short = 0.20) =>
    if af_init_long <= 0 or af_long <= 0 or af_max_long <= af_init_long
        runtime.error("Long AF parameters invalid")
    if af_init_short <= 0 or af_short <= 0 or af_max_short <= af_init_short
        runtime.error("Short AF parameters invalid")
    if offset_on_reverse < 0
        runtime.error("Offset must be >= 0")

    var bool is_long = true
    var float sar = low
    var float ep = high
    var float af = af_init_long

    if bar_index == 0
        // Bar 0: collect first bar data
        is_long := true
        sar := high
        ep := low
        af := af_init_short
        float(na)
    else if bar_index == 1
        // Bar 1: determine initial direction
        if start_value > 0
            is_long := true
            sar := math.min(low[1], low)
            ep := math.max(high[1], high)
            af := af_init_long
        else if start_value < 0
            is_long := false
            sar := math.max(high[1], high)
            ep := math.min(low[1], low)
            af := af_init_short
        else
            // Auto-detect from DM
            float plus_dm = high - high[1]
            float minus_dm = low[1] - low
            if plus_dm > minus_dm and plus_dm > 0
                is_long := true
                sar := math.min(low[1], low)
                ep := math.max(high[1], high)
                af := af_init_long
            else
                is_long := false
                sar := math.max(high[1], high)
                ep := math.min(low[1], low)
                af := af_init_short
        is_long ? sar : -sar
    else
        // Bar 2+: standard SAR state machine with asymmetric AF
        float new_sar = sar + af * (ep - sar)
        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 to short
                is_long := false
                new_sar := ep + offset_on_reverse
                ep := low
                af := af_init_short
            else
                if high > ep
                    ep := high
                    af := math.min(af + af_long, af_max_long)
        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 to long
                is_long := true
                new_sar := ep - offset_on_reverse
                ep := high
                af := af_init_long
            else
                if low < ep
                    ep := low
                    af := math.min(af + af_short, af_max_short)
        sar := new_sar
        is_long ? sar : -sar

// ── Inputs ──
float i_start          = input.float(0.0,  "Start Value (0=auto)")
float i_offset         = input.float(0.0,  "Offset on Reverse", minval=0)
float i_af_init_long   = input.float(0.02, "AF Init Long",  minval=0.001, step=0.001)
float i_af_long        = input.float(0.02, "AF Long",       minval=0.001, step=0.001)
float i_af_max_long    = input.float(0.20, "AF Max Long",   minval=0.01,  step=0.01)
float i_af_init_short  = input.float(0.02, "AF Init Short", minval=0.001, step=0.001)
float i_af_short       = input.float(0.02, "AF Short",      minval=0.001, step=0.001)
float i_af_max_short   = input.float(0.20, "AF Max Short",  minval=0.01,  step=0.01)

// ── Calculation ──
float result = sarext(i_start, i_offset, i_af_init_long, i_af_long, i_af_max_long,
                      i_af_init_short, i_af_short, i_af_max_short)

float sar_val = math.abs(nz(result))
bool  is_long = nz(result) > 0
float sar_above = not is_long ? sar_val : na
float sar_below = is_long ? sar_val : na

// ── Plot ──
plot(sar_above, "SAREXT Above", color=color.red,   style=plot.style_linebr, linewidth=2)
plot(sar_below, "SAREXT Below", color=color.green, style=plot.style_linebr, linewidth=2)

Overview

Parabolic SAR Extended (SAREXT) tracks a stop-and-reverse level with separate acceleration-factor start, step, and maximum settings for long and short modes. The start value can force an initial long or short direction, or zero auto-detects direction from directional movement. An optional non-negative offset shifts the SAR when the trend reverses. The plotted value is sign-encoded: positive in long mode and negative in short mode. It is not a strategy and has no entry or exit rules.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend

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

algorithmic-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

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

SettingTypeDefaultVariable
Start Value (0=auto)float0.0i_start
Offset on Reversefloat0.0i_offset
AF Init Longfloat0.02i_af_init_long
AF Longfloat0.02i_af_long
AF Max Longfloat0.20i_af_max_long
AF Init Shortfloat0.02i_af_init_short
AF Shortfloat0.02i_af_short
AF Max Shortfloat0.20i_af_max_short
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

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
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
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

All Candlestick Patterns

by shunjizhan
292 stars

All Candlestick Patterns detects and highlights a wide range of bullish, bearish, and neutral candlestick patterns directly on the price chart. It features optional trend filtering using either a 50-period simple moving average or a combination of 50-period an

Indicator v4 Candlestick patternsTrend
Updated 3 years ago · None stated GitHub repository of All Candlestick Patterns

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