Home » Technical Analysis Functions » Parabolic SAR Function in Pine Script

Parabolic SAR Function in Pine Script

Photo of author
Published on

The Parabolic SAR (Stop and Reverse) is an effective tool for traders looking to identify potential market trend reversals. Originally developed by J. Welles Wilder, Jr., this method has become a staple in technical analysis. Here, we explore how the Parabolic SAR can be implemented in Pine Script, the scripting language used on the TradingView platform.

The Parabolic SAR helps traders determine the direction of a market’s momentum and when this momentum has a higher probability of changing directions. Its unique feature is the parabolic curve that moves with the price, acting as a trailing stop loss.

Syntax and Arguments

In Pine Script, the function ta.sar is used to implement the Parabolic SAR. The syntax is as follows:

ta.sar(start, inc, max) → series float

Where:

  • start (simple int/float): The initial value for the acceleration factor.
  • inc (simple int/float): The increment by which the acceleration factor increases.
  • max (simple int/float): The maximum value that the acceleration factor can reach.

Example Implementation

Let’s examine a basic example and then a custom implementation in Pine Script.

Basic Usage

//@version=5
indicator("ta.sar")
plot(ta.sar(0.02, 0.02, 0.2), style=plot.style_cross, linewidth=3)

This script plots the Parabolic SAR on the chart with specified parameters.

Custom Implementation

The custom implementation of Parabolic SAR, pine_sar, follows:

//@version=5
indicator("custom_ta_sar")
plot(ta.sar(0.02, 0.02, 0.2), style=plot.style_cross, linewidth=3)

// Custom Pine Script® Implementation
custom_sar(accel_start, accel_inc, accel_max) =>
    var float sar_value = na
    var float extreme_point = na
    var float accel_factor = na
    var bool trend_below = na
    bool isInitialTrend = false
    
    if bar_index == 1
        if close > close[1]
            trend_below := true
            extreme_point := high
            sar_value := low[1]
        else
            trend_below := false
            extreme_point := low
            sar_value := high[1]
        isInitialTrend := true
        accel_factor := accel_start
    
    sar_value := sar_value + accel_factor * (extreme_point - sar_value)
    
    if trend_below
        if sar_value > low
            isInitialTrend := true
            trend_below := false
            sar_value := math.max(high, extreme_point)
            extreme_point := low
            accel_factor := accel_start
    else
        if sar_value < high
            isInitialTrend := true
            trend_below := true
            sar_value := math.min(low, extreme_point)
            extreme_point := high
            accel_factor := accel_start
            
    if not isInitialTrend
        if trend_below
            if high > extreme_point
                extreme_point := high
                accel_factor := math.min(accel_factor + accel_inc, accel_max)
        else
            if low < extreme_point
                extreme_point := low
                accel_factor := math.min(accel_factor + accel_inc, accel_max)
    
    if trend_below
        sar_value := math.min(sar_value, low[1])
        if bar_index > 1
            sar_value := math.min(sar_value, low[2])
        
    else
        sar_value := math.max(sar_value, high[1])
        if bar_index > 1
            sar_value := math.max(sar_value, high[2])
    
    sar_value
    
plot(custom_sar(0.02, 0.02, 0.2), style=plot.style_cross, linewidth=3)

This custom function mirrors the behavior of ta.sar but with additional flexibility.

Code Walkthrough

  • Initialization: The function initializes variables for the SAR value, extreme point (maxMin), acceleration factor, and trend direction (isBelow).
  • First Bar Check: On the first bar, it sets the initial values based on the closing prices.
  • SAR Calculation: It calculates the SAR value for each bar using the acceleration factor and the extreme point.
  • Trend Reversal Logic: The function checks for trend reversals and updates the SAR value, extreme point, and acceleration accordingly.
  • Acceleration Adjustment: The acceleration factor is increased with each new extreme point until it reaches the specified maximum.

Key Features

  • Function Useability: The Parabolic SAR is versatile and can be used in various market conditions.
  • Syntax: It offers a clear and concise syntax for traders who are familiar with Pine Script.
  • Application: Primarily used for setting trailing stops and identifying potential reversal points.

Takeaways

  • The Parabolic SAR is a valuable tool for identifying potential trend reversals.
  • Pine Script provides a straightforward way to implement and customize the Parabolic SAR.
  • The ta.sar function in Pine Script is versatile, allowing for easy incorporation into various trading strategies.

In conclusion, understanding and utilizing the Parabolic SAR in Pine Script can significantly enhance a trader’s technical analysis toolkit, providing a robust method for managing trades and identifying potential trend changes.

Leave a Comment