The Parabolic SAR, or PSAR, is an effective indicator to identify market trends. PSAR stands for Parabolic “Stop and Reverse” and as characterized by its name, the indicator tag on the chart level that is susceptible to be the tipping point for a trend reversal. The PSAR works best in a trending market. The Parabolic SAR has two parameters, the “step” and “maximum” with the default value, 0.02 and 0.2, respectively. Each “step” is characterized by the size of each new high or low depending on the trend, while the “maximum” is the limit added by each step.

The formula of the PSAR may vary between a rising and a falling indicator. A rising PSAR is drawn under the bars or candlesticks, while a falling PSAR is drawn above the price. As per illustration in Fig. 239, the rising PSAR is represented by the variable RPSAR and the falling PSAR by FPSAR.

Parabolic SAR Formula
Fig.239 Parabolic SAR Formula

In this guide, we will go through the standard methodology to identify potential buy and sell signals using the PSAR indicator with the default settings. We will demonstrate the use of the PSAR to determine a price reversal as well as the use of the indicator to define the stop loss and trailing stop level. We will also develop and guide you through the method to store the data points and how to retrieve them to build a trading strategy with the Parabolic SAR. MQL4 PSAR function retrieves the indicator’s data point directly. On the other hand, MQL5 requires the data to be initialized and to be stored in the global cache before being attributed to a declared “double type” array. It may appear a bit confusing, but it is not that complicated. We will go into the details step by step for this MQL5 particularity. Finally, we will illustrate a practical example of usage with code ready to use.

How does the Parabolic SAR indicator work?

Before we dive into the details of the Moving Average indicator code implementation. Let us illustrate the most prevalent trend-following and reversal buy and sell signals interpretation. There are various ways to decipher the PSAR indicator, we will highlight the most popular one which is the “reverse signal.”

Buy signal

Buy signal

A “reverse” buy signal occurs when the PSAR indicator changes its position from above the candlesticks to underneath. Usually, traders will use the position of the PSAR to set the stop loss and to subsequently trail the stop loss using the PSAR dots.

Sell signal

Sell signal

A “reverse” sell signal is triggered when the PSAR dots move from a position under the price to above the candlesticks. Stop orders and trailing stops can be placed using the PSAR dots.

How does the Parabolic SAR indicator store its data points?

The MQL5 Parabolic SAR keeps its data point in a global cache which is a dynamic array that is the indicator buffer. The size of the array is automatically controlled by MetaTrader and initialized when the Expert Advisor loads. An indicator buffer is a time-series array and because of that, it has the particularity of reverse indexing. It means that the array is sorted by descending where the latest data point is on the left side of the array. A Time-series is used to store historical data and comprise the time information.

Consequently, the time-series element with index zero contains the latest quote of a symbol. For instance, an indicator on the daily timeframe will have the current oscillator bar at index zero and yesterday’s data at the position index one (Fig.240).

Indicator data points array

Fig.240 Indicator data points array 

MQL5

To access the Parabolic SAR indicator data, MQL5 requires the use of the CopyBuffer() function. This approach does not apply to MQL4 as the MQL4 indicator function directly returns the value of the PSAR’s data point instead of the indicator handle. The indicator handle is a sort of address to locate the data in the MetaTrader global cache. To initialize the PSAR data, the following parameters should be provided to the CopyBuffer() function:

  1. Provide the handle that is returned by the iSAR() function.
  2. Provide the buffer number. You may provide zero as the default value.
  3. Provide the start position. You may provide zero to retrieve from the most recent data point. This number represents the index where zero is the most recent data point and one is the previous data point.
  4. Specify the number of data points to get from the iSAR() function.
  5. Provide the variable where the data will be stored. The variable must be a double type.

How to retrieve Parabolic SAR data with the CopyBuffer() function?

  1. Declare a variable for the handle: int IndicatorHandle = iSAR().
  2. Declare a variable to store indicator data: double psar_indicator.
  3. Copy the data to the indicator variable and retrieve data points:
    1. CopyBuffer(IndicatorHandle, 0, 0, 100, psar_indicator).
    2. Retrieve the PSAR data point with psar_indicator[0], psar_indicator[1].

CopyBuffer() Function
Fig.241 CopyBuffer() Function

How to access the indicator data?

MQL4

The iSAR() function will return the indicator data point values according to the following parameters:

  1. The symbol of the traded instrument. You may provide a string such as “EURUSD” or use the function Symbol() to retrieve the symbol from the chart where the Expert Advisor is attached to.
  2. Provide the time frame using the enumeration value from the constant ENUM_TIMEFRAMES for instance:
    1. PERIOD_CURRENT, for the current timeframe from the chart.
    2. PERIOD_M1, for the one-minute timeframe.
    3. PERIOD_D1, for the daily timeframe.
  3. Specify the “Step” which defines the price increment. The default value is 0.02.
  4. Set the “Maximum” step. The default value is 0.2.
  5. Provide the index of the indicator buffer where zero is the latest data point and one the previous data point.

MQL4 Parabolic SAR function
Fig.242 MQL4 Parabolic SAR function

MQL5

When calling iSAR(), the function will create the Parabolic SAR indicator in the MetaTrader global cache and returns its handle according to these four parameters:

  1. The symbol of the traded instrument. You may provide a string such as “EURUSD” or you can use the function Symbol() to get the symbol from the chart where the Expert Advisor is attached to.
  2. Provide the time frame of the data point to extract using the enumeration value from the constant ENUM_TIMEFRAMES:
    1. PERIOD_CURRENT, to get data points from the current timeframe from the chart to which the Expert Advisor is attached.
    2. PERIOD_M1, for the one-minute timeframe.
    3. PERIOD_D1, for the daily timeframe.
  3. Specify the “Step” which defines the price increment. The default value is 0.02.
  4. Set the “Maximum” step. The default value is 0.2.

Note that to retrieve data points from the iSAR() function, you need to use it in combination with the function CopyBuffer().

MQL5 Parabolic SAR function
Fig.243 MQL5 Parabolic SAR function

Practical case

MQL5

Fig.244 demonstrates the use of the Parabolic SAR indicator to create buy and sell signals with the trend reversal approach.

Line 144: Declare the indicator handle and assign iSAR() function handle with the specified parameters. We will use this as an example, with the default “step” and “maximum” PSAR settings, 0.02 and 0.2.

Line 145: Declare the indicator “double type” array to store the iSAR() dots.

Line 148: Copy the latest hundred data points into the indicator array psar_indicator.

Line 150 – 165: The IF statement identifies the PSAR trend reversal. The first block defines the conditions of a buy signal, with the confirmation of PSAR three previous data points and the latest data point. A buy signal is triggered when the three previous dots are drawn above the price. The second block assesses the conditions of a sell signal, which is the exact opposite configuration of the first IF block.

Parabolic SAR reversal signal in MQL5
Fig.244 Parabolic SAR reversal signal in MQL5

MQL4

Fig.245 exhibits the use of the Parabolic SAR indicator to create a buy and sell signal with the trend reversal approach.

Line 169 – 172: Declare the variables that will store each of the PSAR dots, the three previous data points and the latest data point by assigning the value from the iSAR() function. Note that the last parameter from the iSAR() function is the index of the dots to retrieve where zero is the most recent data.

Line 174 – 188: IF statement to identify the PSAR trend reversal. The first block defines the conditions of a buy signal, with the confirmation of PSAR three previous data points and the latest data point. A buy signal is triggered when the three previous dots are drawn above the price. The second block assesses the conditions of a sell signal, which is the exact opposite configuration of the first IF block.

Parabolic SAR reversal signal in MQL4
Fig.245 Parabolic SAR reversal signal in MQL4

Leave a Reply

  +  76  =  77