# Creating an Innovative Pull-Back Trading Strategy.

### Coding the Pull-Back Trading Strategy on the Relative Strength Index.

The relative strength index is a known contrarian indicator but can also be used to confirm a new trend by other methods such as a pull-back. We are used to using this technique on the market price, but it can also be used on the technical indicators. This article discusses the method in detail.

I have just released a new book after the success of the previous book. It features advanced trend following indicators and strategies with a **GitHub **page dedicated to the continuously updated code. Also, this book features the original colors after having optimized for printing costs. If you feel that this interests you, feel free to visit the below Amazon link, or if you prefer to buy the PDF version, you could contact me on **LinkedIn**.

### The Relative Strength Index

First introduced by J. Welles Wilder Jr., the RSI is one of the most popular and versatile technical indicators. Mainly used as a contrarian indicator where extreme values signal a reaction that can be exploited. Typically, we use the following steps to calculate the default RSI:

Calculate the change in the closing prices from the previous ones.

Separate the positive net changes from the negative net changes.

Calculate a smoothed moving average on the positive net changes and on the absolute values of the negative net changes.

Divide the smoothed positive changes by the smoothed negative changes. We will refer to this calculation as the Relative Strength — RS.

Apply the normalization formula shown below for every time step to get the RSI.

The above chart shows the hourly values of the GBPUSD in black with the 13-period RSI. We can generally note that the RSI tends to bounce close to 25 while it tends to pause around 75. To code the RSI in Python, we need an OHLC array composed of four columns that cover open, high, low, and close prices.

```
def adder(data, times):
for i in range(1, times + 1):
new = np.zeros((len(data), 1), dtype = float)
data = np.append(data, new, axis = 1)
```

` return data`

```
def deleter(data, index, times):
for i in range(1, times + 1):
data = np.delete(data, index, axis = 1)
```

```
return data
def jump(data, jump):
data = data[jump:, ]
return data
```

```
def ma(data, lookback, close, where):
data = adder(data, 1)
for i in range(len(data)):
try:
data[i, where] = (data[i - lookback + 1:i + 1, close].mean())
except IndexError:
pass
data = jump(data, lookback)
return data
```

```
def ema(data, alpha, lookback, what, where):
alpha = alpha / (lookback + 1.0)
beta = 1 - alpha
data = ma(data, lookback, what, where)
```

` data[lookback + 1, where] = (data[lookback + 1, what] * alpha) + (data[lookback, where] * beta)`

```
for i in range(lookback + 2, len(data)):
try:
data[i, where] = (data[i, what] * alpha) + (data[i - 1, where] * beta)
except IndexError:
pass
return data
```

```
def rsi(data, lookback, close, where):
data = adder(data, 5)
for i in range(len(data)):
data[i, where] = data[i, close] - data[i - 1, close]
for i in range(len(data)):
if data[i, where] > 0:
data[i, where + 1] = data[i, where]
elif data[i, where] < 0:
data[i, where + 2] = abs(data[i, where])
lookback = (lookback * 2) - 1 # From exponential to smoothed
data = ema(data, 2, lookback, where + 1, where + 3)
data = ema(data, 2, lookback, where + 2, where + 4)
```

```
data[:, where + 5] = data[:, where + 3] / data[:, where + 4]
data[:, where + 6] = (100 - (100 / (1 + data[:, where + 5])))
```

```
data = deleter(data, where, 6)
data = jump(data, lookback)
```

` return data`

### Creating the Pull-Back Strategy

The pull-back method is a very known technique in the world of trading, it is based on the fact that after a breakout, the market will stabilize before continuing higher due to profit-taking and increased orders in the cotnrarain direction in anticipation that the support/resistance level will hold. This generally provides a good entry level for the trades in the right direction. Below is an illustration of a pull-back.

Another example can be an exit of a triangular configuration such as the below. The market has broken its descending support outlined with the green upwards pointing arrows, then tried coming back but found resistance at the same descending support line which is now resistance, to finally continue the drop. This is a perfect example of a pull-back.

The idea of the strategy is to apply this method on the famous technical indicator, the Relative Strength Index. It will be easier to code as we have objective barriers already which we call oversold and overbought levels.

**An oversold level is typically below 30 and refers to a state of the market where selling activity was a bit extreme. Therefore, whenever the RSI surpasses the 30 level after being below it, and then retraces back close to it, a bullish signal is generated.****An overbought level is typically above 70 and refers to a state of the market where buying activity was a bit extreme. Therefore, whenever the RSI breaks the 70 level after being above it, and then retraces back close to it, a bearish signal is generated.**

Basically, we will be applying the exact same pull-back method on the Relative Strength Index and using the signals as directional confirmation.

```
lookback = 13
upper_barrier = 70
lower_barrier = 30
margin = 2
```

```
def signal(Data, rsi_column, buy, sell):
Data = adder(Data, 20)
for i in range(len(Data)):
if Data[i, rsi_column] >= lower_barrier and Data[i, rsi_column] <= lower_barrier + margin and Data[i - 1, rsi_column] > lower_barrier and Data[i - 2, rsi_column] < lower_barrier:
Data[i, buy] = 1
elif Data[i, rsi_column] <= upper_barrier and Data[i, rsi_column] >= upper_barrier - margin and Data[i - 1, rsi_column] < upper_barrier and Data[i - 2, rsi_column] > upper_barrier:
Data[i, sell] = -1
return Data
```

`# The margin variable is the maximum distance for the pull-back to be valid. A margin of 2 and a lower barrier of 30 means that that the RSI can retrace back to between 30 and 32 and the event can still be considered a pull-back`

The chart above shows the one signal generated by the algorithm. It can be seen that the moment the RSI has fulfilled all the conditions, a bearish signal around the top has been generated.

Many optimization and modifications can be added to the algorithm such as:

**Tweaking the margin variable so that more pull-backs are taken into account. We should not make this variable too high so that we remain in the spirit of the strategy.****Adding the condition that looks more into the past so that slow pull-backs are considered.****Tweaking the RSI’s lookback period even though the default values are recommended as a very short RSI has unreliable pull-back signals due to the short life between the extremes.****Adding a moving average break confirmation signal.**

The pull-back method is theoretically a laggard compared to the extremes and the barrier exit ones but is the best regarding to entry optimization and realization of the reaction due to the fact that the extremes can last a long time and the barrier exit can provide an expensive entry level. Also, the pull-back method can help avoid false breakouts.

If you are also interested by more technical indicators and strategies, then my book might interest you:

Remember to always do your back-tests. You should always believe that other people are **wrong**. My indicators and style of trading may work for me but maybe not for you.

I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.

### One Last Word

I have recently started an NFT collection that aims to support different humanitarian and medical causes. **The Society of Light** is a set of limited collectibles which will help make the world slightly better as each sale will see a percentage of it sent directly to the charity attributed to the avatar. As I always say, nothing better than a bullet list to outline the benefits of buying these NFT’s:

**High-potential gain**: By concentrating the remaining sales proceedings on marketing and promoting**The Society of Light**, I am aiming to maximize their value as much as possible in the secondary market. Remember that trading in the secondary market also means that a portion of royalties will be donated to the same charity.**Art collection and portfolio diversification**: Having a collection of avatars that symbolize good deeds is truly satisfying. Investing does not need to only have selfish needs even though there is nothing wrong with investing to make money. But what about investing to*make money, help others, and collect art?***Donating to your preferred cause(s):**This is a flexible way of allocating different funds to your charities.**A free copy of my book in PDF**: Any buyer of any NFT will receive a free copy of my latest book shown in the link of the article.

## Create your profile

## Only paid subscribers can comment on this post

Sign in## Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.