Indicator · Pine Script v5 · On the chart

Parabolic Stop and Reverse (Trend Following)

by augustoerni

This indicator calculates and plots a custom Parabolic Stop and Reverse (PSAR) trailing level directly on the price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code50
Settings3inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago9 Aug 2024

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

50 lines of Pine Script v5 from indicators/psar.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

psar.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AugustoErni

//@version=5
indicator(title="Parabolic Stop and Reverse (Trend Following)", shorttitle="PSAR (Trend Following)", overlay=true)

startTooltip = "It defines how sensitive the PSAR is at the beginning of a new trend."
startAccelerationFactor = input.float(defval=0.02, title="start Acceleration Factor (AF)", step=0.01, tooltip=startTooltip)

incrementTooltip = "This is the value by which the AF increases each time a new extreme point (EP) is recorded. An EP is the highest high of the current uptrend or the lowest low of the current downtrend. Each time a new EP is recorded, the AF increases by the increment value."
incrementAccelerationFactor = input.float(defval=0.02, title="Increment (AF) for Extreme Point (EP)", step=0.01, tooltip=incrementTooltip)

maximumTooltip = "Even if new EPs continue to be recorded, the AF will not increase beyond this maximum value. It acts as a control on the AF to prevent it from becoming too large, which would cause the PSAR to become too sensitive to minor price changes and flip too often."
maximumAccelerationFactor = input.float(defval=0.2, title="Maximum Value (AF) Reaches", step=0.1, tooltip=incrementTooltip)

var bool isLong = na
var float accelerationFactor = na
var float extremePoint = na
var float stopAndReverse = na
if (na(stopAndReverse[1]))
    isLong := true
    accelerationFactor := startAccelerationFactor
    extremePoint := high
    stopAndReverse := close
else if (isLong[1])
    if (low < stopAndReverse[1])
        isLong := false
        accelerationFactor := startAccelerationFactor
        extremePoint := low
        stopAndReverse := extremePoint[1]
    else
        isLong := true
        stopAndReverse := stopAndReverse[1] + accelerationFactor[1] * (extremePoint[1] - stopAndReverse[1])
        if (high > extremePoint[1])
            accelerationFactor := math.min(maximumAccelerationFactor, accelerationFactor[1] + incrementAccelerationFactor)
            extremePoint := high
else if (high > stopAndReverse[1])
    isLong := true
    accelerationFactor := startAccelerationFactor
    extremePoint := high
    stopAndReverse := extremePoint[1]
else
    isLong := false
    stopAndReverse := stopAndReverse[1] + accelerationFactor[1] * (extremePoint[1] - stopAndReverse[1])
    if (low < extremePoint[1])
        accelerationFactor := math.min(maximumAccelerationFactor, accelerationFactor[1] + incrementAccelerationFactor)
        extremePoint := low

plot(isLong ? stopAndReverse : na, "ParabolicSAR", style=plot.style_cross, linewidth=2, color=color.teal)
plot(not isLong ? stopAndReverse : na, "ParabolicSAR", style=plot.style_cross, linewidth=2, color=color.maroon)

Overview

Parabolic Stop and Reverse (Trend Following) tracks trend direction by updating acceleration factors as new extreme price highs or lows are reached. Bullish trends are displayed with teal crosses below the price, while bearish trends are marked with maroon crosses above the price. Direction flips occur when the current bar breaches the prior stop-and-reverse level. Users can configure the starting acceleration factor, the increment step, and the maximum factor cap.

Details

Published by

augustoerni on GitHub

Type

Indicator, Pine Script v5

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.

Settings

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

SettingTypeDefaultVariable
start Acceleration Factor (AF)float0.02startAccelerationFactor
Increment (AF) for Extreme Point (EP)float0.02incrementAccelerationFactor
Maximum Value (AF) Reachesfloat0.2maximumAccelerationFactor
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