 Title:
 Parabolic StopAndReverse Indicator — The Full Guide.
 Image:
 Message:

Creating and Coding Various Strategies Using the PSAR in Python.
Sofien Kaabar
Just now·9 min read
www.pxfuel.com
The Parabolic StopandReverse Indicator is a unique and versatile tool that can be used either for directional guidance or for risk management. It is uncommon to find such a powerful indicator. But is it really powerful? We will try to do some modest backtesting right after introducing and coding the indicator in Python.
If you are interested by market sentiment and how to model the sentiment of institutional traders, feel free to have a look at the below article:
Using Python to Download Sentiment Data for Financial Trading.
How to Create a Function that Fetches Market Sentiment Data.
medium.com
The Parabolic stopandreverse is an interesting indicator created by Wilder Willes who is also the creator of the famous RSI. This indicator is mostly used as a trailing stop that tracks the trend as it develops but there is no harm in testing it as a trading strategy. It is worth noting that it performs relatively well in steady trends but just as any other indicator, it has its weakness, in this case, ranging markets.
EURUSD hourly data with the Parabolic SAR.
I will refer to a python library called talib from where the user could import the sar function that uses a data frame and calculates the values. Having modified that function, you can refer to the one below (I do not take credit for it as I merely just changed some lines as opposed to the other functions which have been coded by me):
def sar(s, af = 0.02, amax = 0.2): high, low = s.high, s.low # Starting values
sig0, xpt0, af0 = True, high[0], af
sar = [low[0]  (high  low).std()] for i in range(1, len(s)):
sig1, xpt1, af1 = sig0, xpt0, af0
lmin = min(low[i  1], low[i])
lmax = max(high[i  1], high[i]) if sig1:
sig0 = low[i] > sar[1]
xpt0 = max(lmax, xpt1)
else:
sig0 = high[i] >= sar[1]
xpt0 = min(lmin, xpt1)
if sig0 == sig1:
sari = sar[1] + (xpt1  sar[1])*af1
af0 = min(amax, af1 + af) if sig0:
af0 = af0 if xpt0 > xpt1 else af1
sari = min(sari, lmin)
else:
af0 = af0 if xpt0 < xpt1 else af1
sari = max(sari, lmax)
else:
af0 = af
sari = xpt0sar.append(sari) return sarThe basic understanding is that when the Parabolic SAR (the dots around the market price) is under the current price, then the outlook is bullish and when it is above the current price, then the outlook is bearish.
USDCHF hourly data with the Parabolic SAR.
To add the Parabolic SAR to your OHLC array (preferably numpy), use the following steps:
importing pandas as pd# Converting to a pandas Data frame
my_data = pd.DataFrame(my_data)# Renaming columns to fit the function
my_data.columns = ['open','high','low','close']# Calculating the Parabolic SAR
Parabolic = sar(my_data, 0.02, 0.2)# Converting the Parabolic values back to an array
Parabolic = np.array(Parabolic)# Reshaping
Parabolic = np.reshape(Parabolic, (1, 1))# Concatenating with the OHLC Data
my_data = np.concatenate((my_data, Parabolic), axis = 1)Now, we will discuss 4 different strategies that can be formed around the Parabolic SAR. Our aim is to see whether they are good enough or not given our personal backtesting conditions.
The first and most common strategy relies on the position of the current market price relative to the Parabolic SAR. Essentially, we can use the following conditions as trading triggers:
 Go long (Buy) whenever the market price becomes above the Parabolic SAR while the previous price being below it. Hold this position until the market price dives below the Parabolic SAR.
Go short (Sell) whenever the market price becomes below the Parabolic SAR while the previous price being above it. Hold this position until the market price surpasses the Parabolic SAR.def signal(Data, close, psar, buy, sell):
for i in range(len(Data)):
if Data[i, close] > Data[i, psar] and Data[i  1, close] < Data[i  1, psar]:
Data[i, buy] = 1
if Data[i, close] < Data[i, psar] and Data[i  1, close] > Data[i  1, psar]:
Data[i, sell] = 1The above function is the signal of the conditions mentioned. Let us see an example on the EURUSD buy and sell signals generated from the flip strategy.
Signal chart on the EURUSD.
Now, let us check out the results following the below conditions:
 Hourly data since January 2010 with a spread of 0.5 pips per trade.
No risk management used as we are more interested in the flip strategy on its own.
The initial investment is $1,000 and the trades are made through mini lots 0.1.Equity curves following the strategy.
A strategy that relies purely on these conditions is unlikely to succeed in such a complex market. This is evidenced by the backtesting results which were suboptimal in the other currency pairs. Let us move on to the next strategy.
If you are also interested by more technical indicators and using Python to create strategies, then my latest book may interest you:
New Technical Indicators in Python
Amazon.com: New Technical Indicators in Python (9798711128861): Kaabar, Mr Sofien: Books
www.amazon.com
The second strategy relies on more confirmation than the first one. We remain within the same optic of the Flip but we will wait for three periods (dots) to confirm that it is a true flip before we initiate the position. Therefore, the conditions in detail are:
 Go long (Buy) whenever we have three consecutive dots below the market after the bullish flip occurs. Hold this position until the market price dives below the Parabolic SAR.
Go short (Sell) whenever we have three consecutive dots above the market after the bearish flip occurs. Hold this position until the market price surpasses the Parabolic SAR.def signal(Data, close, psar, buy, sell):
for i in range(len(Data)):
if Data[i, close] > Data[i, psar] and Data[i  1, close] > Data[i  1, psar] and Data[i  2, close] > Data[i  2, psar] \
and Data[i  3, close] < Data[i  3, psar]:
Data[i, buy] = 1
if Data[i, close] < Data[i, psar] and Data[i  1, close] < Data[i  1, psar] and Data[i  2, close] < Data[i  2, psar] \
and Data[i  3, close] > Data[i  3, psar]:
Data[i, sell] = 1The above function is the signal of the conditions mentioned. Let us see an example on the EURUSD buy and sell signals generated from the three dots strategy.
Signal chart on the EURUSD.
Now, let us check out the results following the below conditions:
 Hourly data since January 2010 with a spread of 0.5 pips per trade.
A forced 0.25 riskreward ratio using the Average True Range Indicator.
The initial investment is $1,000 and the trades are made through mini lots 0.1.Equity curves following the strategy.
A strategy that relies purely on these conditions is unlikely to succeed in such a complex market. This is evidenced by the backtesting results which were suboptimal in the other currency pairs. Let us move on to the next strategy.
Our final strategy comes from my continuous love of tinkering with indicators and transforming them. There is nothing fancy or complicated about the Parabolic SAR Indicator (SARI). Its formula is simply:
This gives us an oscillator with some subjective boundaries that we can use to predict a Parabolic reversal.
If we consider 0.005 and 0.005 to be the upper and lower bounds, we can form a trading strategy such as the below:
 Go long (Buy) whenever the Parabolic Indicator reaches 0.005 with the previous two values greater than 0.005. Hold the position until getting another signal or getting stopped out by the risk management system.
Go short (Sell) whenever the Parabolic Indicator reaches 0.005 with the previous two values lower than 0.005. Hold the position until getting another signal or getting stopped out by the risk management system.# Indicator / Strategy parameters
upper_barrier = 0.005
lower_barrier = 0.005def signal(Data, what, buy, sell):
for i in range(len(Data)):
if Data[i, what] < lower_barrier and Data[i  1, what] > lower_barrier and Data[i  2, what] > lower_barrier :
Data[i, buy] = 1
if Data[i, what] > upper_barrier and Data[i  1, what] < upper_barrier and Data[i  2, what] < upper_barrier :
Data[i, sell] = 1Signal chart on the EURUSD.
Now, let us check out the results following the below conditions:
 Hourly data since January 2010 with a spread of 0.5 pips per trade.
A forced 0.25 riskreward ratio using the Average True Range Indicator.
The initial investment is $1,000 and the trades are made through mini lots 0.1.Equity curves following the strategy.
To understand this strategy, we need to first understand what normalization is. This great technique allows us to trap the values between 0 and 1 (or 0 and 100 if we wish to multiply by 100). The concept revolves around subtracting the minimum value in a certain lookback period from the current value and dividing by the maximum value in the same lookback period minus the minimum value (the same in the nominator).
We can try to code this formula in python. The below function normalizes a given time series of the OHLC type:
def normalizer(Data, lookback, what, where):
for i in range(len(Data)):
try:
Data[i, where] = (Data[i, what]  min(Data[i  lookback + 1:i + 1, what])) / (max(Data[i  lookback + 1:i + 1, what])  min(Data[i  lookback + 1:i + 1, what]))
except ValueError:
pass
Data[:, where] = Data[:, where] * 100 return DataEURUSD in the first panel with the Parabolic Index 30 periods in the second panel.
We can use the same signal function as above to find the below signals on the EURUSD using a normalization of 5 periods.
Signal chart on the EURUSD.
Now, let us check out the results following the below conditions:
 Hourly data since January 2010 with a spread of 0.5 pips per trade.
A forced 0.25 riskreward ratio using the Average True Range Indicator.
The initial investment is $1,000 and the trades are made through mini lots 0.1.Equity curves following the strategy.
It becomes clear that the Parabolic SAR is a tough indicator and may be better suited as a risk indicator. It may be interesting to do more indepth studies on this.
It is interesting to mention that originally the Parabolic SAR was not meant to be a trading system per se, rather it is more of a risk indicator that can be used to place stops. A basic strategy is using the Parabolic’s values as trailing stops to follow the prevailing trend.
Why was this article written? It is certainly not a spoonfeeding method or the way to a profitable strategy. If you follow my articles, you will notice that I place more emphasize on how to do it instead of here it is and that I also provide functions not full replicable code. In the financial industry, you should combine the pieces yourself from other exogenous information and data, only then, will you master the art of research and trading.
I always advise you to do the proper backtests and understand any risks relating to trading.
 Date of publication:
 Tue, 02/23/2021  09:16
 Link:

Click on the link  it will be copied to clipboard
 Source:
 medium.com
Parabolic StopAndReverse Indicator — The Full Guide.
 Section: